C - Standard Print Order

The code is as follows:

printf("u");
write(STDOUT_FILENO, "m", 1);
printf("d\n");

conclusion: dirt

Can someone explain why the output is printed in this order?

+4
source share
3 answers

Standard output is buffered by default, which means printf("u")it will put "u"into its buffer until a character fflushor a new character in a line appears . To see the result in order, try the following:

printf("u");
fflush(stdout);
write(STDOUT_FILENO, "m", 1);
printf("d\n");
+3
source

printf "u" . write . printf "d\n" . - ( , printf , ), .

+2

C . printf u , m write, , printf d , , ( , FILE* ), \n , .

stderr does not load by default, try the following:

fprintf(stderr, "u");
write(STDERR_FILENO, "m", 1);
fprintf(stderr, "d\n");
+1
source

All Articles