47

I want to turn off the buffering for the stdout for getting the exact result for the following code

while(1) {
    printf(".");
    sleep(1);
}

The code printf bunch of '.' only when buffer gets filled.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Sreenath Nannat
  • 1,949
  • 1
  • 13
  • 18
  • 1
    possible duplicate of [Why does printf not flush after the call unless a newline is in the format string? (in C)](http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – DarkDust Oct 24 '11 at 14:05
  • 1
    Candidate for *questions that get asked the most on StackOverflow*. – DarkDust Oct 24 '11 at 14:06
  • Possible duplicate of [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – S.S. Anne Aug 22 '19 at 11:04

5 Answers5

123

You can use the setvbuf function:

setvbuf(stdout, NULL, _IONBF, 0);

Here're some other links to the function.

SeetheMoar
  • 105
  • 3
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • 7
    Good one. Thx. By the way `setbuf(stream, NULL);` is equivalent to `setvbuf(stream, NULL, _IONBF, BUFSIZ);` – sehe Oct 24 '11 at 14:02
  • 18
    One caveat: It's only legal to call `setbuf` or `setvbuf` as the **very first** operation on a stream before any input or output is performed on it. Thus using `fflush` explicitly is usually a better idea. – R.. GitHub STOP HELPING ICE Oct 24 '11 at 14:56
23

You can also use setbuf

setbuf(stdout, NULL);

This will take care of everything

Eswar Yaganti
  • 2,536
  • 1
  • 20
  • 22
  • 1
    this is not a very good answer, quoting Newlib: `Both ANSI C and the System V Interface Definition (Issue 2) require <>. However, they differ on the meaning of a <> buffer pointer: the SVID issue 2 specification says that a <> buffer pointer requests unbuffered output. For maximum portability, avoid <> buffer pointers.` – MightyPork Mar 04 '16 at 23:16
  • 2
    @MightyPork Newlib is just flat-out **wrong**. [The POSIX 2 specification for `setbuf()`](https://pubs.opengroup.org/onlinepubs/7908799/xsh/setbuf.html) states: "`setvbuf(stream, buf, _IONBF, BUFSIZ)` if buf is a null pointer." [`_IONBF` means unbuffered](https://port70.net/~nsz/c/c11/n1570.html#7.21.5.6p2) Both [POSIX 7](https://pubs.opengroup.org/onlinepubs/9699919799/functions/setbuf.html) and [the C standard](https://port70.net/~nsz/c/c11/n1570.html#7.21.5.5) all agree, and have agreed for [decades](https://port70.net/~nsz/c/c89/c89-draft.html#4.9.5.5). – Andrew Henle Feb 13 '20 at 22:00
-3

Use fflush(FILE *stream) with stdout as the parameter.

http://www.elook.org/programming/c/fflush.html

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
NickLH
  • 2,643
  • 17
  • 28
-4

You can do this:

write(1, ".", 1);

instead of this:

printf(".");
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
-14

Use fflush(stdout). You can use it after every printf call to force the buffer to flush.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
tdenniston
  • 3,389
  • 2
  • 21
  • 29