How does printf in c++ process the arguments that are passed to it? -
this question has answer here:
i have been trying understand how printf processes arguments passed it. more specific can please explain how following outputs occour.
int a=1; printf("%d %d %d",++a,a,a++);// outputs: 3 3 1 a=1; printf("%d %d %d",a++,a,a++);// outputs: 2 3 1 a=1; printf("%d %d %d",a,a,a++);// outputs: 2 2 1 a=1; printf("%d %d %d",a,a++,a);// outputs: 2 1 2 a=1; printf("%d %d %d",a,a,++a);// outputs: 2 2 2
the same output occurs cout
statement.
this code
printf("%d %d %d",++a,a,a++);// outputs: 3 3 1
modifies twice in same expression. precise modifies twice without intervening sequence point. because of this code has undefined behaviour , attempt reason futile. compiler can wants, , different compilers different things. see undefined behavior , sequence points detailed explanation.
with code this
printf("%d %d %d",a,a,a++);// outputs: 2 2 1
it's undefined when a++ incremented, before or after used other arguments. output vary.
the real thing understand here don't write code this. follow rule , won't go wrong.