Ad space (banner)
๐Ÿ”งC Lessons
Lesson 41 / 68

Enum (Enumerated Types) Basics

This lesson covers the basics of C's Enum (enumerated type), so you can work with a fixed set of options under readable names. It's for anyone searching "C enum usage."

enum lets you create an "enumerated type" representing only a fixed set of options. Use it when you want to limit a value to a known set of categories ahead of time โ€” like "a traffic light is only red, green, or yellow."

The example defines three options with typedef enum { RED, GREEN, BLUE } Color;, and assigns one option, Color c = RED;, to a variable. Notice that internally, C's enum is treated as a sequence of integers starting at 0.

A common early mistake is displaying an enum's value with printf() and seeing the number rather than the name. Unlike some other languages, it's not automatically displayed by name โ€” you'd need to write your own conversion logic to display the name.

In real projects, using a named enum instead of scattering plain strings or numbers everywhere makes your code's intent much clearer.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)