请教c难题!
请看程序:
#include<stdio.h>
main()
{
int i=6;
printf("%d%d%d",i+=3,i-=3,i++);
}
运行结果是746,这是怎么回事?请高手指教,请详细点好吗?
问题点数:50、回复次数:27Top
1 楼hehe214(风之子)回复于 2002-09-22 23:04:27 得分 0
奇怪了,研究研究Top
2 楼sandrowjw(我的小猫照片给弄坏了,心都碎了)回复于 2002-09-22 23:05:17 得分 5
识别参数从右向左,和你的编译器有关。Top
3 楼sandrowjw(我的小猫照片给弄坏了,心都碎了)回复于 2002-09-22 23:05:56 得分 0
i++见效这么快啊?Top
4 楼windworm(HelloEverybody)回复于 2002-09-22 23:06:26 得分 0
跟编译器有关的Top
5 楼hehe214(风之子)回复于 2002-09-22 23:12:53 得分 0
你用的什么编译器啊Top
6 楼qhgary(Gary)回复于 2002-09-22 23:21:44 得分 25
是跟编译器有关,不过这个是从右往左来得。因为i=6,而i++是先用i得值然后用完后再加一。所以就是最右边得那个6,但此时i=7了(因为i用完加一了)
然后计算i-=3;很明显此时i=7-3=4;就是中间那个4,然后i+=3;所以i=4+3=7;
就是最左边那个7(记住这里是从右往左计算得)
累死了,该给点分了吧。我回答得该仔细吧Top
7 楼duxianghe( dux++ )回复于 2002-09-22 23:30:52 得分 10
这个结果是正确的:因为:
在C编译时,先编译后面的i++,然后是i-=3,最后才是i+=3.
最先的i++值为6,运行后值为7,再运行i-=3,得i=7-3=4,再运行
i+=3,得i=i+3=4=3=7.
相信你应该明白了。
Top
8 楼Orsino(Orsino)回复于 2002-09-22 23:42:53 得分 5
Hi birdflash,
The program doesn't conform to ANSI C standard,
and is written in a BAD style.
In "The C Programming Language", section 2.12
"Precedence and Order of Evaluation", it says,
"C, like most languages, does not specify the order in which the operands of an operator are evaluated. (The exceptions are &&, ||, ?:, and `,'.) For example, in a statement like
x = f() + g();
"f may be evaluated before g or vice versa; thus if either f or g alters a variable on which the other depends, x can depend on the order of evaluation. Intermediate results can be stored in temporary variables to ensure a particular sequence.
Similarly, the order in which function arguments are evaluated is not specified, so the statement
printf("%d %d\n", ++n, power(2, n)); /* WRONG */
"can produce different results with different compilers, depending on whether n is incremented before power is called. The solution, of course, is to write
++n;
printf("%d %d\n", n, power(2, n));
"Function calls, nested assignment statements, and increment and decrement operators cause ``side effects'' - some variable is changed as a by-product of the evaluation of an expression. In any expression involving side effects, there can be subtle dependencies on the order in which variables taking part in the expression are updated. One unhappy situation is typified by the statement
a[i] = i++;
"The question is whether the subscript is the old value of i or the new. Compilers can interpret this in different ways, and generate different answers depending on their interpretation. The standard intentionally leaves most such matters unspecified. When side effects (assignment to variables) take place within an expression is left to the discretion of the compiler, since the best order depends strongly on machine architecture. (The standard does specify that all side effects on arguments take effect before a function is called, but that would not help in the call to printf above.)
"The moral is that writing code that depends on order of evaluation is a bad programming practice in any language. Naturally, it is necessary to know what things to avoid, but if you don't know how they are done on various machines, you won't be tempted to take advantage of a particular implementation.
That's it. Just avoid writing such a program.
Enjoy!
OrsinoTop
9 楼kangqi_520(康琦)回复于 2002-09-22 23:47:05 得分 0
从右向左Top
10 楼kingvictor(小黑)回复于 2002-09-22 23:53:55 得分 0
和编译器有关的
你用的是什么编译器?
这个在基本 c的树立头都有提到!
================================================================
CSDN 论坛助手 Ver 1.0 B0402提供下载。 改进了很多,功能完备!
★ 浏览帖子速度极快![建议系统使用ie5.5以上]。 ★ 多种帖子实现界面。
★ 保存帖子到本地[html格式]★ 监视您关注帖子的回复更新。
★ 可以直接发贴、回复帖子★ 采用XML接口,可以一次性显示4页帖子,同时支持自定义每次显示帖子数量。可以浏览历史记录!
★ 支持在线检测程序升级情况,可及时获得程序更新的信息。
★★ 签名 ●
可以在您的每个帖子的后面自动加上一个自己设计的签名哟。
Http://www.ChinaOK.net/csdn/csdn.zip
Http://www.ChinaOK.net/csdn/csdn.rar
Http://www.ChinaOK.net/csdn/csdn.exe [自解压]
Top
11 楼tttc(Azure)回复于 2002-09-23 00:37:33 得分 5
其实是这样的,c遇到参数问题,是从左向右压栈的,因此出栈后顺序就反过来了,i是6,计算i++,因为是后加,所以表达式的值是6,i变为7,再执行i-=3,表达式值与i的值都变为4,再执行i+=3,又都变为7了,然后相对应的打印出来,就得到这结果了!Top
12 楼waterstony(王小石)回复于 2002-09-23 00:46:06 得分 0
只要记住两点,参数从右往左入栈,p++先执行后增一。
Top
13 楼WaterWalker(亢龍有悔)回复于 2002-09-23 02:33:01 得分 0
这是什么书,要粉书坑儒!
刚开始学语言就学一些基础的东西stack,queue,tree。去学这些细节。几次能用到。以后学多了这些也就自然明白了。Top
14 楼cwanter(亚玛逊河上的渔夫)回复于 2002-09-23 09:00:11 得分 0
跟具体的编译器的有关,做为具有良好编码风格的程序员应避免写出这样的代码。你只要知道每条语句的意思就行了,我是这样认为的。Top
15 楼kangji(尾鱼头)回复于 2002-09-23 09:00:22 得分 0
楼上,语言学不好,stack,queue,tree这些东西能学好吗??Top
16 楼cloverplus(爱上小白)回复于 2002-09-23 09:10:47 得分 0
我用VC得出结论是636,而非746.不过用TC确实是746,(不解,正在研究中...)Top
17 楼xiaong(晓雄)回复于 2002-09-23 19:18:58 得分 0
逗号运算符的运算顺序为从右到左,所以在TC中结果为746。Top
18 楼ypt(西毒)回复于 2002-09-23 19:24:17 得分 0
输出语句是逗号表达式.用一个%D就好了
各位看看我的帖子呀Top
19 楼C168(没有退路)回复于 2002-09-23 20:15:53 得分 0
这与逗号的运算顺序和逻辑运算有关。
逗号运算从右到左,逻辑运算是先加减后运算Top
20 楼gscool(炮灰)回复于 2002-09-23 20:27:20 得分 0
从右至左Top
21 楼liming112242()回复于 2002-09-23 20:50:12 得分 0
从右自左Top
22 楼wangg99(风扬)回复于 2002-09-23 20:53:22 得分 0
Orsino的正解!.
这是TC编译器的问题:从左向右进栈,而从右向左POP
i=6;i++后i值为7(打印值6)再-3就是4,加3为7
从左向右出值就是7 4 6 了。
要避免这样 的写法,尤其是初学者。
Top
23 楼Donjuan(唐璜)回复于 2002-09-23 20:54:30 得分 0
同意sandrowjw(Sandro)Top
24 楼aouuuddq(仕杰)回复于 2002-09-23 21:11:57 得分 0
这样的贴子多点就好了望Top
25 楼johnmack(爱若琴弦)回复于 2002-09-23 21:45:05 得分 0
你用的什么编译器?Top
26 楼jeff__lueny(J.L.)回复于 2002-09-23 22:10:50 得分 0
好象是优先级的问题啊
++, +=, -=Top
27 楼sleepsheep(blue)回复于 2002-09-25 13:48:52 得分 0
Orsino is rightTop




