how printf() function behaves in printf("%d %d %d",a,a=a+5,a);?

42 Views Asked by At

i think, the codesample in c given below should output: 10,15,10. But it gives output: 15,15,15. my question is how this result comes?

#include <stdio.h>

int main() {
    int a=10;
    printf("%d %d %d",a,a=a+5,a);

    return 0;
}
1

There are 1 best solutions below

2
Serve Laurijssen On

Then you are in the realm of undefined behaviour.

The C standard does not say in what order arguments are evaluated.

So on one compiler the right 'a' might be evaluated first, then a=a+5 and then the first will be 15.

Then you'll get 15, 15, 10.

Another compiler will evaluate the other way around.

Then you'll get 10, 15, 15