C order of passing Arguments . How is this code is working?

48 Views Asked by At

CODE

#include <stdio.h>

int main()
{

    int a =1;
    printf("%d%d%d%d\n",a,++a,++a,a);

    a=1;
    printf("%d%d%d%d\n",a,a++,++a,a);

    a=1;
    printf("%d%d%d\n",a,++a,a++);

    return 0;
}

Output

3333
3233
331
1

There are 1 best solutions below

4
Mike Nakis On

This is "undefined behavior". You cannot rely on the order of evaluation of function arguments in C. When we say "undefined behavior" we mean that anything might happen: it might work on one compiler, it might not work on another compiler; it might work on one compiler with optimizations disabled, and not work on the same compiler with optimizations enabled; it might not work at all; it might work flawlessly; it might dump core; it might send sperm whales and bowls of petunias falling from the sky.

(See https://www.quora.com/What-is-the-passage-on-the-whale-and-the-bowl-of-petunias-about)