Member Avatar for Rahul47

The expected outcome dosent agree with result. Following is the code snippet.

int a,c;
a=10; c=0;
c=(--a)+(--a);
printf("C: %d", c);
printf("\nValue of a: %d", a);

I expected C to be 17 i.e (9+8), but it comes out to be 16. And a becomes 8 as expected.
When expression is changed to like this.

c=(--a);
c=c+(--a);

Then it works fine.
Someone please point out where is loophole in my knowledge.
Thanx.

Someone please point out where is loophole in my knowledge.

The missing link is sequence points and undefined behavior. In c=(--a)+(--a);, a is being modified multiple times between sequence points, which invokes undefined behavior. The compiler is under no obligation to do anything predictable.

Your second attempt removes the undefined behavior, and thus it works as expected and predictably.

Member Avatar for Rahul47

You were right. I never heard of Sequence Points before. But, dosen't post increment supposed to hold its concept for every kind of compiler ?

But, dosen't post increment supposed to hold its concept for every kind of compiler ?

Yes. The problem is that the compiler can reorder and change expressions between sequence points however it wants as long as the defined behavior isn't affected. One possible ordering of c=(--a)+(--a); might be:

--a;
--a;
c = a + a;

Other equally rational orderings and changes are possible, which is why multiple updating side effects on an object in an expression is undefined.

Member Avatar for Rahul47

Any solution to this problem other than separately evaluating expressions ?

As quoted here
"A sequence point defines any point in a computer program’s execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed."

What does it mean by "all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed." ?

Sorry, am bad at English. I seriously dont follow him.

Any solution to this problem other than separately evaluating expressions ?

Nope.

What does it mean by "all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed." ?

Side effects are things that happen beyond the direct result of the expression. As an example, a side effect of calling printf is that something gets written to the screen. A side effect of --a is that a gets decremented.

One of the reasons for sequence points is to have a consistent place at which you an say that side effects for an expression have completed. The canonical sequence point is the semicolon that terminates a statement.

Its funny to think that c=(--a)+(--a); is not any more efficient than --a;--a;c=a+a; in terms of how it compiles. Its just that the first version is ambiguous in design as (--a) acts as a function with a side effect rather than as a variable in an algebraic formula.

Ever since I learned about the ++ operator I felt it was like cheating and could confuse people on this very thing. It would make C much more logical if you avoided ++ altogether in place of inline function calls achieving the same thing.

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.