Through a little typo, I accidentally found this construct:
int main(void) {
char foo = \'c\';
switch(foo)
{
printf(\"Cant Touch This\\n\");
There is a famous use of this called Duff's Device.
int n = (count+3)/4;
switch (count % 4) {
do {
case 0: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
Here we copy a buffer pointed to by from
to a buffer pointed to by to
. We copy count
instances of data.
The do{}while()
statement starts before the first case
label, and the case
labels are embedded within the do{}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
.