Twisted C ++代码 [英] Twisted C++ code

查看:112
本文介绍了Twisted C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:
未定义的行为和序列点






 #include< ; iostream.h> 
int main()
{
int i = 7,j = i;
j =(i ++,++ i,j ++ * i);
cout<< j;
return 0;
}

C ++代码的输出是什么?



这是我的教授给我的家庭作业。

解决方案

不要相信这是未定义的实际编译程序与几个编译器,并观察结果:



修复 iostream.h

  • Clang ++ 2.8 prints 63 / li>
  • Sun C ++ 5.8第63张照片

  • MSVC 2010第64张照片



  • (噢,重写为使用CI / O,Unix 7上的原始K& RC编译器打印63)


    Possible Duplicate: Undefined behavior and sequence points

     #include< iostream.h>
     int main()
     {
           int i=7,j=i;
           j=(i++,++i,j++*i);
           cout <<j;
           return 0;
     }
    

    What will be the output of the C++ code?

    It's my homework that my professor gave me.

    解决方案

    It sometimes helps to convince people who don't believe this is undefined by actually compiling the program with several compilers and observing the results:

    After fixing the iostream.h error,

    • g++ 4.5.2 prints 64
    • CLang++ 2.8 prints 63
    • Sun C++ 5.8 prints 63
    • MSVC 2010 prints 64

    (oh, and, re-written to use C I/O, the original K&R C compiler on Unix 7 prints 63)

    这篇关于Twisted C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆