Hi I am trying to run the post fix and pre fix operation in C++.
The code which I tried is given below.

#include <iostream>
using namespace std;

int main()
{
        int a =0;
        cout << ++a + ++a + ++a << endl; // 7 
        a =0;
        cout << ++a + ++a + a++ << endl; // 6
        a=0;
        cout << ++a + a++ + ++a << endl; // 4
        a=0;
        cout << ++a + a++ + a++ << endl; // 3
        a=0;
        cout << a++ + ++a + ++a << endl; // 4
        a=0;
        cout << a++ + ++a + a++ << endl; // 3
        a=0;
        cout << a++ + a++ + ++a << endl; // 1
        a=0;
        cout << a++ + a++ + a++ << endl; // 0
}

Output ===>
    7
    6
    4
    3
    4
    3
    1
    0

I am not able to understand the way the value is calculated.
One or two example will be really helpful.

Thanks

Recommended Answers

All 8 Replies

cout << ++a + ++a + ++a << endl; // 7
step by step
++a + ++a + ++a(1)
++a + ++a(2) + ++a(2)
++a(3) + 4
7

Right left execution order

Let's take it step by step:

 cout << ++a + ++a + ++a << endl;

a=0.
++a increments first the variable, than use it in expression so:

a=0;
++a=1;
//so
++a + ++a + ++a = 7
a=1|  a=2|  a=2
  2 + 2  |+  3
         |      = 7

so let me explain. It will take ++a + ++a as a whole expression, than the result of it will be added by the last part. So, by that I mean that it will parshe the expression first, than add the numbers. It will be like this:

(++a + ++a) + ++a
increment a, increment a, end_of expression
add a with a, where
1st increment a=1
2nd increment a=2
add a+a=4.
result of first expression 4
next expression:
4, increment a, end_of expression
4 + 3 = 7

Next one:

cout << ++a + ++a + a++ << endl;

Here is almost the same, except the fact that:

(++a + ++a) + a++
increment a, increment a, end_of expression
add a with a, where
1st increment a=1
2nd increment a=2
add a+a=4.
result of first expression 4
next expression:
4, add a, end_of expression
increment a
4 + 2 = 6

Do it yourself like that and see the combinations.
Later edit:
jallen
It will do from left to right.
/*

++a + ++a = 4 not 3.
and
++a + ++a + 3 = 7

*/

You can't say anything useful about the code, only that it results in undefined behaviour; you're writing to the same variable twice or more without an intervening sequence point.

See section J2 and section 6.5 here.

-edit-

Looked up the exact point in section J2 I wanted to refer to:

Between two sequence points, an object is modified more than once, or is modified
and the prior value is read other than to determine the value to be stored (6.5).

Then, from section 6.5 (expressions):

Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. Furthermore, the prior value
shall be read only to determine the value to be stored.

Then from Annex C one of the definitions of a sequence point:

The end of a full expression: an initializer (6.7.8); the expression in an expression
statement (6.8.3); the controlling expression of a selection statement (if or switch)
(6.8.4); the controlling expression of a while or do statement (6.8.5); each of the
expressions of a for statement (6.8.5.3); the expression in a return statement
(6.8.6.4).

Yes, execution order irrelevant.

Thanks jallen and Andrew ( for explaining it very clearly ) for your reply
Thank you guys .

Thanks Gonbe.

Thanks jallen and Andrew ( for explaining it very clearly )

You should note that their answers are wrong though. The outcome of the expressions cannot be determined.

commented: Indeed. +5

Indeed, after looking over the article Gonbe posted, and after seeing the other outcomes, indeed, the expressions cannot be determined. The only correct part of my explanation is than that
++a increments the value, than asigns it to the variable
a++ asigns the value, than increments it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.