- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Enumeration
![]() Share with a Friend |
C Programming - C Enumeration
C Enumeration ( enum)
In C, an enumeration is a user-defined data type that consists of integral constants, making the code more readable and organized. It is declared using the enum keyword, and the values within the enumeration are automatically assigned integer values starting from 0 by default.
Syntax
C
enum enumeration_name {
constant1,
constant2,
constant3,
// ...
};
- enumeration_name: Optional name for the enumeration.
- constant1, constant2, ...: Named integral constants.
Key Features
- Improved Readability:
- Replaces numerical values with meaningful names.
- Default Value Assignment:
- Values start from 0 and increment by 1 unless explicitly assigned.
- Type Safety:
- Enforces usage of predefined constants.
Example: Enumeration Basics
C
#include <stdio.h>
// Define an enumeration
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
int main() {
enum Day today;
today = WEDNESDAY;
printf("Today is day number %d\n", today);
return 0;
}
Output:
Today is day number 3
Custom Values in Enumeration
You can explicitly assign values to some or all constants in an enumeration.
C
#include <stdio.h>
enum Status {
SUCCESS = 1,
FAILURE = -1,
PENDING = 0
};
int main() {
enum Status currentStatus;
currentStatus = FAILURE;
printf("Current status: %d\n", currentStatus);
return 0;
}
Output:
Current status: -1
Enumerations with Switch Statements
Enumerations are often used in switch statements for better code clarity.
C
#include <stdio.h>
enum TrafficLight {
RED,
YELLOW,
GREEN
};
int main() {
enum TrafficLight light = GREEN;
switch (light) {
case RED:
printf("Stop\n");
break;
case YELLOW:
printf("Ready\n");
break;
case GREEN:
printf("Go\n");
break;
default:
printf("Invalid\n");
}
return 0;
}
Output:
Go
Advantages of enum
- Improved Code Clarity:
- Descriptive names replace arbitrary numerical values.
- Reduced Errors:
- Using named constants minimizes the chance of errors.
- Ease of Maintenance:
- Changes to the constants’ values affect only the enum definition.
Limitations of enum
- Lack of Type Safety:
- Enum values are treated as integers, which can lead to unintended usage.
- Limited Functionality:
- No support for strings or non-integer types.
- Cannot Be Extended:
- Once defined, an enum cannot be modified without recompiling the code.
Using Typedef with enum
You can combine typedef with enum for simpler syntax.
C
#include <stdio.h>
typedef enum {
SPRING,
SUMMER,
FALL,
WINTER
} Season;
int main() {
Season current = FALL;
printf("The current season is %d\n", current);
return 0;
}
Output:
The current season is 2
Conclusion
Enumerations are a convenient way to define and manage integral constants in C programs. They enhance code readability, reduce the likelihood of errors, and are commonly used in applications requiring state management or symbolic representation of constant values.
