Variable definition inside switch statement

前端 未结 5 2131
青春惊慌失措
青春惊慌失措 2020-11-28 15:37

In the following code, why is the variable i not assigned the value 1?

#include       

int main(void)
{   
    int          


        
5条回答
  •  孤城傲影
    2020-11-28 16:31

    In the case when val is not zero, the execution jumps directly to the label default. This means that the variable i, while defined in the block, isn't initialized and its value is indeterminate.

    6.8.2.4 The switch statement

    1. A switch statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a default label and the values of any case labels on or in the switch body. A case or default label is accessible only within the closest enclosing switch statement.

提交回复
热议问题