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

切换不在C中工作

发布时间:2020-12-16 10:24:16 所属栏目:百科 来源:网络整理
导读:我今天遇到了一个非常奇怪的问题.让concider得到以下代码: int llex(){ cout"enter 1"endl; char32_t c = U'('; cout(c==U'#')endl; switch(c){ case U'#': cout"enter 2"endl; return 5; default: break; }}int main( int argc,char** argv){ cout"enter 1
我今天遇到了一个非常奇怪的问题.让concider得到以下代码:

int llex(){
    cout<<"enter 1"<<endl;
    char32_t c = U'(';
    cout<<(c==U'#')<<endl;
    switch(c){
    case U'#':
        cout<<"enter 2"<<endl;
        return 5;
    default:
        break;
    }
}

int main( int argc,char** argv)
{
    cout<<"enter 1"<<endl;
    char32_t c = U'(';
    cout<<(c==U'#')<<endl;
    switch(c){
    case U'#':
        cout<<"enter 2"<<endl;
        return 5;
    default:
        break;
    }

    cout << "------------" << endl;
    llex();
}

输出是:

enter 1
0
------------
enter 1
0
enter 2

请注意,main中的代码对于llex函数中的代码是IDENTICAL.他们为什么输出不同的结果(我在clang上使用C 11).

解决方法

你的llex()函数应该总是返回一个值,但它不会.如果控制流未达到return语句,则这是未定义的行为.根据C 11标准第6.6.3 / 2段:

Flowing off the end of a function is equivalent to a return with no value; this results in undefined
behavior in a value-returning function
.

除非你解决这个问题,否则你不能对你的程序做任何假设,也不能对它有所期望.

例如,我无法重现this fixed live example中的行为.

(编辑:李大同)

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

    推荐文章
      热点阅读