Concept
How do you declare an enumerated type in C?
ComputerScienceOne / Enumerated Types
"C allows you to define enumerated types which allow you to define a fixed list of possible values. For example, the days of the week or months of the year are possible values used in a date. However, they are more-or-less fixed (no one will be adding a new day of the week or month any time soon). An enumerated type allows us to define these values using human-readable keywords (much like the #define macro). To declare an enumerated type we use the keywords typedef enum (short for type definition and enumeration). We then provide a comma delimited list of keywords inside a code block. At the end of the code block we provide the type’s identifier. That is, the name of the type itself. For example, the names of integer and floating-point types in C are int and double respectively. This identifier gives our type a name that we can later use to declare variables of that type. Consider the following example.\n\n1 typedef enum {\n\n2 SUNDAY,\n\n3 MONDAY,\n\n4 TUESDAY,\n\n5 WEDNESDAY,\n\n6 THURSDAY,\n\n7 FRIDAY,\n\n8 SATURDAY\n\n9 } DayOfWeek;\n\nIn this example we’ve defined an enumeration of the days of the week. The name of the type itself is DayOfWeek and we can now declare variables of this type. The possible 309"
Related Ideas
- How do enumerated types represent named values in C?ComputerScienceOne · Using Enumerated Types for Error Codes
- How can enumerated types be used for error codes in C?ComputerScienceOne · Using Enumerated Types for Error Codes
- How are structures defined and named in C?ComputerScienceOne · Alternative Declarations
- What topics are covered in error handling?ComputerScienceOne · Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . .
- How can a quadratic roots function return error codes?ComputerScienceOne · Error Handling By Design
- How do you declare variables in C?ComputerScienceOne · Declaration & Assignment
- What primitive types does C support?ComputerScienceOne · Declaration & Assignment
- How do you declare and use an enumerated type in Java?ComputerScienceOne · More Tricks