Mastering C Textbook
Enum Essentials
Enums (short for enumerations) are a way to assign names to a set of numbers, which makes your code easier to read and manage—especially when working with related constants.
Think of it like giving nicknames to a group of numbers. Instead of writing raw numbers like 0, 1, or 2, you can give them meaningful names like RED, GREEN, and BLUE.
Defining An Enum
Here’s how to define an enum in C:
enum Color { RED, GREEN, BLUE };This creates a new type called Color, and it automatically assigns numbers starting from 0 to each name:
REDis0GREENis1BLUEis2
You can use it like this:
enum Color favoriteColor = GREEN;
printf("My favorite color number is: %d", favoriteColor);Members only
Keep reading this lesson
You've reached the end of the free preview. Unlock the rest of this lesson plus 11 more lessons in Mastering C Textbook.
- Unlimited coding questions
- Unlimited access to all classroom material
- Weekly live call for questions and guidance
- 3,000 AI tutor tokens / month
- Personal SQL database (3 MB)
- Progress tracking
Cancel any time.