How to Print Enum Values in C
You can use the "printf" function in C, a programming language, to print the value of enumeration variable that you've created. The "printf" function prints to the standard output stream, which usually means displaying it to the screen. Enumeration variables belong to the "enum" type and have integer values. The "printf" function doesn't have a flag for enum type, but you can cast your variable to an integer type and then print it.
Instructions
-
-
1
Open the application you use to code C with and open a C source file to edit.
-
2
Include the "stdio.h" header at the top of your source file so that you can use the "printf" function. For example, "#include <stdio.h>".
-
-
3
Declare an enumeration set using the "enum" identifier. Each variable listed is assigned an integer value, starting at 0 and increasing by 1 for each subsequent variable. For example, "enum NUMBERS { zero, one, two, three };" sets "zero" equal to 0, "one" equal to 1 and so on.
-
4
Create a variable of the enumerated type and assign it a value using the form "enum enumeration_type variable_name = value;". For example, "enum NUMBERS n = one;" creates a variable called "n" and assigns it the value of "one", which corresponds to the integer value of 1.
-
5
Use the "printf" function to print your enumeration variable. Cast your enumeration variable to an integer so that you can print it with the "d" integer flag. For example, "printf( "%d", (int)n);".
-
6
Save your C source file.
-
1