gcc - Why does an infinite loop block a printf statement earlier in my C program? -
this question has answer here:
- why programs “skip over” printfs? 4 answers
this c program prints done, enters infinite loop.
#include <stdio.h> int main(int argc, char const *argv[]) { printf("done"); while (1) {} return 0; } but when run it, code not print done. why that?
it needs flush buffer. console output doesn't flush until receives '\n'.
there's little-used function this, fflush().
#include <stdio.h> int main(int argc, char const *argv[]) { printf("done"); fflush(stdout); while (1) {} return 0; } that should print you.
Comments
Post a Comment