简体   繁体   中英

C - Scanf straight into an function argument?

So, if I have the function foo :

float foo(float a, float b){
    return a*b;
}

and I call it from another function, how can I call it like this?

void main(){

    foo(scanf("%d"), scanf("%d");

}

scanf doesn't return the input string, I don't want to create a bunch of temp variables. Is that possible, and if so how?

You can't directly return a value from scanf. But you can create a function yourself that returns a value from scanf, but it'll still use temp variables inside of it:

int getInteger() {
  int input;
  scanf("%d", &input);
  return input;
}

Then you can use it like: foo(getInteger(),getInteger());

However, if you really don't wanna use variables, you can just use get_int() from cs50 library. More info here .

#include <cs50.h> // include cs50 to use get_int

int main(void)
{
  foo(get_int("Number 1: "),get_int("Number 2:"));
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM