int main()
{
printf("main started");
for(;;) {}
return 0;
}
The problem is this it does not print "main started". Doesn't matter what is in for loop and how many statements are there before loop.
int main()
{
printf("main started");
for(;;) {}
return 0;
}
The problem is this it does not print "main started". Doesn't matter what is in for loop and how many statements are there before loop.
Put a newline at the end of the output. Without it the text goes into the output buffer but doesn't get flushed.
You need to flush the output:
fflush(stdout);
Or, terminate the string by \n
character as most systems have line buffered standard output.
printf
prints the string only after flushing it. It auto flushes the data if you add \n
(newline) symbol in the string (although it usually works, there is no guarantee! Don't rely on it). Data is also auto-flushed when the program finishes - after returning from the main function or calling exit
.
In your case program never finishes because of the for loop without a condition, autoflush is never called as a result
If you want to flush it anyway you can force flush yourself calling fflush(stdout)
. The same function is called automatically at exit.
See the similar question which I asked earlier about standard guarantees regarding auto-flush. Is there a guarantee of stdout auto-flush before exit? How does it work?
What you print with printf()
is saved in a buffer. This means that it's not immediately sent to your console. However, this buffer is flushed:
\n
), So, in your case, to see it immediately, add a fflush(stdout)
, or a do printf("main started\n")
.