Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 5th, 2010, 10:17 AM
EliteHussar
Guest
 
Posts: n/a
Default 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 August 30th, 2010, 08:06 PM
Registered User
 
Join Date: Jun 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

In a shorter version: post splits...and pre goes to both memory locations.
Reply With Quote
  #3 (permalink)  
Old April 1st, 2011, 02:00 AM
Registered User
 
Join Date: Dec 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Following is also an interesting example for those who are new in c/c++.

Code:
// PostFix_PreFix.cpp : Defines the entry point for the console application.
//
// Author: kFateh
// Date: Fri, April 01, 2011
// Description: Demonstrates postfix and prefix operator ++
// Note: Run it from inside of visual C++

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int a = 0;
	int b = 0;
	
	a = b++ + b;
	cout << endl << a << ", " << b;	// outputs 0, 1

	a = 0;
	b = 0;

	a = ++b + b;
	cout << endl << a << ", " << b;	// outpust 2, 1

	cout << endl;
	return 0;
}
Output from above program
Quote:
0, 1
2, 1
Press any key to continue . . .
Reply With Quote
  #4 (permalink)  
Old May 4th, 2011, 03:25 PM
Registered User
 
Join Date: May 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default increment and decrement operator

hello friend.Folllowing is a link in which increment and decrement operator are explained with the help of example.May be it will useful to you

http://www.cprogramming.tk/c/arithmetic-operators-c.htm
Reply With Quote
  #5 (permalink)  
Old October 5th, 2011, 05:22 AM
alexa007
Guest
 
Posts: n/a
Default

1. prefix increment operator denoted by ++ and prefix decrement operator denoted by --
2. The difference between the two is that in the postfix notation, the operator appears after postfix-expression, whereas in the prefix notation, the operator appears before expression, for example x--; denote postfix-decrement operator and--x; denote prefix decrement operator.
3. The prefix increment operator adds one to its operand. This incremented value is used in the expression to get the result of the expression. The postfix operator decrement by one and the decremented result is used in the expression to get the value of the expression.
4. Associativity is the order in which a operator gets executes. Prefix and postfix gets the highest precedence among the operators and the associativity of these is from right to left. The operator gets executed from right to left in an expression.
___________________
Business Intelligence Ireland
Reply With Quote
  #6 (permalink)  
Old August 2nd, 2017, 09:25 AM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2017
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Unary operators in C++
The Unary operators in C++ use only a single operand. The unary operator is either specified before or after the operand depending on the usage. C++ provides the following unary arithmetic operators:

Negation operator
Increment operator and Decrement Operator

1. Negation Operator (-)

The negation operator negates the associated operand and returns the negated value of the associated operand. The negation operator is specified to the left of the operand.

Examples:

Int x;

X = -3

Here, the constant value 3 will be negated and assigned to the variable x.

int new, old = 5;

new = - old;

Here the value of the variable ‘old’ is negated and assigned to the variable ‘new’. Hence, the variable ‘new’ will be assigned the value -5.

2. Increment (++) and decrement (--) operators

Consider that a school register is automated and with the entry of the details of every student, the student count is incremented by 1. The student count is associated to the variable ‘std_count’. This assignment is done using the following code statement:

Std_count = std_count+1;

For example, the same could be coded as:

Std_count++;

OR

++std_count;

The increment operator consists of two addition signs that follow each other without any spaces in between.

The decrement operator is the opposite of the increment operator. For instance, in the example, in case there was a student who was leaving the school, then when his details are deleted from the school register, the student count should also decrease by 1. This could be represented as:

Std_count=std_count – 1;

While using the decrement operator, the same is represented as:

std_count--; OR --std_count;

The decrement operator consists of two subtraction signs following each other without any spaces in between.

Using increment or decrement operators increases the readability of the code.

The difference between pre- and post- fixing the operator is useful when it is used in an expression. When the operator precedes the operand, increment or decrement operation takes place after using the value of the operand.

Consider the following,

a = 10;

b = 5;

c = a*b++;

In the expression, the current value of b is used for the product and then the value of b is incremented. That is, C is assigned 50 (a*b) and then the value of b is incremented to 6.

If however, the expression was

C = c * ++b;

The value stored in c would be 60, because b would first be incremented by 1, and then the value stored in c (10 * 6). The use of the increment and decrement operators in both pre- and post-fixing formats has been illustrated in example:-

#include <iostream.h>
Void main(void)
{
int value;
value = 1;
cout<” The Increment Operator” <<endl;
cout<<”Value of Value:” <<value <<endl;
cout<<”Pre-fix increment operator (++value):”<<++value;
cout<<endl;
cout<”value of value:” <<value <<endl;
cout<<”Post-fix increment operator (value++): ” <<value++;
cout<<endl;
cout<<”value of value:” <<value <<endl;
cout<< “\n\n The decrement operator”<<endl;
cout<<”value of value: ”<< value <<endl;
cout<< “Pre-fix decrement operator (--value):” << -- value;
cout<<endl;
cout<<”value of value:” <<value<<endl;
cout<<”Post-fix decrement operator (value--):” <<value- -;
cout<< endl;
cout<<”value of value:” <<value <endl;
}
Output:

The Increment Operator

Value of value:1

Using the pre-fix increment operator (++value) : 2

Value of value: 2

Using the Post-fix increment operator (value++): 2

Value of value: 3

The Decrement Operator

Value of value: 3

Using the pre-fix decrement operator (--value) : 2

Value of value:2

Using the post-fix decrement operator (value --): 2

Value of value: 1
Reply With Quote
  #7 (permalink)  
Old August 2nd, 2017, 09:28 AM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2017
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Increment (++) and decrement (--) operators

Unary operators in C++
The Unary operators in C++ use only a single operand. The unary operator is either specified before or after the operand depending on the usage. C++ provides the following unary arithmetic operators:

Negation operator
Increment operator and Decrement Operator

1. Negation Operator (-)

The negation operator negates the associated operand and returns the negated value of the associated operand. The negation operator is specified to the left of the operand.

Examples:

Int x;

X = -3

Here, the constant value 3 will be negated and assigned to the variable x.

int new, old = 5;

new = - old;

Here the value of the variable ‘old’ is negated and assigned to the variable ‘new’. Hence, the variable ‘new’ will be assigned the value -5.

2. Increment (++) and decrement (--) operators

Consider that a school register is automated and with the entry of the details of every student, the student count is incremented by 1. The student count is associated to the variable ‘std_count’. This assignment is done using the following code statement:

Std_count = std_count+1;

For example, the same could be coded as:

Std_count++;

OR

++std_count;

The increment operator consists of two addition signs that follow each other without any spaces in between.

The decrement operator is the opposite of the increment operator. For instance, in the example, in case there was a student who was leaving the school, then when his details are deleted from the school register, the student count should also decrease by 1. This could be represented as:

Std_count=std_count – 1;

While using the decrement operator, the same is represented as:

std_count--; OR --std_count;

The decrement operator consists of two subtraction signs following each other without any spaces in between.

Using increment or decrement operators increases the readability of the code.

The difference between pre- and post- fixing the operator is useful when it is used in an expression. When the operator precedes the operand, increment or decrement operation takes place after using the value of the operand.

Consider the following,

a = 10;

b = 5;

c = a*b++;

In the expression, the current value of b is used for the product and then the value of b is incremented. That is, C is assigned 50 (a*b) and then the value of b is incremented to 6.

If however, the expression was

C = c * ++b;
Reply With Quote
Reply


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 Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Logical operators ksskumar XSLT 3 November 11th, 2011 05:09 AM
Problem with Increment Operators alexdelamuerte BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 2 December 3rd, 2008 10:51 AM
Math operators nguyendh BOOK: Ivor Horton's Beginning Visual C++ 2005 1 June 25th, 2006 01:35 AM
Matrix operators Ravel99 Pro VB 6 0 April 28th, 2006 03:45 AM
Chap 4--using a combination of operators mixtli BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 June 9th, 2003 04:55 AM



All times are GMT -4. The time now is 09:15 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.