任何人都可以解释这段代码的输出吗? [英] Can any one explain the output of this code?

查看:75
本文介绍了任何人都可以解释这段代码的输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main ()
    {
        int x, y,z;
        x = 1;
        y = ++x * ++x;
        cout << "x="<<x <<" y= "<< y;
        z = ++x * ++x  * ++x;
        cout <<"\nx="<< x<<" z= " << z;
        return 0;
    }







输出:

x = 3 y = 9

x = 6 z = 150




Output:
x=3 y=9
x=6 z=150

推荐答案

这样的例子展示了 C / C ++中最怪异的一个角落。我甚至不建议初学者看看它。但是,如果您感兴趣,可以在此处找到相当完整的信息:未定义的行为和序列点堆栈溢出 [ ^ ]。
Such an example shows one of the weirdest corners of C/C++. I wouldn't even suggest a beginner to have a look at it. However, if you are interested you might find pretty complete info here: "Undefined behavior and sequence points" at Stack Overflow[^].


这是一个了解C语言有趣功能的问题,为新手提供易于理解的解释非常复杂。



比解释好,看它运行!



使用调试器,看看它是做什么的,一步一步。



对于此代码,我建议在汇编级别运行调试器,以便将代码行分割为其组件。执行时要非常注意变量。



整个程序是关于玩狡猾的++
It is a matter of knowing C language funny functionalities, it is very complicated to come with an explanation easy to comprehend for newbies.

Better than an explanation, see it running !

Use the debugger and see what it does, step by step.

For this code, I recommend to run the debugger at assembly level in order to split lines of code to their components. Be very attentive to the variables while executing.

The whole program is about playing with the tricky ++


这篇关于任何人都可以解释这段代码的输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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