Notice: this is a self-Q/A and a more visible targeting the erroneous information promoted by the book "Let us C". Also, please let's keep the c++ out of the discussion, this question is about C.
I am reading the book "Let us C" by Yashwant Kanetkar.
In the book there is the following example:
#include <stdio.h>
int main(void) {
int a = 1;
printf("%d %d %d", a, ++a, a++);
}
The author claims that this code should output 3 3 1
:
Surprisingly, it outputs
3 3 1
. This is because C’s calling convention is from right to left. That is, firstly1
is passed through the expressiona++
and thena
is incremented to2
. Then result of++a
is passed. That is, a is incremented to 3 and then passed. Finally, latest value of a, i.e. 3, is passed. Thus in right to left order1, 3, 3
get passed. Onceprintf
( ) collects them it prints them in the order in which we have asked it to get them printed (and not the order in which they were passed). Thus3 3 1
gets printed.
However when I compile the code and run it with clang
, the result is 1 2 2
, not 3 3 1
; why is that?