c - Is adding the newline character to printf() equivalent to flushing the stream? -
the following program shows how buffered i/o can cause problems in programs when errors 'divide zero' happen:
int main() { int = 1, b = 0, c; printf("dividing..."); c = a/b; printf("answer is: %d\n", c); return 0; } the output floating point exception (core dumped).
fair enough. surprisingly, if changed first printf printf("dividing...\n");, text gets printed before program crashes (i'm running gcc on linux, way).
am conclude adding newline equivalent flushing? , if so, if printf() strings end in \n, i'm depriving myself of benefits of buffered i/o?
if standard output going terminal, yes, it's equal flushing, printf() use line-based buffering.
if it's redirected file, no. in case printf() uses bigger buffers, corresponding file system block sizes or whatever, optimize io.
Comments
Post a Comment