Let's have a look at this Hello World program
#include <stdio.h>
int main(int argc, char ** argv) {
printf("Hello, World!");
const char* sFile = "/dev/stdout"; // or /proc/self/fd/0
const char* sMode = "w";
FILE * output = fopen(sFile, sMode);
//fflush(stdout) /* forces `correct` order */
putc('!', output); // Use output or stdout from stdio.h
return 0;
}
When compiled using the output
file descriptor the output is:
!Hello, World!
when compiled using the stdout
file descriptor provided by stdio.h
the output is as expected:
Hello, World!!
I figure when calling putc
with the latter, it will print directly to the stdout
and when using the file descriptor on /dev/stdout
it will open a pipe and print into that. I'm not certain though.
The behavior is even more interesting, as it does not overwrite the first character of the 'Hello' but rather pushes itself into the first position of the line buffer in front of the already pushed string.
From a logical point of view this is quiet unexpected.
Can anyone explain what exactly is going on here?
I'm using
cc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
and a 3.13.0-52 linux kernel compiled w/ gcc 4.8.2
Edit: I've done an strace
of both programs, and here is the important part:
The output
(fopen("/dev/stdout", "w")) without fflush(stdout)
scenario produces:
...
open("/dev/stdout", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
fstat(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f62f21e9000
write(3, "!", 1!) = 1
write(1, "Hello, World!", 13Hello, World!) = 13
exit_group(0) = ?
using fflush(stdout)
produces and enforces correct order:
...
open("/dev/stdout", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
write(1, "Hello, World!", 13Hello, World!) = 13
fstat(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5ad4557000
write(3, "!", 1!) = 1
exit_group(0) = ?
The stdout
(from stdlib.h) scenario produces:
...
write(1, "Hello, World!!", 14Hello, World!!) = 14
exit_group(0) = ?
So it seems the FILE * output = fopen("/dev/stdout")
stream uses a different file descriptor than stdout
Also as it seems printf
uses stdout
Thus in the third scenario the string is assembled before it is pushed on the stream.