in Programming recategorized by
308 views
3 votes
3 votes

Consider the following C program given below.

#include<stdio.h>
main() {
    int a = 4;
    switch (a) {
    a--;
    case 4:
        printf("Science ");
        break;
    default:
        printf("Technology ");
    case 3:
        printf("Knowledge ");
    case 2:
        printf("Philosophy");
    }
}

What will be the output of the program?

  1. Science
  2. Knowledge Philosophy
  3. Technology Knowledge Philosophy
  4. Science Knowledge
in Programming recategorized by
308 views

1 comment

Within $switch$ block and outside any cases, if some statements are present then they’ll be compiled (and checked for compiler errors) but they’ll not be executed.
4
4

1 Answer

0 votes
0 votes
The statements outside “case” but inside “switch” are never executed. Code may get compiled but those statements would still be unreachable and never execute.

We can think of switch as “goto” statements and jumping directly to those cases. Anything between is ignored.

Hence, a remains 4 and case 4 is executed.
Answer:

Related questions