Enum Constant Values - C Data Type

C examples for Data Type:enum

Introduction

Enumerated constants are of the int type.

By default, the first constant in the enum list has the value zero and each successive constant is one value higher.

Demo Code

#include <stdio.h>

enum color {//from   w  w w  . ja  v  a2  s  . c  o  m
  RED   /* 0 */ ,
  GREEN /* 1 */ ,
  BLUE  /* 2 */
}c, d;

int main()
{
    enum color c = RED;

    printf("red:%d",c);

    c = BLUE;

    printf("\n");

    printf("%d",c);

    return 0;
}

Result

These default values can be overridden by assigning values to the constants.

The values can be computed and do not have to be unique.

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

Related Tutorials