What is Enum type - C Data Type

C examples for Data Type:enum

Introduction

Enum is a user-defined type and an enum type has a fixed list of named constants.

The following code creates an enumeration type for color: RED, GREEN and BLUE.

enum color { RED, GREEN, BLUE };

The color type variable now can hold one of these constant values.

Demo Code

#include <stdio.h>

enum color { RED, GREEN, BLUE };
int main()/*from w w  w .j  a v  a2 s  .  c o  m*/
{
    enum color c = RED;

    printf("%d",c);

    return 0;
}

Result

Enum variables can be declared along with the definition of enum type itself.

enum color { RED, GREEN, BLUE } c, d;

The following code shows how to use enum in switch statement.

Demo Code



#include <stdio.h>

enum color { RED, GREEN, BLUE } c, d;
int main()/*from www .  j  ava  2  s  .  c  o  m*/
{
    enum color c = RED;

    switch(c) {
      case RED:   printf("red:%d",c);break;

      case GREEN: printf("%d",c);break;

      case BLUE:  printf("%d",c);break;
    }


    return 0;
}

Result


Related Tutorials