Search code examples
crecursionoperator-precedenceoperandsc-standard-library

Contradiction about Order of Evaluation of operands


When I study recursive functions in C from deitel c, I read this sentence:

Standard C does not specify the order in which the operands of most operators (including +) are to be evaluated.

But also the book says that:

associativity of '+' from left to right

Order of evaluation of operands:

order of evaluation of operands

Could anyone explain why this is?


Solution

  • Order of evaluation and associativity are two different things, take the example:

    int x = func1() - func2() - func3(); //having int return types
    

    In this expression you can't know if func1() will be evaluated first or last, meaning, you don't know which function will be called and return its value first, however you know that the associativity, as with +, will be left-to-right, first func1() - func2() then the result of that subtraction - func3().

    There is no concept of left-to-right or right-to-left evaluation in C, which is not to be confused with left-to-right and right-to-left associativity of operators: the expression f1() + f2() + f3() is parsed as (f1() + f2()) + f3() due to left-to-right associativity of operator+, but the function call to f3() may be evaluated first, last, or between f1() or f2() at run time.

    https://en.cppreference.com/w/c/language/eval_order