Nearby lessons

64 of 72

C - enum

An enumeration or enum gives meaningful names to a group of integer constants. It makes program values easier to understand, such as days, colours, or order status.

1. What Is an enum?

An enum lets us give meaningful names to related integer constants. This is easier to understand than remembering what each number means.

In simple words: Instead of writing a number like 1 for green, we can use a name such as GREEN.

Example01
CCode Cell
1#include <stdio.h>
2 
3enum Colour {
4 RED,
5 GREEN,
6 BLUE
7};
8 
9int main(void) {
10 enum Colour selectedColour = GREEN;
11 
12 printf("RED = %d\n", RED);
13 printf("GREEN = %d\n", GREEN);
14 printf("BLUE = %d\n", BLUE);
15 printf("Selected colour value = %d\n", selectedColour);
16 
17 return 0;
18}
Output
RED = 0
GREEN = 1
BLUE = 2
Selected colour value = 1

2. Default Values in enum

If you do not assign values, C starts with 0 for the first name and increases each following value by one.

Example02
CCode Cell
1#include <stdio.h>
2 
3enum Day {
4 SUNDAY,
5 MONDAY,
6 TUESDAY,
7 WEDNESDAY
8};
9 
10int main(void) {
11 printf("Sunday: %d\n", SUNDAY);
12 printf("Monday: %d\n", MONDAY);
13 printf("Tuesday: %d\n", TUESDAY);
14 printf("Wednesday: %d\n", WEDNESDAY);
15 
16 return 0;
17}
Output
Sunday: 0
Monday: 1
Tuesday: 2
Wednesday: 3

3. Assign Your Own Values

You can assign specific integer values to enum names. If the next name has no value, it normally gets one more than the previous enumerator.

Example03
CCode Cell
1#include <stdio.h>
2 
3enum Status {
4 SUCCESS = 1,
5 PENDING = 5,
6 FAILED = 10,
7 CANCELLED
8};
9 
10int main(void) {
11 printf("SUCCESS = %d\n", SUCCESS);
12 printf("PENDING = %d\n", PENDING);
13 printf("FAILED = %d\n", FAILED);
14 printf("CANCELLED = %d\n", CANCELLED);
15 
16 return 0;
17}
Output
SUCCESS = 1
PENDING = 5
FAILED = 10
CANCELLED = 11

4. Use enum with switch

Using an enum with switch makes each case easy to understand because we can use meaningful names instead of numbers.

Example04
CCode Cell
1#include <stdio.h>
2 
3enum Menu {
4 HOME = 1,
5 ABOUT,
6 EXIT
7};
8 
9int main(void) {
10 enum Menu choice = ABOUT;
11 
12 switch (choice) {
13 case HOME:
14 printf("Home page selected\n");
15 break;
16 
17 case ABOUT:
18 printf("About page selected\n");
19 break;
20 
21 case EXIT:
22 printf("Exit selected\n");
23 break;
24 
25 default:
26 printf("Invalid choice\n");
27 }
28 
29 return 0;
30}
Output
About page selected

5. enum vs #define

Both enum and #define can be used to give names to values. An enum groups related integer constants together and can automatically assign the values.

  • Use enum for related options such as days, menu choices, and statuses.
  • Use #define when you need a preprocessor replacement, such as a simple symbolic constant.

Common Mistakes

  • Remember that the first enum value is 0 by default, not 1.
  • Use the exact enum constant names defined in your program.
  • Do not assume C automatically rejects every integer value that is not listed in the enum.
  • Do not forget the semicolon after the enum definition.
📝 Key Takeaways
  • The enum keyword defines an enumeration.
  • By default, the first enum value is 0 and each next value increases by 1.
  • You can assign your own values to enum constants.
  • enum works well with switch statements.
  • C does not automatically restrict an enum variable to only the listed values.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4