0

So i was learning c and the tutor on this subject was like compiler takes arguments from right to left while printing in c. okay i understood that part but why does it print output 5 5 3 when i give a=3; if we consider from right to left then it should have been 4 4 3 since it increments only once? i am confused. need clarification!

 #include<stdio.h>

int main() {
    int a;
    scanf("%d",&a);
    printf(" %d %d %d",a,++a,a++);
    return 0;
}

Output :

3
  5 5 3
rioV8
  • 24,506
  • 3
  • 32
  • 49
Yemef
  • 21
  • 5
  • the arguments are pushed to the stack from right to left – rioV8 Mar 13 '22 at 14:26
  • @rioV8: The C standard does not define the order in which arguments are pushed or evaluated. In common processors today, the first several integer arguments are passed in general registers, on the stack, and the compiler may prepare them in any order. Even if an application binary interface calls for arguments from right to left to be passed on the stack from higher addresses to lower, they are not necessarily evaluated in that order; the compiler might write them to the correct addresses in any order. – Eric Postpischil Mar 13 '22 at 14:32
  • This AGAIN? Please check the FAQ before posting:( – Martin James Mar 13 '22 at 14:33
  • @rioV8: Further, even if the argument values are prepared in order from right to left, that does not mean the side effects, notably updating the stored value of an incremented object, are evaluated in the same order or even that they are ordered atomically. One of the purposes of the rule in C 2018 6.5 2 is to allow non-atomic processing of side effects and evaluations. – Eric Postpischil Mar 13 '22 at 14:34
  • @MartinJames i didn't post again. thought it was automatically deleted . cool , i will be. but this concept of undefined behaviour is confusing as fish! – Yemef Mar 13 '22 at 14:41

0 Answers0