output : 12 10Code:void main(){ int a; a=10; printf("%d\t %d",++a,a++); }
i search on google but dont find anything.i thought output would be 11 11 but its not help me guys
output : 12 10Code:void main(){ int a; a=10; printf("%d\t %d",++a,a++); }
i search on google but dont find anything.i thought output would be 11 11 but its not help me guys
The behaviour is undefined, so the output could be anything. Read the FAQ answers under this C FAQ section on Expressions. Note that question 3.7 would normally apply ("the order of evaluation of the arguments to a function call is unspecified"), but here it really is undefined, not merely unspecified, because the arguments to the function are evaluated within consecutive sequence points (or you can they are unsequenced relative to each other).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I had tried this many times and observed different outputs on different kinds of compilers.
But I am quite familiar with the GCC output and can explain what exactly has happened. See below points:
- a++ : first store the value in the variable and then increment the value of the variable
- ++a : First increment the value and then store it in the variable
Now when ( ++a, a++ ) is encountered inside printf and as per right to left precedence first a++ will be executed. So, first this value will be stored as 10 and won't be incremented immediately. (But will be incremented before doing the next operation)
While doing ++a, a contains 11 (because it is already incremented in the last operation) and the value will be 12 because this pre-increment operator will be executed on 11.
So printf prints 12, 10.
Now try with the following code:
I have not tried this yet but as per my experience I believe you will get the following output:Code:void main(){ int a; a=10; printf("%t %d\t %d", ++a, ++a, a++); }
13, 13, 10 (in a GCC compiler)
The reason is, all the post increment variables updated at last, no matter wherever they are encountered.
Last edited by www.infysim.org; 02-26-2016 at 11:26 AM.
thx man thats the answer i was looking for.u clearly explained my queries thx again
Again, the order of evaluation of function arguments is unspecified, so there is no "right to left precedence" to speak of.Originally Posted by www.infysim.org
Um, www.infysim.org's answer is specific to a particular compiler with particular compile options. The behaviour is undefined, hence the observation that "I had tried this many times and observed different outputs on different kinds of compilers".Originally Posted by san12345
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> I had tried this many times and observed different outputs on different kinds of compilers.
There is no mystery once you understand what
- a sequence point is
- that an object may be modified only once between sequence points.
When you know these things, you can see that analysing anything which modifies an object more than once is inherently broken, and analysing it is just a waste of time. Nothing you could learn from the analysis would be of lasting benefit.
See also Question 3.1 and related questions.Originally Posted by standard_c99
And for a practical demonstration of many different compilers and compiler flags, see
Post and Pre Increment
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.