main()
{
int i=1;
printf("%d,%d,%d",i,i++,i++);
}
the o/p is 3,2,1.
But how it is possible In which order does printf executes??why it is not 1,2,3.
main()
{
int i=1;
printf("%d,%d,%d",i,i++,i++);
}
the o/p is 3,2,1.
But how it is possible In which order does printf executes??why it is not 1,2,3.
Short answer: the call in the first post invokes undefined behavior. The C standard does not guarantee any particular order for evaluating function-call arguments, and modifying the same scalar object more than once (or modifying it and also reading it) without sequencing is undefined — that’s why you can see 3,2,1 on one build, something else on a different build, or even strange crashes. (cppreference.com)
Why it’s undefined: the three arguments to printf are evaluated with no sequencing between them, and both i++ expressions produce side effects on i. The rule in the standard (C11 and later wording) says an unsequenced side effect relative to another side effect or value computation on the same scalar object yields undefined behavior. The commas in a function call do not serialize argument evaluation. (cppreference.com)
Relating to the thread: ’s right-to-left observation can match what some compilers do, but it’s an implementation artifact, not a language guarantee. and were correct to flag this as undefined; empirical “it prints X on my machine” evidence is worthless for correctness. Compilers and optimization levels can and do produce different outputs or warnings. (stackoverflow.com)
Safe patterns and practical tips: compute values in separate, sequenced statements and pass those to printf. For example:
int i = 1;
int a = i++;
int b = i++;
int c = i;
printf("%d,%d,%d\n", a, b, c); /* well-defined: prints 1,2,3 */ Enable compiler diagnostics (-Wall / -Wsequence-point in GCC, Clang’s -Wunsequenced) and use the UndefinedBehaviorSanitizer (-fsanitize=undefined) or static analysis to catch these mistakes early. Avoid modifying and reading the same object in one expression. (gcc.gnu.org)
Jump to Post— zeroliken 79actually it's undefined behavior
see this similar thread for clarifications and read WaitP and Narue's comments
Jump to Post— deceptikon 1,790Well.. m sorry to go against what u just said zeroliken, but it isnt undefined... Its how the prefix and postfix operators work..
Nope, it's definitely undefined because i is modified more than once between sequence points. The commas in a function call don't count as sequence points.
…
hi,
i think its so: first the third %d gets a 1 value because of i++, it has a postincrement so after that i = 2. Then the second %d gets the before modified i value thats 2, and it has also a postincrement so after that i = 3. Then there is no more incrementation so the first %d is 3.
an example for a 1,2,3 would be
i=3;
printf("%d %d %d",i,i--,i--);
ps.: as u see the evaluation goes from right to left... hope this helps u :)
actually it's undefined behavior
see this similar thread for clarifications and read WaitP and Narue's comments
Well.. m sorry to go against what u just said zeroliken, but it isnt undefined... Its how the prefix and postfix operators work..
int main()
{
int i=1;
printf("%d,%d,%d",i,++i,++i);
return 0;
}
Try this and this should give u the output as 1,2,3.
hi,
zeroliken is right!
I runned that code that adityatandon wrote it put: 3,3,2
more information: http://c-faq.com/expr/evalorder2.html
Well.. m sorry to go against what u just said zeroliken, but it isnt undefined... Its how the prefix and postfix operators work..
Nope, it's definitely undefined because i is modified more than once between sequence points. The commas in a function call don't count as sequence points.
Try this and this should give u the output as 1,2,3.
Undefined behavior includes doing what you expect, so empirical "it works for me" evidence is completely worthless. In the case of undefined behavior, we must refer to what the language standard says.
int main()
{
int i=0;
printf("%d%d%d",i,i++,i++);
return(0);
}
+1 for undefined, cause printf in your project library maybe written differently or that can be compiler specific no ?
anyway why would someone write like that and care about it?? :))
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.