+ 2
Why the output of the following code is 323 ?
#include<stdio.h> int main() { int i=1 ; printf("%d%d%d",i,i++,++i); return 0 ; }
2 Respuestas
0
It's very interesting.
I found that compiler evalueate printf's arguments in non-specified order. It is optimization thing.
"What is specified by the standard, is that modifying the same variable twice in one operation is undefined behavior; ISO C++03, 5[expr]/4:"
0
hi there,
the answer depends on your compiler. in printf statement, order in which value of arguments is determined is right to left.
remaining specific to this example, first ++i is evaluated which makes the value of i as 2. remember that only ++i has been evaluated value of the 3rd argument has not been decided yet. Next i++ is executed, which makes the value of as 3. now value of arguments is determined. for preincrement current value of i is chosen, hence third argument value will be 3. for post increment operators that value will be one before executing the postincrement operator, which was 2. for normal variables current value is chosen that is 3, hence first argument will be 3.
analogous to this
printf("%d%d%d%d",i,i++,i++,++i);
will print 4324
fourth argument value 4 - current value
third argument value 2 - since before evaluating it value of i was 2
second argument value 3- since before evaluating it value of i was 3
first argument value 4- current value of i.