A friend of mine has just joked that this would be possible in C, to which I responded: impossible. It turned out it is possible and compiles without ANY warnings.
#include "stdio.h"
int main(){
int x = 1;
switch(x) {
case 1:
printf("1\n");
while(1) {
case 2:
printf("bad 2\n");
}
break;
default:
printf("default\n");
break;
}
}
Setting x
to 1 or 2 results in an infinite loop printing bad 2
all the time.
Why? What is going on, how is this even legal???
I am using GCC 4.8.2, Windows, -std=c99
...
Edit: This question is about why is it legal, if it is legal, and since when. Examples are nice, but not the goal of my question. I'd like to know, if this whole placing of case
labels anywhere is a conscious C design, or an accident, or a leaky abstraction / implementation detail leak ?