0

My question concerns how compilers are permitted to implement switch statements in C and C++.

If the case is missing for the variable but no default is provided with the program attempt to "jump" to an odd calculated offset or is this trapped in someway to prevent potential problems with control flow?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
C. Cheng
  • 80
  • 9

1 Answers1

5

As per the C11 standard, chapter §6.8.4.2, The switch statement(emphasis mine)

[...] If a converted value matches that of the promoted controlling expression, control jumps to the statement following the matched case label. Otherwise, if there is a default label, control jumps to the labeled statement. If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

So basically, control goes past the entire switch body.

For C++, the logic is same. From C++11, §6.4.2, Thanks to @Jack

If no case matches and if there is no default then none of the statements in the switch is executed.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • As per C++11 standard, things goes the same way at §6.4.2: _If no case matches and if there is no default then none of the statements in the switch is executed._ – Jack Apr 28 '16 at 17:43
  • @Jack Thanks, let me add that to my answer itself. :) – Sourav Ghosh Apr 28 '16 at 17:44