Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between stdout and STDOUT_FILENO

Tags:

c

linux

stdout

I was wondering the difference between stdout and STDOUT_FILENO in Linux C.

After some searching work, I draw the following conclusion. Could you help me review it and correct any mistake in it? Thanks

  • stdout belongs to standard I/O stream of C language; whose type is FILE* and defined in stdio.h

  • STDOUT_FILENO, possessing an int type, is defined at unistd.h. It's a file descriptor of LINUX system. In unistd.h, it's explained as below:

The following symbolic constants shall be defined for file streams:

STDERR_FILENO
    File number of stderr; 2.
STDIN_FILENO
    File number of stdin; 0.
STDOUT_FILENO
    File number of stdout; 1.

So, in my opinion, the STDOUT_FILENO belongs system-level calling and, to some extent, like a system API. STDOUT_FILENO can be used to describe any device in system.

The stdout locates in a higher level (user level?) and actually encapsulate the details of STDOUT_FILENO. stdout has I/O buffer.

That's my understand about their difference. Any comment or correction is appreciated, thanks.

like image 817
Bing Lu Avatar asked Oct 15 '12 19:10

Bing Lu


People also ask

What value is STDOUT_FILENO?

STDOUT_FILENO. Standard output value stdout. Its value is 1.

What is Stdin_fileno?

stdin is a default FILE pointer used to get input from none other than standard in. STDIN_FILENO is the default standard input file descriptor number which is 0 . It is essentially a defined directive for general use.

Where is Stderr_fileno defined?

On program startup, the integer file descriptors associated with the streams stdin, stdout, and stderr are 0, 1, and 2, respectively. The preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are defined with these values in <unistd.

What does stderr mean?

Stderr, also known as standard error, is the default file descriptor where a process can write error messages. In Unix-like operating systems, such as Linux, macOS X, and BSD, stderr is defined by the POSIX standard. Its default file descriptor number is 2.


1 Answers

stdout is a FILE* "constant" giving the standard outout stream. So obviously fprintf(stdout, "x=%d\n", x); has the same behavior as printf("x=%d\n", x);; you use stdout for <stdio.h> functions like fprintf, fputs etc..

STDOUT_FILENO is an integer file descriptor (actually, the integer 1). You might use it for write syscall.

The relation between the two is STDOUT_FILENO == fileno(stdout)

(Except after you do weird things like fclose(stdout);, or perhaps some freopen after some fclose(stdin), which you should almost never do! See this, as commented by J.F.Sebastian)

You usually prefer the FILE* things, because they are buffered (so usually perform well). Sometimes, you may want to call fflush to flush buffers.

You could use file descriptor numbers for syscalls like write(2) (which is used by the stdio library), or poll(2). But using syscalls is clumpsy. It may give you very good efficiency (but that is hard to code), but very often the stdio library is good enough (and more portable).

(Of course you should #include <stdio.h> for the stdio functions, and #include <unistd.h> -and some other headers- for the syscalls like write. And the stdio functions are implemented with syscalls, so fprintf may call write).

like image 137
Basile Starynkevitch Avatar answered Oct 05 '22 17:10

Basile Starynkevitch