Declarations in the switch statement

Consider this C code, where foo- int:

switch(foo){
    case 1: {
        int bla = 255;
        case 2:
            printf("case12 %d\n", bla);
        break;
        case 3:
            printf("case3  %d\n", bla);
    }
};

For different values, the foocode gives the following result:

case12 255   # foo=1
case12 255   # foo=2
case3  0     # foo=3

I have a problem with understanding foo=3. A line declaring blaand defining its value should not be executed when foo=3. The switch statement should go straight to the label for case 3:. However, there is no warning, therefore bla, apparently, at least it has been announced. It can be used uninitialized, and its value, as a rule, turns out to be 0. Can you explain what happens in case 3 and why is it a legitimate C code?

+4
source share
1

A switch goto. case ( ) , switch, .

int bla = 255; int bla, - .

switch case 2: case 3:, bla, . bla - ( , 0), undefined.

goto

.

( gcc , -Wall -Wextra -Wmaybe-uninitialized.)

( switch . Duff Device.)

+7

Source: https://habr.com/ru/post/1650431/


All Articles