0

Assume I have a function that takes user input and returns returns it:

int userinput(void)
{
    int c, tmp;
    scanf("%d", &tmp);
    while ((c = getchar()) != EOF && c != '\n');
    return tmp;
}

Let's say in another function I want to calculate the difference between two values, which are passed as arguments.

void difference(int num1,  int num2)
{
     printf("%d\n", num1 - num2);
}

When I now call my function 'difference' as:

difference(userinput(), userinput());

and for example input numbers 10 and 5 via the terminal I get -5 as a result and when I enter 5 and 10 I get 5. So it seems values are being switched up. Why is this the case and what can I do to avoid this apart from first assigning the return values of 'userinput' to a variable?

vinjardin
  • 1
  • 1

0 Answers0