'Question about a weird problem in functions in C language?

When calling the function billCalculator and giving it getExtraDrinks and getExtraSandwiches as arguments ,for some reason it is taking the 2nd argument first.

This is the code :

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

//Waseem Qasem Agha - 20210374 - S:18

displayWelcome() {
    printf("*******************************\n* Welcome to CS151 Restaurant *\n*******************************\n\n");
}

int getExtraDrinks() {
    int extraDrinks;
    printf("How many extra drinks do you want to get?\n");
    scanf_s("%d", &extraDrinks);
    return extraDrinks;
}
int getExtraSandwiches() {
    int extraSandwiches;
    printf("How many extra sandwiches do you want to get?\n");
    scanf_s("%d", &extraSandwiches);
    return extraSandwiches;
}

float billCalculator(int extraDrinks,int extraSandwiches) {
    float totalPrice;
    totalPrice = 2.00 + (extraDrinks * 0.5) + (extraSandwiches * 1.25);
    return totalPrice;
}

int main() {
    displayWelcome();
    printf("Total price = %.2f JD", billCalculator(getExtraDrinks(),getExtraSandwiches()));

    return 0;
}


Solution 1:[1]

The order of evaluation of arguments to a function is unspecified. This means (among other things) that if two arguments involve function calls, there is no guarantee which one will be called first.

This is formally spelled out in section 6.5.2.2p10 of the C standard regarding function calls:

There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call. Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function

So because there is no sequence point between the evaluation of each argument, they can be evaluated in any order.

If you need the functions to be called in a specific order, call them in separate statements which assign the return values to variables, then pass those variables to the function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dbush