Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
/*on dev-cpp output=10
 visual c++ output=12
 why*/
#include<stdio.h>
#include<conio.h>
int main()
{
   int i,j;
   i=1;
   j=(++i)+(++i)+(++i);
   printf("%d",j);
   getch();
}
 
[edit]Code block added, HTML encoded - OriginalGriff[/edit]
Posted 17-Mar-13 4:44am
Edited 17-Mar-13 5:34am
v2
Comments
nv3 at 17-Mar-13 11:03am
   
There is an old saying: Crap in - crap out. I admit, OriginalGriff's explanation is more detailed :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

It seems like the compilers handle it differently. Let's divide the statement and, try to figure it out.

First, you use the Prefix Increment Operator - which means that the increment operation should occur before the use of the value of i. But, how much before?

It seems like the first compiler increments the values directly before they are used. So, as we have two + operators, the first two increments are performed before the first + operator and the third increment is performed before the second + operator. What gives us: 3 + 3 + 4 = 10.

The second compiler performs the whole of the increments at the beginning of the statement. What gives us: 4 + 4 + 4 = 12.

  Permalink  
v2
Comments
Sagar Srivastava at 18-Mar-13 12:36pm
   
but would you reply as answer if someone asks you
Sagar Srivastava at 18-Mar-13 12:53pm
   
i think it is because ambiguity in parse tree generated at time of symentic analysis .Bcaz that'only the place where this fault can occur as here only the exp is converted into preorder or postorder ........
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The difference is because the specification does not formally say when the pre-increment should occur: the compiler can decide for itself whether it wants to do the pre-increment immediately before the use or at the beginning of the statement, to the benefit of it's execution module.
Clearly, the two compilers have different ideas of how to do it!
 
You really shouldn't do that - divide it into a couple of lines and let the copiler sort out any optimisations. It is not clear to read that exactly what you should get - one school of thought could be that it "should" be:
2 + 3 + 4 = 9
not 12 or 10!
  Permalink  
Comments
Matthew Faithfull at 17-Mar-13 11:28am
   
That must be the school I went to. 5 from me.
Sagar Srivastava at 18-Mar-13 12:56pm
   
yes i also think so! Please ask this to others....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 309
1 BillWoodruff 300
2 Afzaal Ahmad Zeeshan 252
3 DamithSL 225
4 Maciej Los 215
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 17 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100