Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off buffering of stdout in C

Tags:

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.

like image 927
Sreenath Nannat Avatar asked Oct 24 '11 13:10

Sreenath Nannat


People also ask

Is stdout buffered in C?

In C, file output is block buffered. Output to stdout is line buffered. The stderr device is unbuffered.

What is buffering stdout?

By default writes to stdout pass through a 4096 byte buffer, unless stdout happens to be a terminal/tty in which case it is line buffered. Hence the inconsistency between the immediate output when your program is writing to the terminal and the delayed output when it is writing to a pipe or file.

What is Setbuf stdout NULL );?

setbuf() — Control Buffering The stream pointer must refer to an open file before any I/O or repositioning has been done. If the buffer argument is NULL, the stream is unbuffered. If not, the buffer must point to a character array of length BUFSIZ, which is the buffer size that is defined in the <stdio.

What is the default buffering scheme for standard output?

Default Buffering modes:stdout is buffered (line buffered if connected to a terminal) stderr is unbuffered.


1 Answers

You can use the setvbuf function:

setvbuf(stdout, NULL, _IONBF, 0); 

The link above has been broken. Here're another links to the function.

  • POSIX

  • C/C++

like image 75
Frerich Raabe Avatar answered Oct 07 '22 23:10

Frerich Raabe