++ i和i ++之间的区别 [英] difference between ++i and i++
本文介绍了++ i和i ++之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复项:
什么是更高效的i ++或++ i?
< br>
i ++和++ i在循环中的区别?
您好我正在尝试这两个程序
void fun(){
int k = 0;
int i = 10;
k =(i ++)+(++ i);
cout<< k<< endl;
}
输出= 22,因为i ++会给出10和++ i将计算为12
但
void fun(){
int k = 0;
int i = 10;
k =(++ i)+(++ i);
cout<< k<< endl;
}
输出= 24
解决方案
注意:您正在调用未定义的行为(在序列点之间修改变量两次)
Possible Duplicates:
What is more efficient i++ or ++i?
How do we explain the result of the expression (++x)+(++x)+(++x)?
Difference between i++ and ++i in a loop?
Hi I am trying these two programs
void fun(){
int k=0;
int i=10;
k = (i++)+(++i);
cout<<k<<endl;
}
Output = 22 as i++ will give 10 and ++i will evaluate into 12
but
void fun(){
int k=0;
int i=10;
k = (++i)+(++i);
cout<<k<<endl;
}
Output = 24
It should be 23 I guess or there is sth that I am not able to see
Thanks in advance
解决方案
Note: you are invoking undefined behavior (modifying a variable twice between sequence points)
这篇关于++ i和i ++之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文