What is the purpose of a these #define within an enum?

前端 未结 5 1086
借酒劲吻你
借酒劲吻你 2020-12-01 11:56

I found this code in the linux headers, /usr/include/dirent.h:

enum   
  {    
    DT_UNKNOWN = 0,
# define DT_UNKNOWN DT_UNKNOWN
    DT_FIFO = 1,
# define D         


        
相关标签:
5条回答
  • 2020-12-01 12:18

    I think that Luchian Grigore's answer was correct.

    Code without defines:

    #include <stdio.h>
    
    // Defined somewhere in headers
    #define DT_UNKNOWN 0
    
    enum
    {
        DT_UNKNOWN = 0,
        DT_FIFO = 1,
    };
    
    int main(int argc, char **argv)
    {
        printf("DT_UNKNOWN is %d\n", DT_UNKNOWN);
        return 0;
    }
    

    From the compiler's output it is unclear why some line of code inside enum doesn't want to build:

    alexander@ubuntu-10:~/tmp$ gcc -Wall ./main.c 
    ./main.c:7: error: expected identifier before numeric constant
    

    After we add such defines:

    #include <stdio.h>
    
    // Defined somewhere in headers
    #define DT_UNKNOWN 0
    
    enum
    {
        DT_UNKNOWN = 0,
        # define DT_UNKNOWN DT_UNKNOWN
        DT_FIFO = 1,
        # define DT_FIFO    DT_FIFO
    };
    
    int main(int argc, char **argv)
    {
        printf("DT_UNKNOWN is %d\n", DT_UNKNOWN);
        return 0;
    }
    

    Compiler would tell us that DT_UNKNOWN is redefined and a place where it is redefined:

    alexander@ubuntu-10:~/tmp$ gcc -Wall ./main2.c 
    ./main2.c:7: error: expected identifier before numeric constant
    ./main2.c:8:1: warning: "DT_UNKNOWN" redefined
    ./main2.c:3:1: warning: this is the location of the previous definition
    
    0 讨论(0)
  • 2020-12-01 12:24

    Besides the other answers which are very good - I would go with them for the main reason - the compiler could generate warnings or errors if you try to redefine DT_UNKNOWN.

    0 讨论(0)
  • 2020-12-01 12:29

    My (uneducated) guess is that the #define statements allow conditional tests to see if the constant has been defined.

    For example:

    #ifdef DT_UNKNOWN
        // do something
    #endif
    
    0 讨论(0)
  • 2020-12-01 12:32

    My guess is that some other code can then check if one of (or more of) these enum values have been defined using #ifdef.

    0 讨论(0)
  • 2020-12-01 12:37

    I used -E and -dD arguments (and also -fdump-tree-all) in gcc to see the preprocessor output and found nothing useful. So I guess this code does not have any functionality other than perhaps displaying the symbolic names when debugging using a debugger like gdb.

    0 讨论(0)
提交回复
热议问题