Bifalgorithm post · Computer science one · Enumerated Types

How do you declare an enumerated type in C?

How do you declare an enumerated type in C? (Computer science one · Enumerated Types) — "C allows you to define enumerated types which allow you to define a fi…

Ideas in this flowchart

  • C enumerated types define named sets of possible values.
  • An enumerated type defines a fixed list of possible values.
  • Days of the week and months of the year are typical values used in dates.
  • These values are more or less fixed and are not expected to change frequently.
  • The values use human-readable keywords, much like a #define macro.
  • An enumerated type is declared with the keywords typedef enum.
  • A comma-delimited list of keywords is placed inside a code block.
  • The type identifier is written after the code block.
  • The identifier names the type and can later be used to declare variables.
  • The example declares DayOfWeek with the values SUNDAY through SATURDAY.
Feed/

How do you declare an enumerated type in C?

No cover

Channel

Computer science one

1 page

"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"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.