|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (5 bit)
Join Date: Jul 2000
Posts: 16
|
what exactly is the difference between the postfix
and prefix unary operator in c++? i want to know what exactly happens in memory? |
|
|
|
|
|
#2 |
|
Member (14 bit)
|
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 |
|
|
|
|
|
#3 |
|
Member (5 bit)
Join Date: Jul 2000
Posts: 16
|
int i=5,j;
j=i++ + ++i; cout< 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< shows 6,6 ? 'am lost! |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|