C 语言,碰到个很让人疑惑的问题

2018-12-04 17:15:04 +08:00
 xjr1022

//声明函数
void test(int a, int b);

int main() {
	int a = 2;
	test(a, a++);
	return 0;
}

void test(int x, int y) {

	printf("x=%d,y=%d", x, y);

}

//运行结果 
x=3,y=2

想请教一下这个是为什么呢,明明给形参 y 传递的是自增的量,怎么就 x 变成 3,而 y 还是 2,

3838 次点击
所在节点    C
45 条回复
alaneuler
2018-12-04 19:06:48 +08:00
这就是 sequence point 吧。。。
https://en.wikipedia.org/wiki/Sequence_point
alaneuler
2018-12-04 19:09:30 +08:00
Before a function is entered in a function call. The order in which the arguments are evaluated is not specified, but this sequence point means that all of their side effects are complete before the function is entered.
transzero
2018-12-04 22:34:18 +08:00
默认 cdecl 调用方式,传参压栈从右往左,++是后计算的,所以第二个 2,第一个自然是 3
hx1997
2018-12-04 23:25:20 +08:00
国内考研题有的知识那叫一个老旧啊... 这种题也是上世纪谭浩强观念的遗毒,简直让人怀疑他们的知识体系从国外引进来就没有更新过,只会闭门造车......
iceheart
2018-12-14 10:27:06 +08:00
这是在写 bug 么?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/514267

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX