When 1 is entered, The output of the code below is?
#include <stdio.h>
void main()
{
int ch;
printf("enter a value btw 1 to 2:");
scanf("%d", &ch);
switch (ch, ch + 1)
{
case 1:
printf("1\n");
break;
case 2:
printf("2");
break;
}
}
Answer: B
Correct answer: 2
The controlling expression uses the comma operator: ch, ch + 1 evaluates ch, discards it, and yields ch + 1. With 1 entered, the switch is therefore performed on 2, so case 2 runs and prints 2.
Please log in or register to report this question.