Through a little typo, I accidentally found this construct:
int main(void) {
char foo = 'c';
switch(foo)
{
printf("Cant Touch This\n"); // This line is Unreachable
case 'a': printf("A\n"); break;
case 'b': printf("B\n"); break;
case 'c': printf("C\n"); break;
case 'd': printf("D\n"); break;
}
return 0;
}
It seems that the printf
at the top of the switch
statement is valid, but also completely unreachable.
I got a clean compile, without even a warning about unreachable code, but this seems pointless.
Should a compiler flag this as unreachable code?
Does this serve any purpose at all?
Not only for variable declaration but advanced jumping as well. You can utilize it well if and only if you're not prone to spaghetti code.
Prints
It should be noted that switch-case is one of the fastest control flow clauses. So it must be very flexible to the programmer, which sometimes involves cases like this.
Yes. If instead of a statement, you put a declaration before the first label, this can make perfect sense:
The rules for declarations and statements are shared for blocks in general, so it's the same rule that allows that that also allows statements there.
Worth mentioning as well is also that if the first statement is a loop construct, case labels may appear in the loop body:
Please don't write code like this if there is a more readable way of writing it, but it's perfectly valid, and the
f()
call is reachable.Perhaps not the most useful, but not completely worthless. You may use it to declare a local variable available within
switch
scope.The standard (
N1579 6.8.4.2/7
) has the following sample:P.S. BTW, the sample is not valid C++ code. In that case (
N4140 6.7/3
, emphasis mine):So replacing
int i = 4;
withint i;
makes it a valid C++.It should be noted, that there are virtually no structural restrictions on the code within the
switch
statement, or on where thecase *:
labels are placed within this code*. This makes programming tricks like duff's device possible, one possible implementation of which looks like this:You see, the code between the
switch(n%8) {
and thecase 7:
label is definitely reachable...* As supercat thankfully pointed out in a comment: Since C99, neither a
goto
nor a label (be it acase *:
label or not) may appear within the scope of a declaration that contains a VLA declaration. So it's not correct to say that there are no structural restrictions on the placement of thecase *:
labels. However, duff's device predates the C99 standard, and it does not depend on VLA's anyway. Nevertheless, I felt compelled to insert a "virtually" into my first sentence due to this.There is a famous use of this called Duff's Device.
Here we copy a buffer pointed to by
from
to a buffer pointed to byto
. We copycount
instances of data.The
do{}while()
statement starts before the firstcase
label, and thecase
labels are embedded within thedo{}while()
.This reduces the number of conditional branches at the end of the
do{}while()
loop encountered by roughly a factor of 4 (in this example; the constant can be tweaked to whatever value you want).Now, optimizers can sometimes do this for you (especially if they are optimizing streaming/vectorized instructions), but without profile guided optimization they cannot know if you expect the loop to be large or not.
In general, variable declarations can occur there and be used in every case, but be out of scope after the switch ends. (note any initialization will be skipped)
In addition, control flow that isn't switch-specific can get you into that section of the switch block, as illustrated above, or with a
goto
.You got your answer related to the required
gcc
option-Wswitch-unreachable
to generate the warning, this answer is to elaborate on the usability / worthyness part.Quoting straight out of
C11
, chapter §6.8.4.2, (emphasis mine)Which is very self-explanatory. You can use this to define a locally scoped variable which is available only within the
switch
statement scope.