A Doubt
-
int i =2; int j = ++i + ++i; cout<<" value of j is : " << j;
in the above code the output value of j is 8,I am confused about that.Please help? -
int i =2; int j = ++i + ++i; cout<<" value of j is : " << j;
in the above code the output value of j is 8,I am confused about that.Please help? -
where does the 2nd '4' come from, in your bottom line ?
-
where does the 2nd '4' come from, in your bottom line ?
-
There is only one instance of i, so i will be incremented twice before the addition occurs.
-
There is only one instance of i, so i will be incremented twice before the addition occurs.
-
Just playing devil's advocate... But you're not adding i twice, you're adding the results of the expression ++i which is either going to be 3 or 4 depending on how many times you've done the ++ operation. Cheers, Ash
-
int i =2; int j = ++i + ++i; cout<<" value of j is : " << j;
in the above code the output value of j is 8,I am confused about that.Please help?This is a common issue and the rules are: don't do this as the results are not guaranteed. Whatever answer you get you may get a different answer with another manufacturer's compiler. If you really need to evaluate such an expression you need to break it into steps like:
int i = 2;
int j = ++i;
j += ++i;It's time for a new signature.
-
int i =2; int j = ++i + ++i; cout<<" value of j is : " << j;
in the above code the output value of j is 8,I am confused about that.Please help?It's pretty subtle. Here's a breakdown of what's hapenning:
++i
incrementsi
(soi
=3) and returns a reference toi
.- The second
++i
incrementsi
(soi
=4) and returns a reference toi
. - The
+
is called with the two references. Both are dereferenced and added (4+4).
Steve
-
int i =2; int j = ++i + ++i; cout<<" value of j is : " << j;
in the above code the output value of j is 8,I am confused about that.Please help?To reinforce what Richard said, while this is cute, it's always counterproductive. It's so subtle that even if you KNOW what you're doing, you're going to screw it up and in a production environment, you're going to confuse someone who's not was good with obfurscated code as you think you are. Just write it out in the clearest possible terms and let the compiler worry about optimizing it these days. The compiler can do optimizations that would curl your hair if you looked at what it was doing. :)
Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.
-
It's pretty subtle. Here's a breakdown of what's hapenning:
++i
incrementsi
(soi
=3) and returns a reference toi
.- The second
++i
incrementsi
(soi
=4) and returns a reference toi
. - The
+
is called with the two references. Both are dereferenced and added (4+4).
Steve
-
int i = 2; int j = ++i + i++ + ++i + i++ + ++i; cout<<"Value of j: "<<j;
The value getting for 'j' is 19. As you have said the three ++i statements will execute and the value of i become 5. How will execute the i++ statements? Could you please explain.Firstly there are two cases here, the pre-increment and (
++i
) the post-increment (i++
). The pre-increment case increments the variable and returns a reference to it. The post-increment case copies the variable, increments the original and returns the copy.Steve