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

隐式的else编译器优化

发布时间:2020-12-16 07:20:40 所属栏目:百科 来源:网络整理
导读:我想知道编译器是否有办法理解两个if语句不能同时为true,并添加“隐式else”.例如,在此代码示例中: int main() { char c; scanf_s("%c",c,1); if (c == '1') { printf("received 1n"); } if (c == '2') { printf("received 2n"); } return 0;} c不能是’1
我想知道编译器是否有办法理解两个if语句不能同时为true,并添加“隐式else”.例如,在此代码示例中:

int main() {
    char c;
    scanf_s("%c",&c,1);
    if (c == '1') {
        printf("received 1n");
    }

    if (c == '2') {
        printf("received 2n");
    }

    return 0;
}

c不能是’1’和’2′,但是在Visual Studio中进行编译和反汇编后,我注意到它会检查第二个是否,无论如何.

解决方法

I want to know if there is way for the compiler to understand that two if statements can’t be true at the same time,and to add an “implicit else”.

是的:英特尔C编译器icc 17可以用Matt Godbolt’s Compiler Explorer验证,但clang和gcc似乎都没有执行此优化.

(编辑:李大同)

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

    推荐文章
      热点阅读