| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 943 人关注过本帖
标题:这个程序具体怎样运行的?
只看楼主 加入收藏
wh2000292
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-3-19
收藏
 问题点数:0 回复次数:15 
这个程序具体怎样运行的?
#define MIN(x,y) (x)>(y)?(x):(y)
#define T(x,y,r) x*r*y/4
main()
{int a=1,b=3,c=5,s1,s2;
s1=MIN(a=b,b-a);
s2=T(a++,a*++b,a+b+c);
printf("%d\n",s1);
printf("%d\n",s2);
}
这个程序s2为什么等于28,请大虾们具体解释一下
搜索更多相关主题的帖子: 运行 
2007-03-25 19:36
死了都要C
Rank: 4
来 自:四川成都
等 级:贵宾
威 望:13
帖 子:1582
专家分:116
注 册:2006-12-7
收藏
得分:0 
~~看不懂``

女施主``我给你``送茶来了```师太``你就从了老衲吧``
代码本天成~~~妙头偶得之```
2007-03-25 19:50
jiangliangju
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2007-3-9
收藏
得分:0 
我在DEV下运行的结果为27,但在TURBO C 上结果为28,郁闷
2007-03-25 21:45
爱以走远
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:52
帖 子:7542
专家分:21
注 册:2007-3-16
收藏
得分:0 

#define MIN(x,y) (x)>(y)?(x):(y)
main()
{int a=1,b=3,c=5,s1,s2;
s1=MIN(a=b,b-a);
s2=(a++)*(a+b+c)*(a*++b)/4;

printf("%d\n",s1);
printf("%d\n",s2);
}

为什么这样运行s2又是108
不懂


   好好活着,因为我们会死很久!!!
2007-03-25 22:22
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 

预编译的时候是不会去加括号的,按照原来的照搬


雁无留踪之意,水无取影之心
2007-03-25 22:24
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
就相当于这个

s2=a++*a+b+c*a*++b/4

回复3楼之所以不同的编译器有不同的结果是由于++的原因,不同的编译器会有不同的处理


在运行那个表达式前A=3,B=3,C=5
比如在TC上:
s2=3*3+4+5*3*4/4; //++b会改变运算符前面的B的值

而在VC或DEV上:
s2=3*3+3+5*3*4/4

雁无留踪之意,水无取影之心
2007-03-25 22:29
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
上面我说的没有错,但是我调试了一下

输出这个的时候 b+c*a*++b/4 结果为19,此时B两个地方都是4,不知道编译器到底怎么处理的,最好的解决办法就是以后不要写这么复杂的式子,强烈建议!!!

雁无留踪之意,水无取影之心
2007-03-25 22:47
爱以走远
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:52
帖 子:7542
专家分:21
注 册:2007-3-16
收藏
得分:0 

PcrazyC 预编译的时候是不会去加括号的,按照原来的照搬就相当于这个

s2=a++*a+b+c*a*++b/4

那s2也不得108呀 是怎么一回事哦 谢谢解释一哈


   好好活着,因为我们会死很久!!!
2007-03-25 23:14
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
s2=(a++)*(a+b+c)*(a*++b)/4;
s2=3*(3+4+5)*(3*4)/4=108,就是这么回事,主要就是一个++运算符的问题,不同的编译器可能会有不同的答案,最好是听7楼的建议!

雁无留踪之意,水无取影之心
2007-03-26 16:55
jiangliangju
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2007-3-9
收藏
得分:0 

不要写这样的程序。这样的代码是错误的。参看The C Programming Language:

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.)


这是我老师告诉我的答案,我英语也不好,哪位能翻译一下

2007-03-26 18:27
快速回复:这个程序具体怎样运行的?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012296 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved