Join Regular Classroom : Visit ClassroomTech

Usage of enum in C

Enumeration is a user defined datatype in C language. It is used to:
1. Assign names to the integral constants.
2. Makes a program easy to read and maintain program readability and better debugging capability..
3. Enum has a great choice for using as flag.
4. In enum, variables are automatically assigned with a key value starting from 0.

Let’s see an example

/* C program to understand advantages of enum */
/* www.codewindow.in */

#include <stdio.h>

enum Goals {    // Declaring an enum named Goals
	Messi = 660,
	Ronaldo = 770,
	Neymar = 350,
	Ramos = 250
};  // We initialized four players with their number of goals

int main() {
    int max=0;
    
    /* To print the values we assigned follow the below statement */
	
	printf("Messi = %d\nRonaldo = %d\nNeymar = %d\nRamos = %d", Messi, Ronaldo, Neymar, Ramos);	
	return 0;
}

Output:

Messi = 660
Ronaldo = 770
Neymar = 350
Ramos = 250

You Missed

Also Checkout