C program getting compiled but not showing output - c

I am not sure why my program is not showing "Hello World". I am trying by executing only printf(). Is there anything I am missing here?
Below is my complete program
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}

My guess is that the console window which contains the output flashes by so quickly that you don't have time to see it. You need to put in something to halt the program so you can see the output. One way of doing it is to ask the user to press the Enter key.
Something like
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
printf("Press the Enter key to continue\n");
(void) getc(stdin);
return 0;
}

Related

Why does my CodeBlocks app keep failing to execute code?

Just started to learn programming language and now I'm learning C. When I run this simple code, my CodeBlocks app keeps failing to execute the code. Here is the code I wrote:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
printf("Hello world!\n");
printf("Hello world!\n");
printf("Hello world!\n");
return 0;
}
This is the result after I clicked the run button:
Hello world!
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
Any idea why does my CodeBlocks app keep failing to execute code?
You should compile, then run the app, for a shortcut, press F9.
Press this
or this:

printf is not printing even after enabling buffering

so I was having a problem with a big code that uses strings and file names. I had many issues with it and I addedd some printf in bwtween to Keep check of how the Code works. I couldnt get anything in the output so i decided to take to old good "hello world" program to see if printf was the issue.
so here is the Code :
#include <stdio.h>
int main (void){
printf("hello world\n");
}
I thought Maybe it is a Buffering issue, so I have tried some variations like :
#include <stdio.h>
int main (void){
setvbuf(stdout, NULL, _IOLBF, 0);
printf("hello world\n");
}
and
#include <stdio.h>
int main (void){
printf("hello world\n");
setbuf(stdout, NULL);
}
still Nothing is printed. any idea on how to find the issue? ps: I am writing on Notepad and i execute using gcc on terminal. gcc is installed just fine and I am working on Windows.
I checked the file's path and it is fine. The command I use is gcc filename.c

can someone tell me what's wrong with my program using fork() system calls(Programming C)

In this program, I am trying to use fork() functions to create 6 child processes and execute executionnode() functions,but I feel there is something wrong in my output!
what happened to my code or system calls?
I have a program like this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
int executionnode(int i);
int main(){
pid_t childpid;
int i;
int row=6;
for(i=0;i<row;i++)
{ childpid=fork();
if(childpid==0)
continue;
else if (childpid>0)
executionnode(i);
else {
perror("something wrong");
exit(1);
}
}
}
int executionnode(int i){
sleep(i);
printf("hello, I am process:%ld\n",(long)getpid());
char *execArgs[] = { "echo", "Hello, World!", NULL };
execvp("echo", execArgs);
}
Everytime when I run this program,the output is always like this in my Linux terminal:
/*
hello, I am process:3226
Hello, World!
lixx3527#tiandiao123:~/Desktop/pa1-release$ hello, I am process:3227
Hello, World!
hello, I am process:3228
Hello, World!
hello, I am process:3229
Hello, World!
hello, I am process:3230
Hello, World!
hello, I am process:3231
Hello, World!
*/
I find my output even didn't finish, which means my program's executions haven't been finished, but lixx3527#tiandiao123:~/Desktop/pa1-release$ has appeared in the terminal ahead of time.
what happened to my code or system calls?
can someone help me explain it? I mean why lixx3527#tiandiao123:~/Desktop/pa1-release$ appeared before the program finished its all the execution?
thank you!
The parent runs very quickly, forking children. Then it quits and the shell prints a prompt. Then the other children run and print data. It looks like one child is able to print before the shell prints a prompt, and the others don't. If you want the parent to wait for the children to finish, there is a function conveniently named wait that will do that.

How to write a Hello World in C

I wrote little program to print "Hello world" in C. I'm not a C programmer, but I liked to try it. In my program there is an error. Please tell me what is it?
This is my program:
int main(){
printf("Hello World");
}
I wrote this with my Java Experiences. I can't find what is wrong.
You can't directly use printf() function as in Java. You should tell the compiler that you are going to use the input/output stream. You can tell it in this line:
#include <stdio.h>
and also you should enter this line at the end of the source code:
return 0;
this will tell the compiler :
"If the program succeed It will return 0 otherwise It will return any other number"
This means if your program is successful main() function will return 0. Then the compile know the program is Ok.
Then at last your complete code is:
#include <stdio.h>
int main() {
printf("Hello world");
return 0;
}
To compile this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type
gcc hello.c -o hello && hello
(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)
Remember My computer is Windows. And this compile code is for windows. If your OS is UNIX like OS. then use this code to compile:
gcc hello.c -o hello
./hello
A full hello world program in C:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
Then compile (assuming gcc) and execute it:
gcc -o test test.c
./test
First, you have to use a header file.
#include <stdio.h>
What that does is bring up a header file with a bunch of commands in them. That will make it recognize the "printf" piece of code.
Next, you have to close your program. By not having a closing statement, the program will not compile because it doesn't know if that is the end of the code. Use this at the end of your program...
return 0;
That closes the program, meaning that the compiler can stop looking for other code. You may also want to look at some programming manuals (there are dozens of free online ones) to learn about the syntax.
One last thing: Most pieces of C code require a semicolon at the end. This is not true for the "int main" statement, nor is it for the header file which I have defined above. The "return" function that closes the program, does however, need a semicolon.
Hoped this helped.
Should also include a pause at the end:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
//Read a character from the console
getchar();
return 0;
}
Just like import in Java programs, in here you have to include libraries you're using in your program. You have used library function printf, but not included stdio.h.
I agree there are many ways to write one of the simplest way is
#include<stdio.h>
int main(void){
printf("Hello World\n");
return 0;
}
You can even use different ways as suggested above.
You should first look at the structure of "main". Try to understand the various parts as already explained so well in the above answers.
"#include" : The preprocessing directives to be included in the program. But why? Because you are trying to use the functions defined inside them.
int : The return type of "main" program. But why? Because the function calling "main" needs to know if the "main" program has functioned correctly.
main : The entry point of your code. Dont ask why here :-)
main( void ) : To tell the compiler that we are not passing any arguments to program "main"
return 0 : Beacuse you promised "main" that you will return something if "main" will function properly.
Finally the code:
#include <stdio.h>
int main( void )
{
printf( "Hello World\n" ) ; //Notice the '\n' here. Good coding practice.
return 0 ;
}
#include <stdio.h> //Pre-processor commands<br/>
void main() //Starting point of the program<br/>{ //Opening Braces
printf("Hello World\n"); //Print Hello World on the screen<br/>
return 0;
} //Ending Braces
Check it once it will work, I have written it with comments:
#include<stdio.h> //Pre-processor commands
void main() {
printf("Hello World\n"); //Print Hello World on the screen
}
A full hello world program in C:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
Then compile (assuming gcc) and execute it:
gcc -o test test.c
./test
You can't use printf() function as in Java. You have to tell the compiler what you are going to use.
You can tell this as follows:-
#include <stdio.h>
You must enter this line in last:-
return 0;
Then Your complete code is:-
#include <stdio.h>
int main(){
printf("Hello World");
return 0;
}
For compiling this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type:-
gcc hello.c -o hello && hello
(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)
Remember My computer is Windows. So I can compile only for Windows OS.
#include <stdio.h>
int main() {
// printf, used to print (display) Hello World
printf("Hello World ! ");
// return 0, as the main function is of type int so it must return an integer value
return 0;
}

How to use System(const char*) in TC++

Today , When i coding, met a question..my Code as follow:
#include<stdlib.h>
void main()
{
system("dir");
getch();
}
The question : The user Screen is nothing..Why ? where is my result?
If you want the output when using system, at least into something you can read in your application, you need to pipe the output:
system("dir > /tmp/output.txt");
FILE *f = fopen("/tmp/output.txt", "r");
char text[1024]; // max sizeof of 1 kb, any more and I'd consider using `malloc()` instead.
fread(text, 1, 1024, f);
printf("%s\n", text);
fclose(f);
There are some problems in your program, at least one of which has already been mentioned.
void main() should be int main(void).
As I recall, the Windows/DOS getch function is declared in <conio.h>; you should have a #include directive for it. Be aware that both <conio.h> and getch are non-standard.
Since main returns int, you should return an int result.
But none of these problems explain the problem you're seeing.
With these changes:
#include <stdlib.h>
#include <conio.h>
int main(void)
{
system("dir");
getch();
return 0;
}
This should work; it should show a directory listing of whatever directory your program runs in (which is determined by TC; I don't know the details).
It's possible that the program is running in an empty directory, which means the dir command wouldn't show any files, but it should still produce some output.
Try commenting out the system() call and adding a printf call (note the added #include <stdio.h>):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Hello, world\n");
getch();
return 0;
}
This should open a console window, print "Hello, world" in it, and wait for you to type Enter.
If you still don't see any output (either no console window, or a console window with nothing in it), then you have a problem that's not related to the system() call. Most likely the problem has to do with the way you're using Turbo C (I presume that's what "TC" stands for).
The main function in every C program is supposed to return an int you are returning void
Change void to int:
#include<stdlib.h>
int main()
{
system("dir");
getch();
}
When I tested, the dir command ran in my console and printed to standard out.
May be he is the running the program directly in the Turbo C IDE and hence his output is not visible. If he runs the program directly from cmd line it works. I remember you need to run Alt - F5 or some other combination to see the output window in Turbo C++

Resources