++运算符在前和在后我都懂,但下面这几个式子还是很费解,求解释#include <iostream.h>
int fun(int x, int y)
{
return x*y;
}
void main()
{
int k=5;
cout<<fun(++k,++k)<<endl; k=5;
cout<<fun(k++,k++)<<endl; k=5;
cout<<fun(++k,k++)<<endl;
k=5;
cout<<fun(k++,++k)<<endl; k=5;
cout<<(++k)*(++k)<<endl; k=5;
cout<<(k++)*(k++)<<endl; k=5;
cout<<(++k)*(k++)<<endl; k=5;
cout<<(k++)*(++k)<<endl;
}
答案依次是: 42 25 30 36 49 25 36 36
int fun(int x, int y)
{
return x*y;
}
void main()
{
int k=5;
cout<<fun(++k,++k)<<endl; k=5;
cout<<fun(k++,k++)<<endl; k=5;
cout<<fun(++k,k++)<<endl;
k=5;
cout<<fun(k++,++k)<<endl; k=5;
cout<<(++k)*(++k)<<endl; k=5;
cout<<(k++)*(k++)<<endl; k=5;
cout<<(++k)*(k++)<<endl; k=5;
cout<<(k++)*(++k)<<endl;
}
答案依次是: 42 25 30 36 49 25 36 36
cout<<fun(++k,++k)<<endl;k=5;
cout<<fun(k++,k++)<<endl;k=5;
cout<<fun(++k,k++)<<endl;
k=5;
cout<<fun(k++,++k)<<endl;上面几个我调试了一下发现结果是:49 30 35 42
比如这个:
k=5;
cout<<(++k)*(++k)<<endl;
算得49怎么搞的,我怎么想都是6*7=42的VC6编译下是按什么顺序算的?
比如这个:
k=5;
cout<<(++k)*(++k)<<endl;
算得49怎么搞的,我怎么想都是6*7=42的VC6编译下是按什么顺序算的?
//////////////////////////////////////////////////////////楼主需要搞清楚计算的先后顺序:
“()”的运行级别要高于“*”, 所以:
第一步(++k)执行后,k=6 ;
第二步(++k)执行后,k=7;
第三步才是 7*7 = 49;