The switch statement has the following structure:
switch ( expression ){ // declarations case constant-expression : ... case constant-expression : ... default : ... }
The declarations section is used at compile time to declare variables, but it is not used at run time to initialize them (in fact, no instructions are executed in this section). Not the difference between declaring and initializing a variable. Since b
never initialized, your code has the same result as:
int main(){ int b; printf("b is %d\n", b); return 0; }
This is clearly undefined. Compiling with the -Wall
flag will catch that you are using an uninitialized value.
source share