Credit Card UK | Loans | Credit Card | Debt Consolidation | Yahoo Personals
c++ unary operators [Archive] - PCMech Forums

PDA

View Full Version : c++ unary operators


toji
07-27-2000, 08:28 AM
what exactly is the difference between the postfix
and prefix unary operator in c++? i want to know
what exactly happens in memory?

RJ
07-27-2000, 08:45 AM
Hi !!

The difference is the following:
When using it as a prefix, such as ++a, then a will be incremented before any operation with a.
When using it as a postfix (a++), then the old value of a will be used, and then a will be incremented.
Example:

cout << "The value of a is" << a++;

If a (let's say it's an integer type) is 1, then c++ displays "The value of a is 1" before incrementing it.

cout << "The value of a is" << ++a;

Now a will be incremente before, so on the screen "The value of a is 2" will displayed (in case a was 1).

RJ

toji
07-29-2000, 08:46 AM
int i=5,j;
j=i++ + ++i;
cout<<j<<","<<i;

will display 12,7

Unary operators have precedence over + operator. Then,
why doesn't the assignment part of i++ (i.e. assigning
i+1 to i) take place before the addition?

Why
int i=5;
cout<<i++<<","<<++i;
shows 6,6 ?

'am lost!