C - Scanf 直接进入函数参数?
C - Scanf straight into an function argument?
所以,如果我有函数 foo
:
float foo(float a, float b){
return a*b;
}
我从另一个函数调用它,我怎么能这样调用它?
void main(){
foo(scanf("%d"), scanf("%d");
}
scanf 没有 return 输入字符串,我不想创建一堆临时变量。这可能吗?如果可能的话如何?
您不能直接 return 来自 scanf 的值。但是您可以自己创建一个函数,该函数 return 是 scanf 的一个值,但它仍然会在其中使用临时变量:
int getInteger() {
int input;
scanf("%d", &input);
return input;
}
然后你可以像这样使用它:
foo(getInteger(),getInteger());
但是,如果您真的不想使用变量,您可以只使用 cs50 库中的 get_int()
。更多信息 here.
#include <cs50.h> // include cs50 to use get_int
int main(void)
{
foo(get_int("Number 1: "),get_int("Number 2:"));
return 0;
}
所以,如果我有函数 foo
:
float foo(float a, float b){
return a*b;
}
我从另一个函数调用它,我怎么能这样调用它?
void main(){
foo(scanf("%d"), scanf("%d");
}
scanf 没有 return 输入字符串,我不想创建一堆临时变量。这可能吗?如果可能的话如何?
您不能直接 return 来自 scanf 的值。但是您可以自己创建一个函数,该函数 return 是 scanf 的一个值,但它仍然会在其中使用临时变量:
int getInteger() {
int input;
scanf("%d", &input);
return input;
}
然后你可以像这样使用它:
foo(getInteger(),getInteger());
但是,如果您真的不想使用变量,您可以只使用 cs50 库中的 get_int()
。更多信息 here.
#include <cs50.h> // include cs50 to use get_int
int main(void)
{
foo(get_int("Number 1: "),get_int("Number 2:"));
return 0;
}