1

Here is some C code :

int i;
printf("This text is printed\nThis text is not until the for loop end.");
for (i = 5; i > 0; i--)
{
    printf("%d", i);
    sleep(1);
}

Why is the rest of the text after the '\n' not printed before the for loop start? Even the printf inside the for loop is only printed after the loop end. If I put a '\n' at the end of the text, it prints, but I do not want a new line.

This only happens on Linux and not on Windows (just changed sleep(1) to Sleep(1000) and added windows.h).

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
varnaud
  • 227
  • 2
  • 11
  • It's been answered: http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – dcaswell Aug 27 '13 at 00:43
  • Windows and Linux have very different console output drivers. In Linux, the output is being buffered until the `\n` occurs in the case of your program. – lurker Aug 27 '13 at 00:44
  • Why do you think you don't want a newline at the end of your output? There must be a reason you (and a lot of other Windows programmers) don't output it, but it seems weird to Unix programmers (e.g. me) — just as outputting a newline seems weird to Windows programmers, I guess. On Unix, you do it to make sure the output is produced — so there's a sound reason to do it. On Windows, presumably, the default mode corresponds to non-buffered so all output is generated as the call ends. – Jonathan Leffler Aug 27 '13 at 01:15
  • I did not want the new line, because I wanted the loop to output on the same line each turn. I was used to see it working on Windows and I was surprised to see that on Linux. I knew it was something about buffering. Thanks all. – varnaud Aug 28 '13 at 03:33

3 Answers3

3

It's caused by output buffering. When you call printf, the output isn't printed immediately. It is written to a buffer to be output sometime in the future. Usually a \n character causes the buffer to be flushed automatically. You can also flush the stdout buffer manually:

printf("This text is printed\nThis text is not until the for loop end.");
fflush(stdout);
Paul
  • 130,653
  • 24
  • 259
  • 248
1

Look up setvbuf() and _IONBF vs _IOLBF vs _IOFBF. By default, when the output is to a terminal, the output is line buffered (_IOLBF). This means that output only appears when a newline is included. _IONBF is non-buffered, sometimes reserved for standard error (though that too can be line buffered). _IOFBF is fully-buffered; the output won't appear until the internal buffer is full, or you explicitly fflush() the output (or, sometimes, if you read from standard input). If you write to a pipe, or a file, the output will normally be fully buffered.

In general, you should expect to end outputs with a newline if you want to see them in a timely fashion — Windows-based examples to the contrary notwithstanding. (It is a common meme on Stack Overflow that reasonably reliably identifies when a program is written for Windows.)

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
1

The buffer of stdout is not specified by the C standard. But POSIX implies(not requires) that usually stdout is line buffered when it's connected to a terminal, is fully buffered otherwise.

Linux follows this POSIX convention but Windows does not. You can change the default behavior using setvbuf or use fflush to flush the output, that's guaranteed by the C standard.

Yu Hao
  • 111,229
  • 40
  • 211
  • 267