Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 07-27-2000, 09:28 AM   #1
Member (5 bit)
 
Join Date: Jul 2000
Posts: 16
Question

what exactly is the difference between the postfix
and prefix unary operator in c++? i want to know
what exactly happens in memory?
toji is offline   Reply With Quote
Old 07-27-2000, 09:45 AM   #2
RJ
Member (14 bit)
 
Join Date: Feb 2000
Location: Offenbach/Main (Germany)
Posts: 8,485
Send a message via ICQ to RJ
Lightbulb

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
RJ is offline   Reply With Quote
Old 07-29-2000, 09:46 AM   #3
Member (5 bit)
 
Join Date: Jul 2000
Posts: 16
Question

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!

toji is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:25 AM.
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.6.0