-2

When I run this program I get output as 2

#include<iostream.h>
#include<conio.h>
void main(){
    clrscr();
    int a = 10;
    int c = a-- - --a;
    cout<<c;
    getch();
}

... but when I just modify it to

#include<iostream.h>
#include<conio.h>
void main(){
    clrscr();
    int a = 10,c;
    c = a-- - --a;
    cout<<c;
    getch();
}

... I get output 0. WHY? In java both the of them gave output as 2. Whats wrong with C++? Explain :(

Souradeep Nanda
  • 3,116
  • 2
  • 30
  • 44
  • 1
    Nothing is wrong with C++ but everything is wrong with your code. Check this : http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points/4176333#4176333 – Prasoon Saurav Jul 11 '12 at 09:48
  • @KingsIndian pretty sure you mean daily. – Luchian Grigore Jul 11 '12 at 09:49
  • haha...On a serious note: OP might want to [read this](http://stackoverflow.com/questions/4352954/sequence-points-in-java) for why it's not an issue in Java. – P.P Jul 11 '12 at 09:51
  • Besides, the only correct signatures for main are `int main(int, char**)` and `int main()`. DON'T NEVER WRITE `void main()`, it just hurts our eyes. – akappa Jul 11 '12 at 09:51
  • possible duplicate of [Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...)](http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc) – Bo Persson Jul 11 '12 at 09:52
  • Please do not tag it as C++, since Turbo C++ is not C++ – PlasmaHH Jul 11 '12 at 10:19
  • @PlasmaHH: In fact, this is purely a standard C++ question. It has nothing to do with the specific compiler, though OP did not know it initially. – Gorpik Jul 11 '12 at 11:40
  • @Gorpik: all code he posted so far is quite far away from standard c++ – PlasmaHH Jul 11 '12 at 11:41
  • @PlasmaHH: `clrscr()` is very accessory here. The interesing part of the code is standard C++; it does not work as OP expected, but once again the reason can be found in the standard. – Gorpik Jul 11 '12 at 11:45

2 Answers2

3

Nothing wrong with C++, but there's something wrong in how you're using it.

The expression a-- - --a has undefined behavior in C++, and anything can happen.

The cleanest solution is to not write code like that (I wouldn't do it even if it were legal).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • ... but its a part of my school syllabus and the book we are following has some parts dedicated to this prefix and postfix stuff. What to do? – Souradeep Nanda Jul 11 '12 at 09:51
  • @SouradeepNanda fight the system! – Luchian Grigore Jul 11 '12 at 09:53
  • This has nothing to do with the operators in themselves, it's a matter of expression evaluation (that is: don't use two or more of them in the same expression, because you don't know how the expression will be evaluated). – akappa Jul 11 '12 at 09:53
  • Prefix and postfix operators are useful, but you should only use them one at a time. – Bo Persson Jul 11 '12 at 09:54
  • Yeah, I removed the java tag. I wish I could vote it up but it requires 15 reputation. Thanks anyways :D – Souradeep Nanda Jul 11 '12 at 10:03
  • @SouradeepNanda: You cannot upvote, but you can select the answer as correct, if you want to. – Gorpik Jul 11 '12 at 11:41
1

To elaborate a bit on Luchian's answer. In C++, the order in which sub-expressions are evaluated inside an expression is not specified. This means that in the following expression:

int c = a-- - --a;

there are two equally valid evaluation orders.

  1. Evaluate a-- first (returns 10), then --a (returns 8), then substract (returns 2).
  2. Evaluate --a first (returns 9), then a-- (returns 9), then substract (returns 0).

This is meant to improve optimisation oportunities for compilers. Naturally, this means that in C++ it is a mistake to use expressions that depend on evaluation order, because there is no way to know which order the compiler will select. The trade-off between efficiency and error avoidance is different in C++ and in Java.

Gorpik
  • 10,940
  • 4
  • 36
  • 56