In Linux C, what is the difference between stdout and STDOUT FILENO?
Following some research, I’ve come to the following conclusion. Could you assist me in reviewing it and correcting any errors? Thanks
As a result, the STDOUT FILENO, in my perspective, belongs to system-level calling and, to some extent, functions as a system API. Any device in the system can be described with STDOUT FILENO.
The stdout is located at a higher level (user level?) and encapsulates the STDOUT FILENO information. There is an I/O buffer on stdout.
That is how I interpret their differences. Thank you for any feedback or corrections.
Asked by Bing Lu
The standard output stream is represented by the FILE* “constant” stdout. So fprintf(stdout, “x= percent dn”, x); behaves the same as printf(“x= percent dn”, x);; stdout is used for stdio.h> methods like fprintf, fputs, and so on.
STDOUT FILENO is a file descriptor with an integer value (actually, the integer 1). You may use it to make a write syscall.
STDOUT FILENO == fileno is the relationship between the two (stdout)
(Unless you do strange stuff like fclose(stdout); or maybe some freopen after some fclose(stdin), which you should nearly never do!) See J.F.Sebastian’s comment on this.)
The FILE* items are frequently preferred because they are buffered (so usually perform well). To flush buffers, you may need to use the fflush command.
For syscalls like write(2) (which is used by the stdio library) or poll, you may utilize file descriptor numbers (2). However, utilizing syscalls is cumbersome. It may provide excellent performance (but is difficult to code), although the stdio library is frequently sufficient (and more portable).
(Of course, for stdio functions, you should #include stdio.h>, and for syscalls like write, you should #include unistd.h> -and some other headers.) And because syscalls are used to implement stdio functions, fprintf may call write).
Answered by Basile Starynkevitch
Post is based on https://stackoverflow.com/questions/12902627/the-difference-between-stdout-and-stdout-fileno