Sreenath Nannat
Sreenath Nannat

Reputation: 2019

How to turn off buffering of stdout in C

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.

Upvotes: 53

Views: 57358

Answers (5)

Frerich Raabe
Frerich Raabe

Reputation: 94279

You can use the setvbuf function:

setvbuf(stdout, NULL, _IONBF, 0);

Here're some other links to the function.

Upvotes: 129

tdenniston
tdenniston

Reputation: 3519

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

Upvotes: -14

NickLH
NickLH

Reputation: 2645

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

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

Upvotes: -3

Mickael Ciocca
Mickael Ciocca

Reputation: 121

You can do this:

write(1, ".", 1);

instead of this:

printf(".");

Upvotes: -3

Eswar Yaganti
Eswar Yaganti

Reputation: 2646

You can also use setbuf

setbuf(stdout, NULL);

This will take care of everything

Upvotes: 28

Related Questions