View Full Version : c++ unary operators
what exactly is the difference between the postfix
and prefix unary operator in c++? i want to know
what exactly happens in memory?
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
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!
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.