In this program, printf("GATE-CSE"); statement is not part of any case. Compiler transfer control with Switch(0) to case 0 if found in switch block, otherwise check for default case in swich block.
Hence, printf("GATE-CSE"); statemt will not be executed by compiler. Therefore, compiler execute case 1 and print One. As there is not break statement after that compiler continue execute all statements till break statement or end of switch block. OUTPUT: One None
As there is no case 5 in switch block, so compiler execure default case So, it will print None As there is not break statement after that compiler continue execute all statements till break statement or end of switch block. OUTPUT: None One Two
*("practicepaper"+5) return the index-5 char (with 'p' as index-0), which is char 'i'. Compiler directly transfer control to case 'i'. NOTE: default is written above case'i', but compiler executes default case only when no case matches with switch.
Statement switch(a=0) evaluated by compiler as switch(0) (NOTE: a=0 transfer 0 into a and return 0) Hence, case 0 match in switch block. It will print Zero, As there is not break statement after that compiler continue execute all statements till break statement or end of switch block.
Statement switch(a=5) evaluated by compiler as switch(5) (NOTE: a=5 transfer 5 into a and return 5) Now, there is not a case 5 in switch block. NOTE: default is written at top in swich block but it will execute only when no case match in switch block. As there is no case 5 in switch block, so compiler execure default case So, it will print GATE-CSE As there is not break statement after that compiler continue execute all statements till break statement or end of switch block. OUTPUT: GATE-CSE Zero One
Statement switch(a=0) evaluated by compiler as switch(0) (NOTE: a=0 transfer 0 into a and return 0) Hence, case 0 match in switch block. It will print Zero and One as ther is not break statement in case 0.
NOTE: default is written at top in swich block but it will execute only when no case match in switch block.
What is the output of the following program? NOTE: Consider int size is of 4 Byte.
#include < stdio.h >
int main()
{
int A[6]={1,2,3,4};
switch(sizeof(A))
{
case 1:
case 2:
case 3:
case 4:printf("practice");
case 5:printf("paper ");
case 16:printf("Mock Test ");
}
printf("GATE-CSE");
return 0;
}
sizeof(A) return the size of array A. Array A is declared as int A[6], so sizeof(A) return 6*4=24 (as size of int given as 4). It will not match with any case in switch. Therefore, program print GATE-CSE OUTPUT: GATE-CSE
What is the output of the following program? NOTE: Consider int size is of 4 Byte.
#include < stdio.h >
int main()
{
int A[6]={1,2,3,4};
switch(sizeof(A[0]))
{
case 1:
case 2:
case 3:
case 4: printf("practice ");
case 5: printf("paper ");
case 16: printf("Mock Test ");
}
printf("GATE-CSE");
return 0;
}
sizeof(A[0]) return 4 (as size of int given as 4). It will match with case 4 in switch. Therefore, it first print practice After that compiler continue execute statements without any case mathing till it get the break statement or end of switch block. So, it also print paper Mock Test GATE-CSE OUTPUT: practice paper Mock test GATE-CSE