← Back to C Course | Chapter 8: Structures & Unions | Lesson 6 of 7

C Enumerations

An enumeration is a fixed list of named choices, like days of the week or traffic light colors. It gives readable names to numbers.
Syntax
c
enum EnumName { VALUE_ONE, VALUE_TWO, VALUE_THREE };

enum EnumName variable_name = VALUE_ONE;

What is an Enumeration?

Writing status instead of magic numbers like 0, 1, 2 makes code far easier to read and maintain -- comparing if (status == ACTIVE) is immediately clear, unlike if (status == 1) which requires you to remember what 1 means.

Example: What is an Enumeration?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Status { ACTIVE, INACTIVE };
// Program execution starts in main()
int main() {
	enum Status status = ACTIVE;
	// Check whether status == ACTIVE
	if (status == ACTIVE) {
		// Print formatted text to the screen
		printf("Active");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Defining an Enum

This declares a new type alongside a set of named integer constants belonging to it, similar in spirit to how struct introduces a new composite type -- both use the same curly-brace-and-semicolon syntax.

Example: Defining an Enum

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
// Program execution starts in main()
int main() {
	enum Color c = GREEN;
	// Print formatted text to the screen
	printf("%d", c);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Enum Constant Values

enum Color {RED, GREEN, BLUE} assigns RED=0, GREEN=1, BLUE=2 automatically -- this predictable numbering is why enums work seamlessly as array indices or switch-case values.

Example: Enum Constant Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d %d", RED, GREEN, BLUE);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Custom Enum Values

enum Status {ACTIVE=1, PENDING, CLOSED} explicitly sets ACTIVE to 1, and since PENDING and CLOSED are unassigned, they automatically continue the sequence as 2 and 3.

Example: Custom Enum Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Status { ACTIVE = 1, PENDING, CLOSED };
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d %d", ACTIVE, PENDING, CLOSED);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Using Enums in switch Statements

Because switch requires constant integer expressions for its case labels, and enum constants are exactly that, they're one of the few types (besides plain integers and chars) that switch statements can branch on directly.

Example: Using Enums in switch Statements

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
// Program execution starts in main()
int main() {
	enum Color c = GREEN;
	// Choose a branch based on the value of c
	switch (c) {
		case RED: printf("Red"); break;
		case GREEN: printf("Green"); break;
		case BLUE: printf("Blue"); break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming the compiler stops you from assigning an invalid integer to an enum variable, when C treats enums like ints.
  2. Forgetting that enumerators start at 0 by default unless you assign values.
  3. Printing an enum with %s and expecting its name, when it holds an integer and needs %d.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.