Which of the following errors would be reported by the compiler on compiling the program given below?
#include <stdio.h>
int main()
{
int a = 5;
switch(a)
{
case 1:
printf("First");
case 2:
printf("Second");
case 3 + 2:
printf("Third");
case 5:
printf("Final");
break;
}
return 0;
}
Answer: C
Correct answer: Duplicate case case 5:
case 3 + 2 is perfectly legal - a label may be any constant expression, and the compiler evaluates it to 5. That is exactly the problem: it then collides with case 5, and duplicate labels are an error, since the jump would be ambiguous. Missing break statements are legal (they produce fall-through).
Please log in or register to report this question.