开发者

undefined behavior of switch [closed]

开发者 https://www.devze.com 2023-01-29 15:21 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_运维百科 Closed 12 years ago.

Given the following code:

#include<stdio.h>
int main()
{
    int a=1;
    switch(a)
    {   int b=20;
        case 1: 
            printf("b is %d\n",b);
            break;
        default:
            printf("b is %d\n",b);
            break;
    }
    return 0;
}

What do you think can be value of b in both of these printf statements? Of course, it is not 20.


In C++ the code is ill-formed. Case-label 1 crosses initialization of b.
In C99 it is valid but invokes Undefined Behaviour.

0

精彩评论

暂无评论...
验证码 换一张
取 消