【发布时间】:2022-11-17 20:45:29
【问题描述】:
让我们假设以下代码在 c 中:
#include <stdio.h>
#include <cs50.h>
int test (int a, int b);
int main(void)
{
test(2,3);
}
int test (int a, int b)
{
int c = a+b;
printf("%d \n", test(a,b));
return c;
}
为什么不能打印 test 的值而不必先将其保存在变量中并打印变量?我收到错误:
function.c:12:1: 错误:通过此函数的所有路径都将调用自身 [-Werror,-Winfinite-recursion]
谢谢!
#include <stdio.h> #include <cs50.h> int test (int a, int b); int main(void) { test(2,3); } int test (int a, int b) { int c = a+b; printf("%d \n", test(a,b)); return c; }
【问题讨论】:
-
这是完全可能的。但是你的函数是无限递归的(正如你的编译器告诉你的那样)。你需要一个方法停止打电话给
test()。
标签: c function variables methods new-operator