我正在学习 C 并遇到了一些我很好奇的事情。对我来说,像下面这样在 main 中调用 foo 不会返回任何东西:
int foo(bar){
return bar * 2;
}
int main(){
foo(10);
}
只有当我用 printf 格式化输出时,它才会将 foo 的结果返回给 main。这是故意的还是我错过了什么?以下示例有效:
int foo(bar){
return bar * 2;
}
int main(){
// will return 20
printf("%d\n", foo(10));
}
最佳答案
函数调用foo(10)在
int foo(bar){
return bar * 2;
}
int main(){
foo(10);
}
确实返回值。但是您没有在任何变量中捕获它或打印返回值。您的第二个示例实际上打印了从 foo(10);
关于c - 带参数的函数调用如何在 C 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58531744/