Consider the following C code using an enum. What will be printed?
#include <stdio.h> enum Color { RED, GREEN, BLUE }; int main() { enum Color c = GREEN; printf("%d\n", c); return 0; }
By default, enum values start at 0 and increase by 1.
The enum Color assigns RED=0, GREEN=1, BLUE=2. So printing c when it is GREEN prints 1.
What value will be printed by this program?
#include <stdio.h> enum Status { OK = 5, WARNING, ERROR }; int main() { enum Status s = ERROR; printf("%d\n", s); return 0; }
Enum values increment from the last explicitly set value.
OK is set to 5, so WARNING is 6, and ERROR is 7. The variable s is ERROR, so it prints 7.
What is the output or error of this code?
#include <stdio.h> enum Level { LOW, MEDIUM, HIGH }; int main() { enum Level l = HIGH; printf("%c\n", l); return 0; }
Enum values are integers but printing with %c prints the character for that ASCII code.
The enum value HIGH is 2. Using %c prints the character with ASCII code 2, which is a non-printable control character, so it may appear as a strange symbol or nothing.
Given this enum, how many distinct named constants does it contain?
enum Days { MON=1, TUE=2, WED=3, THU=4, FRI=5, SAT=6, SUN=7 };
Count the number of names listed inside the enum.
There are seven named constants: MON, TUE, WED, THU, FRI, SAT, SUN.
What error will this code cause when compiled?
#include <stdio.h> enum Numbers { ONE, TWO, ONE }; int main() { printf("%d\n", ONE); return 0; }
Enum constants must have unique names.
The enum defines 'ONE' twice, which is not allowed and causes a compilation error for duplicate enum constant.