What is an enum in C ?

 
An enum in C is used to enumerate things . For example , to enumerate the days of the week , such as Monday , Tuesday or Wednesday , or to enumerate error codes , such as OK or WARN , or maybe to enumerate colors , such as red , or blue .

Data which is enumerated is of the int type. The enum type , have a type which is compatible with one of : char , signed or unsigned integer type . The choice of the enum type is implementation defined , but it must be able to store all the values of the enumerated data .

The enum keyword is used to declare an identifier of the enum type ,

enum color ;
enum error_codes;

The enum color , and enum error_codes are incomplete types because no enumerated data has been initialized , they cannot be used later on , to declare variable of type enum color or enum error_codes .

enum color ;
enum color red ;
/* Will cause the following compilation error :
 error: variable has incomplete type 'enum color'
  enum color red ;*/

So an enum type , such as an enum color is used to enumerate things , an enum type is declared by using the enum keyword . An enum type must list its enumerated data using an enumerator list , when being declared , or else it will be an incomplete type . This can be done like this :

enum daysOfWeek { Monday , Tuesday , Wednesday , Thursday , Friday , Saturday , Sunday };

The preceding declaration declares the identifier daysOfWeek to be of the enum type , it is initialized with enumerated data of the int type : Monday , Tuesday , Wednesday , Thursday , Friday , Saturday , and Sunday . Monday will have a value of 0 , Tuesday a value of 1 , and each subsequent data will have a value of the previous one plus one .

#include<stdio.h>

enum daysOfWeek  { Monday , Tuesday , Wednesday , Thursday , Friday , Saturday , Sunday };

int main ( void ) {

  printf( "Monday is : %d \n", Monday );
  printf( "Tuesday is : %d \n", Tuesday );
  printf( "Wednesday is : %d \n", Wednesday );
  printf( "Thursday is : %d \n", Thursday );
  printf( "Friday is : %d \n", Friday );
  printf( "Saturday is : %d \n", Saturday );
  printf( "Sunday is : %d \n", Sunday );}

/* output :
Monday is : 0 
Tuesday is : 1 
Wednesday is : 2 
Thursday is : 3 
Friday is : 4 
Saturday is : 5 
Sunday is : 6 */

The enumerated data , such as Monday , or Tuesday , or Friday … have a scope , which is where these identifiers are visible . Their scope is the scope where they are declared , so if they are declared globally , as in the previous example , they have a source file or a global scope , as such they can be accessed from anywhere in the source file , if declared inside a function , they have a block scope , so they can only be accessed from within the function …

The enumerated data , is also referred to as an enumeration constant , since once it is assigned a value in an enumerator list , its value cannot change .

It is not necessary to provide an identifier for an enum type , only the data can be initialized .

#include<stdio.h>

int main ( void ) {
  enum  { Monday , Tuesday , Wednesday , Thursday , Friday , Saturday , Sunday };
  printf( "Tuesday is : %d \n" , Tuesday );}

/* output :
Tuesday is : 1*/

The initialized data , can be initialized to specific values using the = operator . The next enumerated data , will always have a value of the previous one , plus one . In addition to that , enumerated data , can be initialized to have the same values .

#include<stdio.h>

enum code { OK , WARN, ERROR , PASS = 0 , INFORMATIONAL , EXCEPTION };

int main ( void ) {
    printf( "[OK,%d] , [WARN,%d] , [ERROR,%d]\n" , OK , WARN , ERROR  );
    printf( "[PASS,%d] , [INFORMATIONAL,%d] , [EXCEPTION,%d]\n" , PASS , INFORMATIONAL , EXCEPTION );}

/* output :
[OK,0] , [WARN,1] , [ERROR,2]
[PASS,0] , [INFORMATIONAL,1] , [EXCEPTION,2]
*/

A variable of an enum type , can be declared using one of these methods :

enum color{red , green , blue}aFirstColor ;
enum color aSecondColor ; 
enum {OK, WARN }status_code ;

In the first method , the variable aFirstColor of the enum type color , is declared while declaring and initializing the enum color type .

In the second method , the variable aSecondColor is declared after having declared the enum type . And in the last method , a variable status_code is declared without assigning an identifier to the enum type .

It is not necessary for a variable of an enum type , to have as a value one of the enumerated values , it can be assigned any value .

#include<stdio.h>

enum networkState { up , down };

int main ( void ) {
  enum networkState state = 2 ;
  enum networkState *statePtr =	&state;
  switch( *statePtr )
    {
    case up :
      printf( "network state is up\n" );
      break;
    case down :
      printf( "network state is down\n" ) ;
      break ;
    default :
      printf( "network state is unknown\n" );
  }}

/* output :
network state is unknown */