How to Use Enum in C Program
Using an enumeration type in C is an easy way to create a set of named tags that represent integers. C is a popular programming language, frequently used to develop application software. A common example of enumeration is the Boolean data type, which is enumerated with "true" having an integer value of 1 and "false" having a value of 0. Enumerations and integers data types can be mixed and matched throughout your program.
Instructions
-
-
1
Open the application you use to code C with and load your program's source file.
-
2
Type "enum," the name you want to give your enum type and a "{". Type an ordered list of the names you want to include in the enum type. Place a comma after each name, except for the last one. Type "{;" after the last name in the enum type list. The names will receive integer values starting from 0 and increasing by 1 each line. You can change the default values by using the "=" sign, like "my_name = 5,". If you use "=" on a line, subsequent names will have integer values increasing by 1 from that line's value. An example:
enum my_type {
zero,
two = 2,
three
};
Zero has a value of 0, two has a value of 2 and three has a value of 3.
-
-
3
Create a variable of your enum type with the format "enum enum_type variable_name;". An example: "enum my_type my_variable = three;" You can use the enum type names throughout your program as you would an integer. For example, "int i = three;".
-
1