i know because of buffering of stdout when can expect output of stdout in following proggram. if run, getting "stderr" output. if add '\n' or fflush(stdout) getting both statements. if don't add '\n' or fflush(stdout),i not getting "stdout" output. when buffered "stdout"s output if don't add '\n' or fflush(stdout).
#include <stdio.h> #include <unistd.h> int main() { for(;;) { fprintf(stdout,"stdout"); fprintf(stderr,"stderr"); sleep(1); } return 0; }
indeed, new line or flush because default stdout
line buffered when refers terminal device.
to more precise: standard input , standard output buffered, if , if not refer interactive device. standard error never buffered.
about line buffering, quoting apue:
line buffering comes 2 caveats. first, size of buffer standard i/o library using collect each line fixed, i/o might take place if fill buffer before writing newline. second, whenever input requested through standard i/o library either (a) unbuffered stream or (b) line-buffered stream (that requiresdata requested kernel), line-buffered output streams flushed. reason qualifier on (b) requested data may alreadybe in buffer, doesn't require data read kernel. obviously, input unbuffered stream, item (a), requires data obtained kernel.
to change unbuffered, use setvbuf
:
setvbuf(stdout, null, _ionbf, 0);
Comments
Post a Comment