C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 对于三元操作,“警告:操作……可能未定义” – 如果/ else阻止大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Undefined behavior and sequence points                                    5个
这是我的代码

int main() {
    static int test = 0;
    const int anotherInt = 1;
    test = anotherInt > test ? test++ : 0;
    if (anotherInt > test)
        test++;
    else
        test = 0;
    return 0;
}

这是我构建它时产生的警告:

../main.cpp:15:40: warning: operation on ‘test’ may be undefined [-Wsequence-point]
  test=     anotherInt>test ? test++ : 0;
                                        ^

为什么C会对三元操作发出警告,而不是常规if..else语句?

解决方法

他们并不等同.请注意,在三元运算符表达式中,您将结果分配给test

将if条件更改为:

if(anotherInt > test)
    test = test++;  // undefined!

您可能也会在此处看到相同的警告.

大佬总结

以上是大佬教程为你收集整理的c – 对于三元操作,“警告:操作……可能未定义” – 如果/ else阻止全部内容,希望文章能够帮你解决c – 对于三元操作,“警告:操作……可能未定义” – 如果/ else阻止所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。