枚举常量属于int类型。默认情况下,枚举列表中的第一个常量值为零,每个连续常量值都高一个值。

#include <stdio.h>

enum color {
  RED   /* 0 */ ,
  GREEN /* 1 */ ,
  BLUE  /* 2 */
}c, d;

int main()
{
    enum color c = RED;

    printf("red:%dn",c);
    c = BLUE;
    printf("n");
    printf("%dn",c);
    return 0;
}

可以通过为常量赋值来覆盖这些默认值。值可以计算,不必是唯一的。

enum color {
  RED   = 5,         /* 5 */
  GREEN = RED,       /* 5 */
  BLUE  = GREEN + 2, /* 7 */
  ORANGE             /* 8 */
};