C中的函数调用之前参数评测秩序 [英] Parameter evaluation order before a function calling in C
本文介绍了C中的函数调用之前参数评测秩序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在C调用时,它承担的函数参数的计算顺序?根据以下程序,似乎没有特定的顺序,当我执行它。
的#include<&stdio.h中GT;诠释的main()
{
诠释一个[] = {1,2,3};
INT * PA; PA =&放大器;一个[0];
的printf(A [0] =%d个\\ TA [1] =%d个\\ TA [2] =%d个\\ N,*(PA),*(PA ++),*(++ PA));
/ *结果:a [0] = 3 [1] = 2 [2] = 2 * / PA =&放大器;一个[0];
的printf(A [0] =%d个\\ TA [1] =%d个\\ TA [2] =%d个\\ N,*(PA ++),*(PA),*(++ PA));
/ *结果:a [0] = 2 [1] = 2 [2] = 2 * / PA =&放大器;一个[0];
的printf(A [0] =%d个\\ TA [1] =%d个\\ TA [2] =%d个\\ N,*(PA ++),*(++ PA),*(PA));
/ *一个[0] = 2 [1] = 2 [2] = 1 * /}
解决方案
没有,函数参数没有在C定义的顺序进行。
请参阅马丁纽约的答案<一个href=\"http://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviour-that-c-programmer-should-know-about\">What都是常见的未定义行为的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\ta[1] = %d\ta[2] = %d\n",*(pa), *(pa++),*(++pa));
/* Result: a[0] = 3 a[1] = 2 a[2] = 2 */
pa = &a[0];
printf("a[0] = %d\ta[1] = %d\ta[2] = %d\n",*(pa++),*(pa),*(++pa));
/* Result: a[0] = 2 a[1] = 2 a[2] = 2 */
pa = &a[0];
printf("a[0] = %d\ta[1] = %d\ta[2] = %d\n",*(pa++),*(++pa), *(pa));
/* a[0] = 2 a[1] = 2 a[2] = 1 */
}
解决方案
No, function parameters are not evaluated in a defined order in C.
See Martin York's answers to What are all the common undefined behaviour that c++ programmer should know about?.
这篇关于C中的函数调用之前参数评测秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文