c function parameters order of evaluation

浪子不回头ぞ 提交于 2019-12-02 03:18:16

Isn't it guaranteed that if there's a function call as parameter, that function will be called first?

No, that is not guaranteed. The order of evaluation of actual parameters is not defined. The fact that one of your parameters is the result of evaluating a function call changes nothing. The other parameters could be evaluated before the function is called.

With your example:

printf("Input: %d. Output: %d.\nFunction called %d times.\n\n", n, 
    fat(n, &count), count);

the call to printf is passed 4 parameters. These parameters can be evaluated in whatever order the compiler chooses.

isn't it guaranteed that if there's a function call as parameter, that function will be called first?

It is guaranteed that in a call such as the following:

f(g(), h)

g will be called before f is called. However, there is no guarantee that g is called before h is evaluated.

Generally, if you care which of two things happens first, put them in separate statements. That way, you won't have to memorize sequenced-before relationships or wonder when side effects occur.

In your code, printf is given 4 parameters. Each of those parameters has to be evaluated before entering printf, but the order in which they are evaluated is not specified. Compilers take advantage of this to optimise code.

C99 §6.5.2.2p10:

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.

printf("Input: %d. Output: %d.\nFunction called %d times.\n\n", n, fat(n, &count), count);

In this code, fat() is guaranteed to be called before printf() is called, but there is no guarantee about the order in which n, fat(), and count are evaluated -- count's value as an argument to printf() could be the value it had before fat() was called, or the value it has after. Further, I think this is totally undefined behavior, in which case count could take on absolutely any other value as well.

No there is no such a guarantee neither in C nor in C++ (the exception is C#). So arguments of a function call can be evaluated either from right to left (usually for most of compilers) or from left to right. The order of evaluation of function arguments is unspecified.

It is obvious that used by you compiler evaluates arguments from right to left. count is avaluated first so its value is 0. Then the compiler evaluates the call of function fat and at last it evaluates n.

To get the correct result you should split the printf statement into two statements

int rez = fat(n, &count); 
printf("Input: %d. Output: %d.\nFunction called %d times.\n\n", n, rez, count); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!