Monday, June 11, 2012

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


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 DT_FIFO DT_FIFO
DT_CHR = 2,
# define DT_CHR DT_CHR
DT_DIR = 4,
# define DT_DIR DT_DIR
DT_BLK = 6,
# define DT_BLK DT_BLK
DT_REG = 8,
# define DT_REG DT_REG
DT_LNK = 10,
# define DT_LNK DT_LNK
DT_SOCK = 12,
# define DT_SOCK DT_SOCK
DT_WHT = 14
# define DT_WHT DT_WHT
};



What is this construct for? - why define something with an identical string, which will then compile to the int value?


Source: Tips4all

5 comments:

  1. 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.

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

    ReplyDelete
  3. 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

    ReplyDelete
  4. 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

    ReplyDelete
  5. 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.

    ReplyDelete