c功能参数的评估顺序 [英] c function parameters order of evaluation

查看:151
本文介绍了c功能参数的评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道函数参数的调用顺序没有保证,但是,如果函数调用作为参数,那么不能保证函数将被首先调用吗?



我帮助学生介绍编程入门课程的实验室,他们应该创建一个递归因子函数,它接收n(对于n!)和一个指向一个整数的指针将用于计数函数调用,然后它们应该打印结果(n,n!和count)。

许多人抱怨说他们使用指针是错误的所以我看了看代码,他们都是这样的:

$ $ $ $ $ $ $ $ $ $ $ b {
(* count)++;
if(n> 1)
{
return n * fat(n-1,count);
}

return 1;
}

int main()
{
int n,count = 0;
do
{
printf(Write n for fat(n> = 0):);
scanf(%d,& n);
} while(n <0);

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

返回0;
}

使用gcc编译(Debian 4.7.2-5)4.7.2输出是:

 为fat(n> = 0)编写n:4 
输入:4.输出:24 。
函数调用0次。

4次

所以,如果胖子应该先跑步, Function called ...应该打印出Function called 4 times而不是0。

所以,我的问题是:



尽管可以保证参数中的函数调用会在接收之前运行,但在查看非函数调用参数之前是否运行它仍然是不确定的?



另一个奇怪的是,这个相同的代码在xcode上打印出Function called 4 times...

解决方案


不能保证,如果有函数调用作为参数,那么该函数将首先被调用吗?



不,这是不能保证的。没有定义实际参数的评估顺序。你的一个参数是评估一个函数调用的结果的事实不会改变任何内容。其他参数可以在函数调用之前进行评估。



以您的示例为例:

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

调用 printf 传递4个参数。这些参数可以按照编译器选择的顺序进行评估。


I understand that there are no guarantees regarding the order which the parameters of a function will be called, but, isn't it guaranteed that if there's a function call as parameter, that function will be called first?

I help students on the laboratory of an introductory subject on programming, and they were supposed to create a recursive factorial function which receives the n (for n!) and a pointer to an integer that will be used to count function calls and then they are supposed to print the results (n, n! and count).

Many were complaining that their use of pointers were wrong so I looked at the code and they were all like this:

int fat(int n,int *count)
{
    (*count)++;
    if(n>1)
    {
        return n * fat(n-1,count);
    }

    return 1;
}

int main()
{
    int n, count=0;
    do
    {
        printf("Write n for fat (n >= 0): ");
        scanf("%d", &n);
    }while(n<0);

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

    return 0;
}

Compiling with gcc (Debian 4.7.2-5) 4.7.2 the output is:

Write n for fat (n >= 0): 4
Input: 4. Output: 24.
Function called 0 times.

4 times

So, if fat was supposed to run first, the "Function called..." should print "Function called 4 times" instead of 0.

So, my question is:

Even though it's guaranteed that a function call in a parameter will run before the "receiving" one, it is still uncertain whether it runs before looking at parameters that aren't function calls?

Another strange thing is that this same code prints "Function called 4 times" on xcode...

解决方案

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.

这篇关于c功能参数的评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆