在 C 中调用函数之前的参数评估顺序 [英] Parameter evaluation order before a function calling in C
本文介绍了在 C 中调用函数之前的参数评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 C 中调用函数时,是否可以假定函数参数的求值顺序?根据下面的程序,我执行的时候好像没有特定的顺序.
Can it be assumed a evaluation order of the function parameters when calling it in C ? According to the following program, it seems that there is not a particular order when I executed it.
#include <stdio.h>
int main()
{
int a[] = {1, 2, 3};
int * pa;
pa = &a[0];
printf("a[0] = %d a[1] = %d a[2] = %d
",*(pa), *(pa++),*(++pa));
/* Result: a[0] = 3 a[1] = 2 a[2] = 2 */
pa = &a[0];
printf("a[0] = %d a[1] = %d a[2] = %d
",*(pa++),*(pa),*(++pa));
/* Result: a[0] = 2 a[1] = 2 a[2] = 2 */
pa = &a[0];
printf("a[0] = %d a[1] = %d a[2] = %d
",*(pa++),*(++pa), *(pa));
/* a[0] = 2 a[1] = 2 a[2] = 1 */
}
推荐答案
不,函数参数在 C 中没有按照定义的顺序求值.
No, function parameters are not evaluated in a defined order in C.
参见 Martin York 对 C++ 程序员应该知道哪些常见的未定义行为?.
See Martin York's answers to What are all the common undefined behaviour that c++ programmer should know about?.
这篇关于在 C 中调用函数之前的参数评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文