具有重复值的枚举

枚举值绝不必是唯一的:

#include <stdlib.h> /* for EXIT_SUCCESS */
#include <stdio.h> /* for printf() */

enum Dupes
{
   Base, /* Takes 0 */
   One, /* Takes Base + 1 */
   Two, /* Takes One + 1 */
   Negative = -1,
   AnotherZero /* Takes Negative + 1 == 0, sigh */
};

int main(void)
{
  printf("Base = %d\n", Base);
  printf("One = %d\n", One);
  printf("Two = %d\n", Two);
  printf("Negative = %d\n", Negative);
  printf("AnotherZero = %d\n", AnotherZero);

  return EXIT_SUCCESS;
}

样本打印:

Base = 0
One = 1
Two = 2
Negative = -1
AnotherZero = 0