Bifalgorithm post · Computer science one · Using Enumerated Types for Error Codes

How do enumerated types represent named values in C?

How do enumerated types represent named values in C? (Computer science one · Using Enumerated Types for Error Codes) — "19. Error Handling values it can take a…

Ideas in this flowchart

  • Enumerated types in C provide named values but require careful error handling.
  • An enumerated type can take named values such as SUNDAY and MONDAY.
  • A DayOfWeek variable can be compared with SUNDAY or SATURDAY to detect the weekend.
  • Type identifiers use upper camel casing, while enumerated values use uppercase underscores.
  • Enumerated type declarations are usually placed in header files with function prototypes.
  • C internally associates integer values with enumerated values.
  • In the example, SUNDAY is 0, MONDAY is 1, and SATURDAY is 6.
  • A DayOfWeek variable may receive an invalid integer such as 1000 without compiler errors or warnings.
  • Only valid enumerated values should be assigned, and proper error checking should be performed.
  • Enumerated types avoid the need for magic numbers representing values.
  • Human-readable keywords reduce guesswork about values such as the days of the week.
  • If a value contains multiple words, underscores separate those words.
Feed/

How do enumerated types represent named values in C?

No cover

Channel

Computer science one

1 page

"19. Error Handling values it can take are SUNDAY, MONDAY, etc. and we can use these keywords in our program. For example,\n\nDayOfWeek today = MONDAY;\n\nif(today == SUNDAY || today == SATURDAY) {\n\nprintf(\"It is the weekend!\n\n\");\n\n}\n\nNote the modern naming conventions: the type identifier uses upper camel casing while the enumerated values follow an uppercase underscore convention. Though our example does not contain a value with multiple words, if it had, we would have used an underscore to separate them. Furthermore, enumerated type declarations are usually placed in separate header files along with function prototype declarations. Care must be taken when using enumerated types, however. Internally, C simply associates integers with the values. Thus, in our example, SUNDAY is actually 0, MONDAY is 1, and SATURDAY is 6. When we do assignments or equality comparisons, we’re actually just comparing integers. Consequently, a DayOfWeek variable may be assigned values that do not correspond to our enumeration. For example, DayOfWeek today = 1000; is valid code and will not (in general) result in any compiler errors or warnings, even though it is assigning an invalid value to the variable. You should only assign valid values to an enumerated type variable. Proper error checking should also be done. Despite this limitation, using enumerated types in C provides an obvious advantage. Without an enumerated type we’d be forced to use a collection of magic numbers to indicate values. Even for something as simple as the days of the week we’d be constantly trying to remember: which day is Wednesday again? I forget, does our week start with Monday or Sunday? Etc. By using an enumerated type these questions are mostly moot as we can use the more human-readable keywords and eliminate the guess work."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.