Enumeration in C (C enum)

Enumeration is also called enum is a user defined data type and here we will learn its use in C programming. Enumeration consists of integral constant and we use enum keyword to declare an enumeration in C.

Syntax of enum in C :

// syntax of enumeration in C

enum flag {constant1, constant2, constant3, ..... , constantN};

By default the value of constant1 is = 0, constant2 is = 1, constant3 is = 2 …… constantN is = (N – 1). Let’s try to print their value using a C program.

// printing value from enum

#include <stdio.h>
enum flag{constant1, constant2, constant3, constant4, constant5};

int main(){

    enum flag first_const;
    enum flag last_const;

    first_const = constant1;
    last_const = constant5;

    printf("%d\n", first_const);
    printf("%d\n", last_const);
    
    return 0;
}

This program will give us output like this :

c-enumeration-c-enum

Don’t worry if you don’t understand this program that how it works. We will discuss more now.

However we can change the value of every constant here. We can give any value against the constant. The value of a name will be the value of previous name + 1.

#include <stdio.h>
enum all_day{saturday = 1, sunday , monday = 7, tuesday,
                      wednesday, thursday = 14, friday};

int main(){
    printf("%d %d %d %d %d %d %d", saturday, sunday, monday, tuesday,
            wednesday, thursday, friday);
    return 0;
}

Program output :

1 2 7 8 9 14 15

But if we do not assign any value against a name, then the default value will be printed as like as the index of an array where the value is started from zero.

#include <stdio.h>
enum all_day{saturday, sunday , monday, tuesday,
            wednesday, thursday, friday};

int main(){
    printf("%d %d %d %d %d %d %d", saturday, sunday, monday, tuesday,
            wednesday, thursday, friday);
    return 0;
}

Output of this program :

0 1 2 3 4 5 6

A enum should be unique in their scope otherwise we will get an error. Let’s take an example of program which can not be compiled.

#include <stdio.h>
enum first {male, female};
enum second {female, nutral};


int main(){
    return 0;
}
error: redeclaration of enumerator 'female'

Why we use enumeration (enum) in C

Suppose you want to design three button for windows operating system. These are bold, italic and underline. Now, let’s take the given example;

enum button_design{
	italic = 1,
	bold = 2,
	underline = 4
} button;

Now, let’s think this for 8 bit register. The binary equivalent will be as like as bellow;

c-enumeration-c-enum-use-of-enum-in-c

This is the reason why the integral constants are a power of 2 in the above pseudocode. Now if anyone use two or more design then we can easily understand it without overlapping. We use bitwise OR operator here. Let’s see the program bellow;

#include <stdio.h>

enum button_design{
	italic = 1,
	bold = 2,
	underline = 4
} button;

int main(){
	int selected_design = italic | underline;

	printf("%d\n", selected_design);

	return 0;
}

Output :

5
Here the output is 5 because of OR operation

00000001   =    1
00000100   =    4
_____________________________
00000101   =    5

Now we can easily understand that what design a user is selected.