The following C program doesn't printing anything on the screen.
I compiled the program with gcc
:
#include<stdio.h>
main()
{
printf("hai");
for(;;);
}
The following C program doesn't printing anything on the screen.
I compiled the program with gcc
:
#include<stdio.h>
main()
{
printf("hai");
for(;;);
}
Most likely, stdout
is line buffered. Your program does not call fflush
or send a newline so the buffer does not get written out.
#include <stdio.h>
int main(void) {
printf("hai\n");
for(;;)
;
return 0;
}
See also question 12.4 and What's the correct declaration of main()? in the C FAQ.
Standard output tends to be line buffered by default so the reason you're not seeing anything is because you haven't flushed the line.
This will work:
#include <stdio.h>
int main (int argC, char *argV[])
{
printf("hai\n");
for(;;)
;
return 0;
}
Alternatively, you could fflush
standard out or just get rid of the infinite loop so the program exits:
#include <stdio.h>
int main (int argC, char *argV[])
{
printf("hai");
return 0;
}
but you probably want the newline there anyway.
This is caused by the buffering which takes place in stdio (i.e. it is not output immediately unless you tell it to by including a \n or fflush). Please refer to Write to stdout and printf output not interleaved which explains this.
(p.s. or the compiler is not happy about the typo in #include)
Your for(;;) loop stops the stream from being flushed. As others have suggested, add a newline to the string being output, or flush the stream explicitly:
fflush( stdout );
after your printf. And correct the spelling of #include.