I have code like this below:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("hello");
while(1){
// whatever here
}
}
and the question is: why the first instruction is skipped? It runs only the loop, hello is never printed. I compiled it with gcc and g++ with the same result.
Your assumption is wrong, your code does run, only stdout
is not flushed, but buffered.
Use fflush(stdout)
after printf("hello")
, this forces stdout to be printed.
And, as @Bathsheba pointed out, also a newline character ("\n"
) within the printf
forces it to flush, which is explained in this SO question.