- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Enumeration (or enum) in C
Enumeration (or enum) in C
C enumeration (enum) is an enumerated data type that consists of a group of integral constants. Enums are useful when you want to assign user-defined names to integral constants. The enum keyword is used to define enums.
Defining and Declaring an Enum Type
To declare and define an enumeration (enum) data type, use the enum keyword followed by the enum name and assign all values inside the curly braces.
Syntax
This is the syntax you would use to define an enum type −
enum enum_name{const1, const2, ... };
Enum Variable Declaration
After declaring an enumeration type, you can also declare its variable to access enumeration members (constants). To declare an enum variable, write the enum keyword followed by the enaum name (enum_name) and then the variable name (var).
Syntax
Below is the syntax to declare a variable of enum type −
enum enum_name var;
Example
Let us define an enum type with the name myenum −
enum myenum {val1, val2, val3, val4};
Identifier values are unsigned integers and they start from "0". val1 refers 0, val2 refers 1, and so on.
A variable of myenum type is declared as follows −
enum myenum var;
The possible constant values of myenum type are enumerated inside the curly brackets.
Change Enum Constants Values
When we declare an enum type, the first constant of the enum is initialized with 0 by default, the second constant is initialized with 1, and so on. We can also assign any integer value to any constant of enum.
Example
In the following example, we are declaring an enum type and assigning different values to the enum constants.
#include <stdio.h> enum status_codes { OKAY = 1, CANCEL = 0, ALERT = 2 }; int main() { // Printing values printf("OKAY = %d\n", OKAY); printf("CANCEL = %d\n", CANCEL); printf("ALERT = %d\n", ALERT); return 0; }
Output
It will produce the following output −
OKAY = 1 CANCEL = 0 ALERT = 2
Enum in Switch Statements
C language switch case statement works with integral values. We can use enum type to define constants with (or, without) integral type values to use them in switch case statements.
Example
In the following example, the colors in the rainbow are enumerated.
#include <stdio.h> // Enum declaration enum colors { VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED }; int main() { // Enum variable declaration enum colors color = YELLOW; // switch statement using enum switch (color) { case BLUE: printf("Blue color"); break; case GREEN: printf("Green color"); break; case RED: printf("Red color"); break; default: printf("Color other than RGB"); } return 0; }
Output
Run the code and check its output −
Color other than RGB
Examples of Enumeration (enum) Type
Practice the following examples to understand the concept of enumeration (or, enum) type in C programming language.
Example 1: Enum Constants Get Incrementing Integer Values
C assigns incrementing integer values to each constant, starting with "0". When we assign val2 to the above variable,
var = val2;
The integer value assigned to val2 is 1. Take a look at the following example −
#include <stdio.h> enum myenum {val1, val2, val3, val4}; int main(){ enum myenum var; var = val2; printf("var = %d", var); return 0; }
Output
It will produce the following output −
var = 1
Example 2: Enumerating the Weekdays
Let us declare an enum type weekdays to enumerate the names of the days and assign its value to the enum type variable −
#include <stdio.h> int main(){ enum weekdays {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; printf ("Monday = %d\n", Mon); printf ("Thursday = %d\n", Thu); printf ("Sunday = %d\n", Sun); }
Output
It will produce the following output −
Monday = 1 Thursday = 4 Sunday = 0
Example 3: Declaring a Variable of Enum Type
A typical application of enumerated data types is to assign weekdays to the integer values. In this code, we have declared a variable of weekdays enum type −
#include <stdio.h> enum weekdays {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; int main(){ enum weekdays day; day = Wed; printf("Day number of Wed is = %d", day); return 0; }
Output
When you run this code, it will produce the following output −
Day number of Wed is = 3
Example 4: Enum Values By Default Start from "0"
In this code, we define an enum type for names of the month in a calendar year. By default, C assigns the values starting from "0". For example, in the following C program, Jan gets the value "0", Feb gets "1", and so on.
#include <stdio.h> enum months{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; int main(){ for (int i = Jan; i <= Dec; i++) printf("Month No: %d\n", i); return 0; }
Output
Run the code and check its output −
Month No: 0 Month No: 1 Month No: 2 Month No: 3 Month No: 4 Month No: 5 Month No: 6 Month No: 7 Month No: 8 Month No: 9 Month No: 10 Month No: 11
Example 5: Starting an Enum from 1
To start enumeration from 1, assign 1 explicitly to the first value, the compiler will assign incrementing numbers to the subsequent values.
#include <stdio.h> enum months{Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; int main(){ for (int i = Jan; i <= Dec; i++) printf("Month No: %d\n", i); return 0; }
Output
Run the code and check its output −
Month No: 1 Month No: 2 Month No: 3 Month No: 4 Month No: 5 Month No: 6 Month No: 7 Month No: 8 Month No: 9 Month No: 10 Month No: 11 Month No: 12
Example 6: Enumerating HTTP Status Codes
It is not necessary that the constants in the enum type should have incremental integer values. You can assign each of the constants a different and unique value, which may be in any sequence.
In the following code, the enum type enumerates HTTP status codes.
#include <stdio.h> enum status { OK = 200, BadRequest = 400, Unauthorized = 401, Forbidden = 403, NotFound = 404, InternalServerError = 500, }; int main(){ enum status code = InternalServerError; if (code == 500) printf("Internal Server Error has been encountered"); return 0; }
Output
Run the code and check its output −
Internal Server Error has been encountered
Example 7: Assigning a Fixed Number to Enum Constants
You can assign a fixed integer to some of the constants, leaving the compiler to assign values to others. All unassigned names get a value that is "value of previous name plus one".
#include <stdio.h> enum myenum {a, b = 5, c, d, e = 10, f}; int main(){ printf("a: %d\n", a); printf("b: %d\n", b); printf("c: %d\n", c); printf("d: %d\n", d); printf("e: %d\n", e); printf("f: %d\n", f); return 0; }
Output
Run the code and check its output −
a: 0 b: 5 c: 6 d: 7 e: 10 f: 11
Applications of Enums
We can use enums in C in different cases, some of which are listed below −
- To store constant values, for example, weekdays, months, directions, colors, etc.
- Enums are used for using flags, status codes, etc.
- Enums can be used while using switch-case statements in C.