c函数参数评测和传球 [英] c function parameter evaluation and passing
问题描述
我有一个code这是小,但我不知道为什么输出就是这样。结果
这
I have a code which is small but I couldn't get why the output is like that.
Here it is
#include <stdio.h>
int f(int i, int j, int k);
int main(int argc, char const *argv[])
{
int a;
printf("enter a\n");
scanf("%d",&a);
f(a,a++,a++);
printf("%d \n",a );
return 0;
}
int f(int i, int j, int k)
{
printf("function arguments \n");
printf("%d %d %d\n",i,j,k );
}
输入:4结果
输出:6 5 4
input: 4
output:6 5 4
推荐答案
中所标示的重复接受的答案是错误的。
f(a,a++,a++);
原因未定义行为即可。它试图修改参数 A
没有插入顺序点。另外,需要注意的函数参数评估的顺序是非常重要的就是未指定。它可以是:
Causes Undefined behavior. It tries to modify the argument a
without an intervening sequence point. Also, it is important to note that order of evaluation of function arguments is Unspecified. It can be:
- 从左至右或
- 右键向左或
- 任何神奇的顺序编译器选择。
如果你正在使用gcc,你可以使用警告标志 -wsequence点
来警告你们顺序点相关的不确定的行为。
If you are using GCC you can use the warning flag -wsequence-point
to warn you of sequence point related undefined behaviors.
在GCC如果您在严格的警告级别编译程序,编译器就会给你这个诊断:
In GCC If you compile your program at strictest warning levels, the compiler will give you this diagnostic:
prog.c中:10:18:错误:操作上A可能是不确定的[-Werror =序列点]结果
prog.c中:10:18:错误:操作上A可能是不确定的[-Werror =序列点]
prog.c:10:18: error: operation on ‘a’ may be undefined [-Werror=sequence-point]
prog.c:10:18: error: operation on ‘a’ may be undefined [-Werror=sequence-point]
参考:
C99标准§6.5.2.2:结果
第10段:
功能指示符,实际的论点评估的秩序,
实际的参数内SUBEX pressions是不确定的,但有一个序列点
前实际调用。
The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.
请注意,该报价只说有实际的函数调用前序列来看,它并不意味着有一种SUBEX pression评价参数之间的顺序点。
Note that the quote only says that there is a sequence point before the actual function call, it does not imply there is a sequence point between evaluation of subexpression arguments.
这篇关于c函数参数评测和传球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!