首页新闻找找看学习计划

printf这个函数的执行顺序到底是怎么样的?

0
悬赏园豆:5 [待解决问题]

include<stdio.h>

include<string.h>

int main()
{
int count = 1;
printf("%d %d %d\n", count++, count++, count++);
count = 1;
printf("%d %d %d\n", ++count, ++count, ++count);
count = 1;
printf("%d %d %d\n", count--, count--, count--);
count = 1;
printf("%d %d %d\n", --count, --count, --count);
return 0;
}

水龙头的主页 水龙头 | 初学一级 | 园豆:196
提问于:2020-01-03 16:37
< >
分享
所有回答(2)
0

这个规范里是没有指定的,取决于实现(编译器),可以看下c语言标准

jakio6 | 园豆:781 (小虾三级) | 2020-01-04 12:08

那你能不能解释一下这个程序结果的产生过程?

支持(0) 反对(0) 水龙头 | 园豆:196 (初学一级) | 2020-01-04 12:10

@水龙头: 函数参数求值,传递给函数,函数处理它接收到的值,至于那个参数先求值,未指定

支持(0) 反对(0) jakio6 | 园豆:781 (小虾三级) | 2020-01-04 12:21
0

···
$ gcc -O2 -Wall 1.c
1.c: In function ‘main’:
1.c:6:45: warning: operation on ‘count’ may be undefined [-Wsequence-point]
printf("%d %d %d\n", count++, count++, count++);
^
1.c:6:45: warning: operation on ‘count’ may be undefined [-Wsequence-point]
1.c:8:40: warning: operation on ‘count’ may be undefined [-Wsequence-point]
printf("%d %d %d\n", ++count, ++count, ++count);
^
1.c:8:40: warning: operation on ‘count’ may be undefined [-Wsequence-point]
1.c:10:45: warning: operation on ‘count’ may be undefined [-Wsequence-point]
printf("%d %d %d\n", count--, count--, count--);
^
1.c:10:45: warning: operation on ‘count’ may be undefined [-Wsequence-point]
1.c:12:40: warning: operation on ‘count’ may be undefined [-Wsequence-point]
printf("%d %d %d\n", --count, --count, --count);
^
1.c:12:40: warning: operation on ‘count’ may be undefined [-Wsequence-point]
···shell
请注意这些warning,永远不要这么写代码。当然,你可能会参加国家的计算机考试什么之类的,以前那帮命题的老师根本不懂计算机,现在的情况不知道改观了没有。

窗户 | 园豆:827 (小虾三级) | 2020-01-04 23:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册