Search code examples
cenumsc89

Why enums have names in C?


Let's take the following example:

#include <stdio.h>


enum fruit  {APPLE, ORANGE, BANANA};
enum planet {EARTH, MARS, NEPTUNE};


void foo(enum fruit f)
{
    printf("%ld", f);
}


int main()
{
    enum planet p = MARS;

    foo(p); /* compiler doesn't complain */

    return 0;
}

What's the point of enums having names, if that code would compile?
Here is my command line for gcc:

gcc file.c -ansi -Wall -Wextra

Solution

  • AFAIK, in C99, the "tag" (what you call the name, e.g. fruit) of enum is optional.

    So

    enum { BLACK, RED, GREEN, BLUE, YELLOW, WHITE };
    

    is acceptable (and quite useful)

    and you can even give values integral constants

    enum { RFCHTTP =2068, RFCSMTP= 821 };
    

    which is a more modern way than

    #define RFCHTTP 2068
    #define RFCSMTP 821
    

    (in particular, such enum values are known to the debugger, when you compile with debug info)

    But in C enum are mostly integral values. Things are different in C++.

    Regarding using %ld format control conversion for printf with an enum argument it is probably some undefined behavior (so you should be scared). Probably gcc -Wall -Wextra might warn you.

    BTW, GCC has many options. Read its chapter on Invoking GCC.

    I recommend (in 2017) coding for C99 or C11, not old 1989 ANSI C.