为什么我使用函数时会出现这样的结果?

问题描述 投票:0回答:1
#include <stdio.h>

int apple(int total, int ate);

int main(void) {
    printf("If you eat %d out of %d apples, there will be %d left.\n", 
             4, 10, apple(10, 4));
    return 0;
}

int apple(int total, int ate) {
    printf("This is a function with a passing value.\n");
    return total - ate;
}

当你调试这个程序时,结果将如下所示。

This is a function with a passing value.
If you eat 4 out of 10 apples, there will be 6 left.

当我根据目前所学的知识来解释它时,结果是

If you eat 4 out of 10 apples, there will be 6 left.
This is a function with a passing value.

本来应该是这样的结果,为什么结果却不同呢?

我想我还没有理解C语言的函数顺序。

c sequence
1个回答
0
投票

在这次通话中

printf("If you eat %d out of %d apples, there will be %d left.\n", 
         4, 10, apple(10, 4));

首先,所有参数在传递给函数之前都会被评估。

所以这个表情

apple(10, 4)

被评估并且函数 apple 输出其消息。

© www.soinside.com 2019 - 2024. All rights reserved.