0

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?

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 3
    What behavior do you expect when `scanf` fails to read a value? You can always define a function to do what you want, but note that the order of evaluation of function arguments is _unspecified_. Even if your second snippet "worked", there's no guarantee that `a` would be read before `b`. – Brian61354270 Jan 30 '21 at 03:16
  • 1
    You *need* a variable to read into. Read the documentation of [`scanf`](https://man7.org/linux/man-pages/man3/scanf.3.html), and also [this post](https://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c) about order of evaluation. – costaparas Jan 30 '21 at 03:16
  • 1
    Reading the [documentation](https://www.cplusplus.com/reference/cstdio/scanf/) "On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file." – OldProgrammer Jan 30 '21 at 03:16
  • 1
    'I don't want to create a bunch of temp variables'.....why not? Are you some kind of masochist who wants to make debugging as difficult as possible? :) – Martin James Jan 30 '21 at 03:28
  • 3
    '%d' will not read a float anyway:( – Martin James Jan 30 '21 at 03:30
  • You cannot use any input function correctly unless you ***check the return*** before you access the values. If that is what `foo()` is doing, good-on-you-mate... though ... I'm skeptical whether that is in fact the case. – David C. Rankin Jan 30 '21 at 04:47
  • Btw `main` is **not** `void` -- it returns an `int`. Please use a [valid prototype](https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function) for `main`. – costaparas Jan 30 '21 at 10:09

1 Answers1

0

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;
}
  • [Parameter evaluation order before a function calling in C](https://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c) -- this was also mentioned in the comments (twice). – costaparas Jan 30 '21 at 10:41
  • Also, you should check the return value of `scanf`. – costaparas Jan 30 '21 at 10:42
  • I would not suggest to use `cs50.h` and the related implementation to anyone unless they are taking the course and are forced to use it. – Gerhardh Jan 30 '21 at 13:31