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

Dani AI

Generated

Short answer: the single-line expressions that mix multiple ++/-- on the same variable invoke undefined behavior in C++. The numeric results you observed are just what a particular compiler happened to do — they are not guaranteed by the language. was right to point to the standard; you cannot reliably "step through" these expressions assuming a particular evaluation order as and did. cppreference: Order of evaluation · C++ working draft (N4659)

Why: the standard prohibits unsequenced side effects on the same scalar object. In older wording this was phrased with sequence points; in C++11+ it is expressed with the sequenced-before / unsequenced relations. If two side effects on the same memory location are unsequenced (or a side effect is unsequenced with a value computation of the same object), the behavior is undefined. That is the formal reason you cannot deduce a single correct result. cppreference: Order of evaluation — undefined cases

How to make it well-defined: don’t modify the same object more than once inside a single expression. Break the operations into separate, sequenced statements or use temporaries. For example:

int a = 0;
int x = ++a;
int y = ++a;
int z = ++a;
std::cout << (x + y + z) << '\n';   // deterministic result

Each increment here is in its own full-expression, so the order is defined. See the rule that “each full-expression is sequenced before the next.” cppreference: Order of evaluation

Practical tips: enable compiler warnings (GCC/Clang warn about sequence-point / unsequenced modifications), compile with -Wall -Wextra and fix such warnings, and test on more than one compiler when behavior looks surprising. Note that C++17 relaxed/changed some evaluation details (some cases became indeterminately sequenced or unspecified rather than outright undefined), but relying on those subtleties is brittle — the safest fix is to write clearly sequenced code. GCC warning options · cppreference: Order of evaluation (C++17 notes)

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 .

-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.