什么是i ++和++ i运算符,它们是如何工作的 [英] What is i++ and ++i operator, how do they work

查看:92
本文介绍了什么是i ++和++ i运算符,它们是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< bits / stdc ++。h>

使用命名空间std;



int main()

{

int i = 5;



int ans =(i ++)*(++ i);



cout<< ans<<结束;



返回0;

}



此代码提供输出35我无法理解那里发生的事情



我尝试过的事情:



我无法尝试这个,我无法找到解决方案

#include<bits/stdc++.h>
using namespace std;

int main()
{
int i = 5;

int ans = (i++) *(++i);

cout << ans << endl;

return 0;
}

This code gives output 35 and i am not able to understand what is happening there

What I have tried:

nothing i can try in this, i cant find the solution for this

推荐答案

基本上,不要这样做!

见这里:为什么x = ++ x + x ++给出了错误的答案? [ ^ ]
Basically, don't do that!
See here: Why does x = ++x + x++ give me the wrong answer?[^]


计划结果:



int ans = i *((i + 1)+1);



第一个i是5; i的第二个参考在评估*之前得到i ++然后是++ i(7)。



订单很重要;否则你得到36。
Works out to:

int ans = i * ((i+1)+1);

The first "i" is 5; the "second reference" to "i" gets i++ and then ++i (7) before "*" is evaluated.

Order matters; otherwise you get 36.


这篇关于什么是i ++和++ i运算符,它们是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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