Wednesday, September 16, 2009

Why I couldnt work with the gcc compiler without ‘\n’ in printf?

I have written a printf() statement like below:

printf("hello\n");

this works fine when built using Linux' gcc compiler. However if I write

printf("hello");

the print doesn't appear on the screen. There is some buffering mechanism it seems?


Answer :

Even if buffering isn't a problem, if you don't print the newline your shell's prompt might be clobbering the output.

you are for example using gcc in a unix shell and at the end of your program do printf("hello") it won't print a newline before your shells prompt is displayed. The prompt will be printed on that same line, sometimes overwriting the entire line depending on kind of prompt you have set up.


No comments:

Post a Comment