answersLogoWhite

0

When a pre increment is followed by a post increment?

Updated: 8/17/2019
User Avatar

Wiki User

8y ago

Best Answer

You cannot follow a prefix increment with a postfix increment.

int x = 40;

int y = ++x++; // error

This has to be an error because the postfix operator has higher precedence than the prefix operator. Thus the above code is equivalent to:

int x = 40; int y = ++(x++); // error

The expression (x++) is evaluated first. This increments x to 41 but the expression evaluates to 40 (the original value of x). Thus the prefix operator is essentially trying to evaluate the expression ++40 rather than ++x. This cannot work because the value 40 is not a modifiable lvalue. It has to be modifiable because we want to increment the value and return a reference to the modified value. But there's nothing to refer to here. The value is temporary and will fall from scope immediately after we use it. That is, the prefix operator may well be able to increment the temporary value to 41, but that value immediately falls from scope. With nothing to refer to, the prefix expression cannot be evaluated.

The only way we can use both operators together is if we reverse the precedence using parenthesis:

int x = 40;

int y = (++x)++;

Now the prefix operator is evaluated first, returning a reference to x which (now) holds the value 41. The postfix operator then increments x to 42 but returns the original value of 41 which is then assigned to y. Thus when all statements have been executed, y holds the value 41 while x holds the value 42.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When a pre increment is followed by a post increment?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How different is pre and post increment in c plus plus from pre and post increment in C programming?

The pre and post increment (and decrement) operator is the same in C++ as it is in C.


Is x plus equals y is post increment or pre increment?

The '+=' operator behaves like a pre increment operator.


What is the use of using pre increment operator?

int a, b; b = 5; /* post-increment operator */ a = b++; /* a is now 5, and b is now 6. */ /* pre-increment operator */ a = ++b; /* a and b are now both 7 */


How can you differentiate overloading of pre-fix and post-fix increment operator?

The prefix increment operator is overloaded as operator++() while the postfix increment operator is overloaded as operator++(int).


How the predecrementer and postdecrementer are taken care of while declaring operator overloading?

When you overload the -- and ++ operators in C++, if you want the pre version, aka --var, simply declare the function without an argument; whereas if you want the post version, aka var--, simply declare the function with an argument of type int. class abc { public: abc& operator++(); // pre-increment form abc& operator++(int); // post-increment form ... }; abc::operator++() { ... pre increment stuff return *this; } abc::operator++(int) { ... post increment stuff return *this; }


What is the declaration of overloaded pre-increment operator implemented as member function?

The pre-increment operator accepts no parameters and returns the same object (by reference) after incrementing. The post-increment operator accepts an unused (dummy) integer parameter and returns a copy of the object (by value) that is made immediately prior to incrementing the object. Note that it is good practice to always use the pre-increment operator even if a post-increment operator exists. The only time you should ever use a post-increment is when you actually store the return value. If you don't store the return value then you will end up making an unnecessary copy, which is highly inefficient. With primitive data types that are less than or equal in length to a pointer this isn't a major issue, but it's good practice nonetheless. If you do it for primitives then you're far more likely to remember to do it for class instances as well. The following example emulates an integer type with pre-increment and post-increment operators implemented: class Simple { public: // Construction: Simple(int data = 0):m_data(data){} Simple(const Simple& simple):m_data(simple.m_data){} public: // Assignment: Simple& operator= (const Simple& simple) { m_data = simple.m_data; return( *this ); } // pre-increment: Simple& operator++ () { // no parameters! ++m_data; // increment this object return( *this ); } // return a reference to this object // post-increment: Simple operator++(int) { // int parameter (not used)! Simple copy( *this ); // call the copy constructor ++m_data; // increment this object return( copy ); } // return the copy (by value) private: int m_data; };


What are applications of pre-increment and post-increment operators why these operators behaviour are made like this is there any reason for such behaviour?

All operators must return a value, and it is this return value (the evaluation of the operation) that differentiates the pre-increment and post-increment operators. If the return value is of no concern, then the pre-increment operator is the preferred version, particularly when working with objects. This is because the post-increment operator's implementation will generally make a copy of the original object which it will return after incrementing the original object. If the return value is of no concern, then making a copy adds an expensive overhead (this doesn't apply to primitive data types since CPU optimisation can pre-empt the return value without requiring a copy to be made). Applications for post-increment and pre-increment are many and varied, but ultimately the goal is to increment the value. Whether you use post-increment or pre-increment is largely down to whether you need to use the return value or not, and whether you need to use the original value or the incremented value. Although the same thing can be achieved with longhand code, it is more efficient to use the appropriate post-increment or pre-increment operators. For instance: int x=1; int y=x; ++x; Can be implemented more efficiently with: int x=1; int y= x++; Whereas: int x=1; ++x; int y=x; Can be implemented more efficiently with: int x=1; int y= ++x; Use caution when pre-incrementing the same value in a compound statement. For example: int x=0; int y= ++x * ++x; The expected result is 2, but the actual result is 4. This is because both expressions (++x) return the same value: a reference to x. And since x is incremented twice, the expression evaluates to 2*2, not 1*2. To get the expected result of 2, each operation must be evaluated separately: int x=0; int y= ++x; y*= ++x;


Why c plus plus name was given?

In C, the ++ operator means to increment the object associated with it. If you said xyz++; for instance, you would increment xyz. (There are pre-increment and post-increment forms, but that is out of scope for this question.) When the C language was enhanced, the new language was called C++, implying that C++ was the "next", or "incremented", version of C.


Program in java to illustrate the pre increment and post increment?

An example might help:int a, b;a= 2;b= a++;/* now a=3, b=2 */a= 2;b= ++a;/* now a=3, b=3 */Read more: What_is_difference_between_pre_increment_and_post_increment


What is increment and decrement operators?

increment operator increments the variable by 1 at a time and decrement operator decrements by 1 in this we have two types increments pre_increment and post increment. In pre_increment the original value is incremented by 1 and assign the new value n=10 i=++n then i =11 In post increment the original value is assigned and after it increments value by 1. n=10 i=n++ then i=10 example: k=5 i=k++ + ++k i=? ans: in first k++ value is 5 second ++k value is 7 i=5+7=12


What is the meaning of plus plus in c?

++a (plus plus a) is pre-incrementing operator to aa=10;printf("%d",++a); /* it will print 11 as ++a increment first a by 1 then prints it */printf("%d",a++); /*it will printf 10 as it is post _ increment operator , it prints the value a first then increment it by 1 */


What is post and pre issue capital?

Post and pre issue capital is one thing. It is capital and post money.