-7
#include<stdio.h>
int main()
{
int a=1;
printf("%d%d%d",a,++a,a++);

}

Here why is the output 331 and not 122.

The reason i found on internet was that arguements are passed grom right to left . First a++ then ++a then a gets evaluated and then get printed in the reverse order. Is it the right reason.

1 Answers1

-1

gcc -S your_source.c explain your question

    movl    $1, -4(%rbp)    #, a
    movl    -4(%rbp), %ecx  # a, a.0
    addl    $1, -4(%rbp)    #, a
    addl    $1, -4(%rbp)    #, a
    movl    -4(%rbp), %edx  # a, tmp61
    movl    -4(%rbp), %eax  # a, tmp62
    movl    %eax, %esi      # tmp62,
    movl    $.LC0, %edi     #, offset to "%d%d%d" sttring
    movl    $0, %eax        #,
    call    printf  #

compiler developer must not expect that someone will try to use it in such an ugly way

if you really need 122: then change asm code as follow:

    movl    $1, -4(%rbp)    #, a
    movl    -4(%rbp), %eax  # a, a.0
    addl    $1, -4(%rbp)    #, a
    movl    -4(%rbp), %edx  # a, tmp61
    movl    -4(%rbp), %ecx  # a, tmp62
    movl    %eax, %esi      # tmp62,
    movl    $.LC0, %edi     #,
    movl    $0, %eax        #,
    call    printf  #
    addl    $1, -4(%rbp)    #, a
    leave
Ivan Ivanovich
  • 736
  • 5
  • 11