In C, enum constants are not modifiable lvalues. For the enumeration below with explicit values, what will happen if we attempt to pre-increment MON inside printf?
#include
int main()
{
enum days { MON = -1, TUE, WED = 6, THU, FRI, SAT };
printf("%d, %d, %d, %d, %d, %d
", ++MON, TUE, WED, THU, FRI, SAT);
return 0;
}
-
A-1, 0, 1, 2, 3, 4
-
BError
-
C0, 1, 6, 3, 4, 5
-
D0, 0, 6, 7, 8, 9
Answer
Correct Answer: Error
Explanation
Introduction / Context: This question checks whether you recognize that enum identifiers represent constant values, not variables. Applying increment operators to constants is illegal in C.
Given Data / Assumptions:
- enum days has MON = -1, TUE = 0, WED = 6, THU = 7, FRI = 8, SAT = 9 by standard enumeration rules.
- Expression used: ++MON in printf.
Concept / Approach: Increment operators require a modifiable lvalue. Enum constants are compile-time integral constants, not objects with storage that can be incremented. Therefore, ++MON is invalid and causes a compilation error.
Step-by-Step Solution:
Check operand category of MON -> constant expression, not lvalue Apply ++ to a non-modifiable operand -> constraint violation Compiler issues error; program does not compileVerification / Alternative check: Replacing ++MON with MON would compile and print the values -1, 0, 6, 7, 8, 9. The error arises solely from trying to modify a constant identifier.
Why Other Options Are Wrong:
- Any numeric tuple assumes successful compilation and execution, which does not occur.
Common Pitfalls: Treating enum names like variables or expecting them to behave like ordinary ints that can be incremented in-place.
Final Answer: Error