Search code examples
cincrementdecrement

Explain the output?


#include<stdio.h>

int main()
{
int a = 10;

printf("%d %d %d",++a,a++,a--);

return 0;
}

I edited the code a bit..now the outputs are : 11 9 10 It's more complex now..


Solution

  • It's up to the compiler in which order he evaluates the parameters of a function call.


    If the compiler goes from left to right (that would explain your output):

    • a is 10
    • prä-increment which means a is incremented (the value 11 is passed as parameter)
    • post-decrement which means a is decremented later (the the value 11 is passed as parameter)
    • post-increment which means a is incremented later (the value 10 is passed as parameter)

    But if I compile this e.g. with another compiler I could get different output.