C EnumsLesson 42Free preview
C Enums
This lesson is part of C · All levels
Summary
Enumeration defines a set of constant values called number-based, so code is more readable than using a magic number without a name. The first member by default is 0 and the next one is added to 1, unless it is given an initial value manually. Six is suitable for status, menu selection, and constant used repeatedly in switch branches.
Key points
- The basic syntax is enum {RED, GREEN, BLU}; then used as enum W = GREEN;
- The first member is default 0, the next automatically adds 1, and the value can be assigned manually.
- The enum values behave like int so they can be compared and used in the switch.
- The enum replaces the magic number so the code is easier to understand and bug.
- In C, the enum is not a strong type, so the normal numbers can still be added to the enum variable.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.