Enumerated Types

Raheelanjum
2 min read1 day ago

Enumerated types are a special way of creating your own type in C. The type is a "list of key words." Enumerated types are used to make a program clearer to the reader/maintainer of the program. For example, say we want to write a program that checks for keyboard presses to find if the down arrow or up arrow has been pressed. We could say: if (press_value == 32). 32 is the computer's representation of the down arrow. Or, we could create our own enumerated type with the keywords: down_arrow and up_arrow. Then we could say: if (press_value == down_arrow). This second version is much more readable and understandable to the programmer.

Enumerated Types

Enumerated types allow us to create our own symbolic names for a list of related ideas. The key word for an enumerated type is enum. For example, we could create an enumerated type for true and false (note: this is done for you by C and is type bool).

We could also create an enumerated type to represent various security levels so that our program could run a door card reader:

--

--

Raheelanjum
Raheelanjum

Written by Raheelanjum

Highly skilled and experienced content writer dedicated to crafting compelling, informative content that resonates with diverse audiences. Expertise spans blog

Responses (26)