in Programming in C recategorized by
632 views
4 votes
4 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 in C recategorized by
632 views

2 Comments

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.
8
8

Reason : In C, statements within the curly braces {} of a switch block but outside any case labels will be compiled and checked for compiler errors, but they won't be executed during runtime.

0
0

1 Answer

2 votes
2 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