Declared variable within a switch block

This question already has an answer here:

  • Variable definition inside switch statement 5 answers
  • Variable Definition Ignore in C [duplicate] 7 answers
  • Why can't variables be declared in a switch statement? 23 answers

  • In this case , it is not undefined behaviour.

    For case 0 (when expr == 0 , which is your case), i gets assigned a value before being used (value being read from).


    OK, to elaborate a bit more, for the snippet

    switch (expr)
        {
                int i;
    
                case 0:
                        i = 17;
                default:
                        printf("%dn", i);
        }
    

    just makes the variable i defined in the block scope. Even if, the code would have been written as

                int i = 0; //or any value
    

    the value of i not initialized, it's just the identifier is visible in the scope. You must have another statement assigning value to i before you can make use of it.

    In this regard, the C11 standard has a very enlightening example and description. let me quote it, from chapter §6.8.4.2/P7

    EXAMPLE In the artificial program fragment

    switch (expr)
    {
    int i = 4;
    f(i);
    case 0:
    i = 17;
    /* falls through into default code */
    default:
    printf("%dn", i);
    }
    

    the object whose identifier is ``i exists with automatic storage duration (within the block) but is never initialized, and thus if the controlling expression has a nonzero value, the call to the printf function will access an indeterminate value. [....]

    链接地址: http://www.djcxy.com/p/14800.html

    上一篇: 在C ++中的每个案例步骤中使用自动中断来切换语句

    下一篇: 在开关块内声明的变量