Hi . i m new in c++ programming..plz help me in sorting out this problem.....
HOW THE VALUE OF "j" VARIES IN FOLLOWING PROBLEMS ? Plz Explain...

1. int i=10,j;
j= (i++) + (i++);
cout<<j;

2. int i=10,j;
j= (i++) + (++i);
cout<<j;

3. int i=10,j;
j= (++i) + (i++);
cout<<j;

4. int i=10,j;
j= (++i) + (++i);
cout<<j;

Dani AI

Generated

Short answer: each of the one-line expressions in the original post invokes undefined behavior in standard C++. That is why the old Turbo C++ build you tried produced 20, 22, 22, 24 — the compiler simply chose one possible evaluation order, but the language does not guarantee any particular result. Both and were correct to call this undefined; the modern rule is expressed in terms of “sequenced” vs “unsequenced” evaluations: if two side effects (or a side effect and a value computation) on the same scalar object are unsequenced, the behavior is undefined. See the C++ reference on order of evaluation. (cppreference.com)

Why this is misleading: the C++ standard generally does not fix the order in which operands are evaluated, and parentheses do not create a sequencing point that forces one increment to finish before the other starts. That means any hand-written “machine instruction order” (as in ’s example) is only one possibility; compilers can evaluate or even interleave subexpressions differently. The standard gives specific sequencing rules (pre/post increment have defined relationships between value computation and side effect), but combining multiple modifications of the same object in one expression is the problem. (cppreference.com)

Workaround (defined and portable): do the increments in separate full-expressions or use temporaries so each modification is completed before the next read/write:

int i = 10;
int a = i++;   // defined
int b = i++;   // defined
int j = a + b; // defined
std::cout << j;

This produces a well-defined result on all conforming compilers.

Practical tips: enable compiler diagnostics (for GCC/Clang, turn on warnings such as -Wall and -Wsequence-point) and use runtime checks like UndefinedBehaviorSanitizer (-fsanitize=undefined) to catch these mistakes. Prefer a modern, standards-conforming toolchain rather than ancient Turbo C++ to avoid misleading behavior. See GCC warning options and UBSan documentation for details. (gcc.gnu.org)

Recommended Answers

All 8 Replies

I hate it when teachers teach undefined behavior. :angry: The value of j will depend on the compiler you are using because the language does not define the behavior of i.

But turbo C..compiler shows the value oj j in ...
1. 20
2. 22
3. 22
4. 24
But i unable to understand why its showing these values...

It's the order that the sums and increments are executed. In each expresion you listed there are different addition operations to be done and depending on what order you do them effects the result. i++/++i is shorthand for i = i + 1; If you place the ++ before the i (prefix) this is generally accepted as increment i *before* evaluating the expression, putting ++ after the i (postfix) means evaluate the expression first and increment i *afterwards*. Also placing things in parentheses generally means evaluate this part of the expression first, but I imagine they are only in this example for clarity (there's only so many +'s the human eye can take!)

But turbo C..compiler shows the value oj j in ...

It doesn't matter what the compiler does. The expression is undefined so the compiler is free to do anything, like give you 0 as the result regardless of j's value, or throw a system exception, or evaluate the expression in one of the ways it could be done. That's the problem. The expression could be evaluated in more than one way and they're all equally possible. So instead of just picking one, C++ says that the whole thing is undefined and you can't change a variable more than once between sequence points.

1. j= (i++) + (i++);

Compiles to machine instructions in this order:

j = 10 + 10
i = 10 + 1
i = 11 + 1

2. and 3.

i = 10 + 1
J = 11 + 11
I = 11 + 1

4.
i = 10 + 1
i = 11 + 1
j = 12 + 12

But like everyone else points out it's kind of nonsense, because 1. why would you ever need to do that anyway? Obfuscation? other than that I can't think of anything. and 2. it's contrary to the standard.

But like everyone else points out it's kind of nonsense, because 1. why would you ever need to do that anyway? Obfuscation? other than that I can't think of anything. and 2. it's contrary to the standard.

and 3. he has an idot for a teacher.

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.