++ ,--以及printf里的一些问题

#include <stdio.h>
int main(){
int i=3,j=3,k=0,m=3,n=0,l=3,a=5,b=5;
k=(j++)*(++j)+(j--)*(--j);
n=(l++)+(l++)+(l++);
printf("k=%d (i++)*(++i)+(i--)*(--i)=%d (m++)+(m++)+(m++)=%d n=%d\n",k,(i++)*(++i)+(i--)*(--i),(m++)+(m++)+(m++),n);
a=(a--)+3;
printf("a=%d b=%d (b=(b--)+3)=%d b=%d\n",a,b,b=(b--)+3,b);
printf("i=%d j=%d l=%d",i,j,l);
}
为什么k跟printf里的(i++)*(++i)+(i--)*(--i)值不同
为什么a=(a--)+3结果为7
以及为什么printf("a=%d b=%d (b=(b--)+3)=%d b=%d\n",a,b,b=(b--)+3,b);最后结果为7,8,8,5
谢谢了
[557 byte] By [llaajjjj-卫子衿] at [2008-1-9]
# 1
这跟编译有关系的,不同的编译器结果可能不同,比如我在VC2005下的结果如下:

k=18 (i++)*(++i)+(i--)*(--i)=18 (m++)+(m++)+(m++)=9 n=9
a=7 b=7 (b=(b--)+3)=8 b=7
i=3 j=3 l=6
mochen5460-mochen at 2007-10-19 > top of Msdn China Tech,C/C++,C语言...
# 2
具体结果如何 就看编译器了
代码写成这样 奈何...
# 3
a=(a--)+3;
这一句相当于:
a=a+3;//a=8
a--;//a=7
mochen5460-mochen at 2007-10-19 > top of Msdn China Tech,C/C++,C语言...
# 4
非常感谢 mochen5460(mochen)
不知能否 详细点讲解下
a=(a--)+3;
llaajjjj-卫子衿 at 2007-10-19 > top of Msdn China Tech,C/C++,C语言...
# 5
又见++贴.你可搜一下,在C版中就有N多
http://community.Codefund.cn/Expert/topic/5161/5161529.xml?temp=.2859613
http://community.Codefund.cn/Expert/topic/5090/5090550.xml?temp=.1131098
http://community.Codefund.cn/Expert/topic/5090/5090550.xml?temp=.1131098
http://community.Codefund.cn/Expert/topic/5050/5050739.xml?temp=.5225641
http://community.Codefund.cn/Expert/topic/5051/5051593.xml?temp=.6827509
http://community.Codefund.cn/Expert/topic/5003/5003337.xml?temp=.9408075
http://community.Codefund.cn/Expert/topic/4983/4983657.xml?temp=2.363223E-02
http://community.Codefund.cn/Expert/topic/4967/4967105.xml?temp=.2996637
...

这个与编译器有关,讨论这种问题实在没多大意义
keiy at 2007-10-19 > top of Msdn China Tech,C/C++,C语言...
# 6
a=(a--)+3;
在这句话中,--是后缀式的自减操作符。所以是先执行该语句,然后自减的。
如果一行中有几个自减,则不同的编译器有不同的结果,如:
int a=5;
a=(a--)+(a--)+(a--);
在不同的编译器下会有不同的结果。
mochen5460-mochen at 2007-10-19 > top of Msdn China Tech,C/C++,C语言...
# 7
OK 结帖
非常感谢mochen5460(mochen)
llaajjjj-卫子衿 at 2007-10-19 > top of Msdn China Tech,C/C++,C语言...