1

So learning C by questing everything and understanding behavior of std library functions, I come across this:

In the following 2 codes;

Code1:

#include <stdio.h>

int main()
{

printf(" %d %d",printf("Hello"),printf("Bye"));

return 0;
}

Output:

ByeHello 5 3

Code2:

#include <stdio.h>

int main()
{

printf(" %d",printf("Hello")+printf("Bye"));

return 0;
}

Output:

HelloBye 8

In the first program; the expressions seems to be executed from right to left however in second code the expressions are being executed from left to right. Why would this happen. How are expressions executed exactly?

S_D
  • 41
  • 6
  • 1
    The order is unspecified in both cases. Unspecified means that the compiler is free to do whatever is more convenient for it at that particular moment. – Art Feb 01 '18 at 13:15
  • If you have `f(x) + g(y)` as an expression in C, I don't think there's any particular requirement for C to call `f` or `g` in any particular order. It's up to the compiler implementation. So you can't count on it. Similarly if you call `foo(f(x), g(x))`. The order could be anything. When C calls a function, arguments are pushed onto the stack in reverse order. So if you call `foo(x, y)`, then `y` is put on the stack first followed by `x`, then the call is made. But that doesn't mean it has to completely evaluate the expression of `g` before `f` for `foo(f(x), g(x))`. – lurker Feb 01 '18 at 13:16
  • Have a look at [Order of evaluation of operands](https://stackoverflow.com/questions/7112282/order-of-evaluation-of-operands). – lurker Feb 01 '18 at 13:25
  • [Also this previous case](https://stackoverflow.com/q/31046816/2173917). – Sourav Ghosh Feb 01 '18 at 13:36
  • Possible duplicate of [Parameter evaluation order before a function calling in C](https://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c) – Joseph Quinsey Feb 01 '18 at 15:41
  • This is not too broad to answer. I think it should be put on hold. – haccks Feb 02 '18 at 08:50

1 Answers1

6

The order of evaluation of function arguments are are not guaranteed to be in any order, i.e. order of evaluation is unspecified. They can evaluate in any order.

n1570-J.1 Unspecified behavior:

-The order in which the function designator, arguments, and subexpressions within the arguments are evaluated in a function call (6.5.2.2).

6.5.2(p12):

EXAMPLE In the function call

(*pf[f1()]) (f2(), f3() + f4())

the functions f1, f2, f3, and f4 may be called in any order. [...]

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 2
    [possible dupe, sir?](https://stackoverflow.com/q/31046816/2173917) – Sourav Ghosh Feb 01 '18 at 13:36
  • @SouravGhosh; That's not the exact dupe but there is a close one [here](https://stackoverflow.com/q/376278/2455888), but an answer there explain it with the reference of C99. I didn't find any answer that gives quote from C11 standard about the order of evaluation of arguments of a function and that's why I didn't marked it as a dupe. It should be also noted that the link you are referring to and the one I am referring in this comment is more about the side-effects on the arguments and that makes the behavior of those codes are guaranteed to be undefined. ..... – haccks Feb 01 '18 at 13:49
  • 1
    @SouravGhosh; .... In the present case the behavior of the program is **unspecified** while the cases discussed in the above links are **undefined**. – haccks Feb 01 '18 at 13:59