X
Xavier Roche
Hi folks!
I stumbled upon an interesting case (!) of case/switch code suggesting
that case/switch are somehow equivalent to goto/<labels> in C ; ie. you
may interleave loops of conditions between case and switch, for example.
Here's an example (I would not recommend the coding style) of what I saw
; the code compiles without a single warning with gcc or clang. Is the
above (horrible) code really valid as per C standard ?
#include <stdio.h>
#include <stdlib.h>
static void foo(int c) {
switch(c) {
while(--c) {
default:
printf("%d\n", c);
continue;
case 0:
printf("function called with zero\n");
break;
case 1:
printf("function called with one\n");
}
break;
case 42:
printf("function called with 42\n");
break;
}
}
int main(int argc, const char **argv) {
int i;
for(i = 1 ; i < argc ; i++) {
const int value = atoi(argv);
foo(value);
}
return EXIT_SUCCESS;
}
$ ./case 0 1 2 42
function called with zero
function called with one
2
1
function called with 42
I stumbled upon an interesting case (!) of case/switch code suggesting
that case/switch are somehow equivalent to goto/<labels> in C ; ie. you
may interleave loops of conditions between case and switch, for example.
Here's an example (I would not recommend the coding style) of what I saw
; the code compiles without a single warning with gcc or clang. Is the
above (horrible) code really valid as per C standard ?
#include <stdio.h>
#include <stdlib.h>
static void foo(int c) {
switch(c) {
while(--c) {
default:
printf("%d\n", c);
continue;
case 0:
printf("function called with zero\n");
break;
case 1:
printf("function called with one\n");
}
break;
case 42:
printf("function called with 42\n");
break;
}
}
int main(int argc, const char **argv) {
int i;
for(i = 1 ; i < argc ; i++) {
const int value = atoi(argv);
foo(value);
}
return EXIT_SUCCESS;
}
$ ./case 0 1 2 42
function called with zero
function called with one
2
1
function called with 42