C Enumeration

C Tutorial

1. What is a C Enumeration?

  • A C Enumeration (enum) is a user-defined data type in C.

  • It consists of a set of named integer constants.

  • Makes code more readable and less error-prone compared to using raw integers.

Syntax:


2. Example: Basic Enum


 

Output:

Day number: 3

By default, the first enumerator has value 0, the next 1, and so on.


3. Assigning Custom Values


 

Output:

Day number: 1
  • After Sunday = 1, the next members automatically increment (Monday = 2, Tuesday = 3, …).


4. Using Enum with Switch Case


 

Output:

Today is Friday

5. Enum with Typedef


 

Output:

Color value: 1

typedef allows you to use Color directly without writing enum Color.


6. Key Points About Enumerations

  1. Enum members are constants of type int by default.

  2. Useful for readable code instead of using raw integers.

  3. Default values start from 0 and increment by 1.

  4. You can assign custom integer values.

  5. Can be used with switch statements for better clarity.

  6. Combine with typedef for simpler syntax.