C ++编程增量递减 [英] C++ programming increment decrement

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

问题描述

#include <stdio.h>  

using namespace std;

int main()
{
  int x = 5, y = 5, z;
  x = ++x; y = --y;
  z = x + ++x;
  cout << z;
        return 0;
}





我的尝试:



我尝试了输出但是来了14它如何工作请通过stemp解释步骤



What I have tried:

I tried the output but coming 14 How it works please explain step by stemp

推荐答案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



此行位于灰色区域:无法知道结果,因为它取决于编译器内部。

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

This line is in the gray zone: it is not possible to know the result as it depend on the compiler internals.
z = x + ++x;


#include <stdio.h>  

using namespace std;
 
int main()
{
  // Declaring and initializing (e.g. assigning values) two variables x and y
  // Also declaring the variable z without initialization
  int x = 5, y = 5, z;
  // Using prefix increment and decrement operators to change the
  // value of x and y variables. Normally you don't need to perform
  // x = ++x; and y = --y, because the operation on 'x' and 'y' might 
  // not be undefined because of the operator precedence violation; 
  // Instead, just do as follows: ++x; --y;
  x = ++x; y = --y;
  // Here the value of x is incremented using prefix increment operator
  // but, again, we've got the precedence violation at this point.
  // Instead, we have to do as follows: z = x + (x + 1);
  z = x + ++x;
  cout << z;
        return 0;
}





这是执行所述计算的完整更正代码:





Here's the complete corrected code that performs the described computation:

#include <stdio.h>
#include <iostream>

using namespace std;
 
int main()
{
  int x = 5, y = 5, z;
  ++x; --y;
  z = x + (x + 1);
  cout << z;
        return 0;
}





结果是13.( http://codepad.org/n9uSsw2Z [ ^ ])



这是你真正需要的吗?只需发表您的评论。


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

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