1,677 views
1 votes
1 votes
#include

int main()

{ char check = 'a';

switch (check)

{

case 'a' || 1: printf("Gates ");

case 'b' || 2: printf("Quiz ");

break;

default: printf("GatesQuiz");

} return 0; }

(a) Gates

(b) Gates Quiz

(c) Gates Quiz GatesQuiz

(d) Compile-time error

2 Answers

1 votes
1 votes
i think  ryt option is "D" bcoz 'a'||1 will evaluate to 1 and 'b'||2 will also evaluate to 1 so both the case will become same .

and in swicth case every case must be unique so it will generate compile time error.
0 votes
0 votes

The integral expressions used in labels must be a constant expressions.

The cases are labeled with characters which gets converted to their ascii values 97(for a) and 98(for b).

Now 97 OR with 1 will give 1 as TRUE OR TRUE is TRUE. Similar for 2nd case.

Two case labels cannot have same value therefore gives compile time error.

Related questions

5 votes
5 votes
1 answer
1
Tuhin Dutta asked Nov 14, 2017
1,453 views
#include <stdio.h>int main(){ int check = 20; const int arr[] = {10, 20, 30}; switch (check) { case arr[0]: printf("Tuhin "); case arr : printf...
1 votes
1 votes
1 answer
2
iita asked Dec 17, 2016
596 views
1 votes
1 votes
2 answers
4
Hira Thakur asked Sep 14, 2018
1,483 views
int main() { int a =50; switch(a) { default: a=45; case 49: a++; case 50: a ; case 51: a =a+1; } printf("%d",a); }my doubt is the default case is not executed here why??,...