What is the difference between stdin and STDIN_FILENO?

C

C Problem Overview


What is the practical difference, if any, between stdin and STDIN_FILENO in C?

C Solutions


Solution 1 - C

The interface. Like everyone else has said, stdin is a FILE * as defined by the standard c library. You can use some of the higher level interfaces like fread, fwrite, and fprintf. On the other hand, STDIN_FILENO is just a file descriptor (almost certainly 0). This uses a slight lower level interface through the likes of read and write.

Solution 2 - C

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.

Solution 3 - C

From /usr/include/stdio.h,

/* Standard streams.  */
extern struct _IO_FILE *stdin;          /* Standard input stream.  */
extern struct _IO_FILE *stdout;         /* Standard output stream.  */
extern struct _IO_FILE *stderr;         /* Standard error output stream.  */
/* C89/C99 say they're macros.  Make them happy.  */
#define stdin stdin
#define stdout stdout
#define stderr stderr

From /usr/include/unistd.h

/* Standard file descriptors.  */
#define STDIN_FILENO    0       /* Standard input.  */
#define STDOUT_FILENO   1       /* Standard output.  */
#define STDERR_FILENO   2       /* Standard error output.  */

Ex, stdin (_IO_FILE defined in /usr/include/libio.h) is a structure data. STDIN_FILENO is a macro constant, which points to a file descriptor used by kernel.

#include <stdio.h>
#include <unistd.h>

void
stdin_VS_STDIN_FILENO(void)
{
    printf("stdin->_flags = %hd\n", stdin->_flags);
    printf("STDIN_FILENO  : %d\n", STDIN_FILENO);
}

int
main(void)
{
    stdin_VS_STDIN_FILENO();
    return 0;
}

Solution 4 - C

stdin : 1. A file pointer (* FILE) 2. The file descriptor table holds its address when process is created. 3. present in /usr/include/stdio.h

STDIN_FILENO : 1. It is a macro 2. Its nothing but an array index of a file descriptor table (default 0). 3.present in /usr/include/unistd.h

Could be more clear by following code.

#include<stdio.h> 
#include<unistd.h> 
int main() 
{	 
	 printf("%d\t\t%p ----- ",STDIN_FILENO,stdin);
	
return 0; 
} 

Solution 5 - C

The STDIN_FILENO in the linux header #include "unistd.h" is just a macro for 0 which stands for the stdin for the pipe, when you are using Linux pipe.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1212697View Question on Stackoverflow
Solution 1 - CMichaelView Answer on Stackoverflow
Solution 2 - CsquiguyView Answer on Stackoverflow
Solution 3 - CdebugView Answer on Stackoverflow
Solution 4 - CUdhay DevgotraView Answer on Stackoverflow
Solution 5 - CJeroen TanView Answer on Stackoverflow