c - Switch statement code without a label -
this question has answer here:
- can put code outside of cases in switch? 5 answers
how come ansi c allows extraneous code before case labels within switch statement?
#include <stdio.h> int main(void) { const int foo = 1; switch (foo) { printf("wut\n"); /* no label, doesn't output */ case 1: printf("1\n"); break; default: printf("other\n"); break; } return 0; } compiled with
$ gcc -pedantic -wall -werror -wextra -ansi test.c it compiles without warnings , executes fine - sans "wut".
it allowed put statements in switch without label. standard says that:
c11: 6.8.4.2 switch statement (p7):
in artificial program fragment
switch (expr) { int = 4; f(i); case 0: = 17; /* falls through default code */ default: printf("%d\n", i); }the object identifier
iexists automatic storage duration (within block) never initialized, , if controlling expression has nonzero value, callprintffunction access indeterminate value. similarly, the call functionfcannot reached.
Comments
Post a Comment