Current location: Hot Scripts Forums » Programming Languages » C/C++ » C++ Tip : What is difference between Postfix and Prefix Operators?


C++ Tip : What is difference between Postfix and Prefix Operators?

Reply
  #1 (permalink)  
Old 07-05-10, 09:21 AM
Learnbyexamples Learnbyexamples is offline
Disabled
 
Join Date: Jun 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
C++ Tip : What is difference between Postfix and Prefix Operators?

The built-in ++ and operators can appear on both sides of their operand:

Code:
int n=0;  
++n; /*prefix*/  
n++; /*postfix*/
You probably know that a prefix operator first changes its operand before taking its value. For example:

Code:
int n=0, m=0;  
n = ++m; /*first increment m, then assign its value to n*/  
cout << n << m; /* display 1 1*/
In this example, n equals 1 after the assignment because the increment operation took place before m's value was taken and assigned to n. By contrast,

Code:
int n=0, m=0;  
n = m++; /*first assign m's value to n, then increment m*/  
cout << n << m; /*display 0 1*/
In this example, n equals 0 after the assignment because the increment operation took place after m's original value was taken and assigned to n.
To understand the difference between postfix and prefix operators better, examine the disassembly code generated for these operations. Even if you're not familiar with assembly languages, you can immediately see the difference between the two; simply notice where the inc (increment) assembly directive appears:
Code:
/*disassembly of the expression: m=n++;*/  
mov ecx, [ebp-0x04] /*store n's value in ecx register*/  
mov [ebp-0x08], ecx /*assign value in ecx to m*/  
inc dword ptr [ebp-0x04] /*increment n*/  
  
/*disassembly of the expression: m=++n;*/  
inc dword ptr [ebp-0x04] /*increment n;*/  
mov eax, [ebp-0x04] /*store n's value in eax register*/  
mov [ebp-0x08], eax /*assign value in eax to m*/
Reply With Quote
  #2 (permalink)  
Old 04-20-11, 10:27 AM
shailu shailu is offline
New Member
 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
hi man in post fix first the condition check then increment and in prefix first the increment than check the condition........
Reply With Quote
  #3 (permalink)  
Old 04-21-11, 05:09 AM
Abitha Abitha is offline
Newbie Coder
 
Join Date: Apr 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
In prefix, first the value is incremented and then the value is assigned.
In postfix, first the value is assigned and then incremented
Reply With Quote
  #4 (permalink)  
Old 11-09-11, 03:10 AM
Ken Peterson Ken Peterson is offline
New Member
 
Join Date: Nov 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
It is important to note that a postfix increment or decrement expression evaluates to the value of the expression prior to application of the respective operator. The increment or decrement operation occurs after the operand is evaluated. This issue arises only when the postfix increment or decrement operation occurs in the context of a larger expression.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 01:16 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.