加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C中的goto语句

发布时间:2020-12-16 10:09:25 所属栏目:百科 来源:网络整理
导读:#includestdio.hint main() { int i = 10; printf("0 i %d %pn",i,i); if (i == 10) goto f; { int i = 20; printf("1 i %dn",i); } { int i = 30; f: printf("2 i %d %pn",i); //statement X } return 0;} 输出: [test]$./a.out 0 i 10 0xbfbeaea82 i 1
#include<stdio.h>

int main() 
{
    int i  = 10;

    printf("0 i %d %pn",i,&i);
    if (i == 10)
        goto f;

    {
        int i = 20;
        printf("1 i %dn",i);
    }
    {
        int i = 30;
        f:
        printf("2 i %d %pn",&i); //statement X
    }

    return 0;
}

输出:

[test]$./a.out 

0 i 10 0xbfbeaea8

2 i 134513744 0xbfbeaea4

我很难理解语句X如何工作?当你看到输出它是垃圾.应该说我没有声明?

解决方法

那是因为goto跳过阴影变量我的初始化.

这是C和C之间差异的细微差别之一. In strict C++ go to crossing variable initialization is an error,而在C,它不是. GCC也证实了这一点,当你使用-std = c11进行编译时,它允许使用std = c 11它会抱怨:跳转到标签’f’越过’int i’的初始化.

来自C99:

A goto statement shall not jump from outside the scope of an identifier having a variably modified type to inside the scope of that identifier.

VLA是可变修饰类型.允许在不包含VM类型的范围内跳转.

从C 11(强调我的):

A program that jumps from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type,class type with a trivial default constructor and a trivial destructor,a cv-qualified version of one of these types,or an array of one of the preceding types and is declared without an initializer.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读