What is the difference between stdin and STDIN_FILENO?

后端 未结 4 2016
悲&欢浪女
悲&欢浪女 2020-12-24 00:58

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

4条回答
  •  萌比男神i
    2020-12-24 01:43

    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 
    #include 
    
    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;
    }
    

提交回复
热议问题