c / beginner
Snippet
The enum Keyword
An enumeration (enum) is a user-defined data type that consists of a set of named integer constants. By default, the first name is assigned the value 0, the second 1, and so on. Enums make the code much easier to read and maintain by replacing magic numbers with descriptive names.
snippet.c
1
2
3
enum Level { LOW, MEDIUM, HIGH };enum Level myStatus = MEDIUM;printf("Status is %d", myStatus);
Breakdown
1
enum Level { LOW, MEDIUM, HIGH };
Defines a new enumeration type named Level with three possible values.
2
enum Level myStatus = MEDIUM;
Declares a variable of type 'enum Level' and assigns it the value MEDIUM (which is 1).