answersLogoWhite

0


Best Answer

They both increment the variable. But the value returned by the pre-increment operator is the value of the variable after it has been incremented, while the value returned by the post-increment operator is the value before it has been incremented.

For example:

int a = 1;

int b = ++a;

// Now a is 2 and b is also 2.

int a = 1;

int b = a++;

// Now a is 2 but b is 1.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

16y ago

The post and pre increment operators work like this:

POST: $i++;

PRE: ++$i;

The difference is NOT apparent in most statements. For instance, the above two statements do the same thing- increase $i by 1.

But when combined with other statements like echo, or comparisions, then the difference comes to light.

e.g.

$i=1;

echo $i++;

prints 1.

$i=1;

echo ++$i;

prints 2.

In the first case, $i is first printed (or compared) and then incremented.

In the second case, $i is first incremented, then printed (or compared).

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Pre increment means that if you have for instance a loop:

{

...

for (int i = 1; i<10; ++i)

{

// the index i before the first iteration equals 1

...

// the index i after the first iteration equals 2

}

}

Post increment

{

...

for (int i = 1; i<10; i++)

{

// the index i after before the iteration equals 2

...

// the index i after the first iteration equals 3

}

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Both pre-increment and post-increment ultimately increment the operand. The difference is that during the evaluation of the expression, the results of the intermediate expression for the pre-increment case uses the already incremented value, while the post-increment case uses the non incremented value. An example.

int a = 2;

int b = 5;

int c = a ++ * b;

In this case, a will have a value of 3, and c will have a value of 10.

If, on the other hand, you said

int c = ++ a * b;

then c would have a value of 15, while a would still have a value of 3.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The pre-increment operator (++identifier) increments the identifier before making its value available to any surrounding expression. The post-increment operator (identifier++) increments the identifier after making its value available to any surrounding expression.

int a = 2;

int b = 3;

int c = a++ + b;

the result is that a 6

Used alone, the two versions have the same effect, however, the pre-increment version is more efficient because the compiler does not need to generate an interim copy of the object. This can be important if the object is complex, such as a class, and if the expression is used within a tight loop.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The execution sequence/timing.

For pre-decrement, the decremental happens BEFORE the actual operation (passing as an argument to another function, for example).

Post-decrement, the decremental (-1) happens AFTER the actual operation.

The confusion parts is the stand-alone version. They are "equivalent":

n--;

--n;

But:

int k = 10;

Call_FUN(k--); // k is 10, passed to CALL_FUN, but after CALL_FUN finished, K is 9

Call_ARG(--k); // K is 9 from above, substract 1 first, becomes 8, then passed 8 to Call_ARG()

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Consider the following declaration:

class MyClass

{

public:

MyClass:x( 0 ){}

private:

int x;

public:

MyClass& operator++() //Prefix increment operator (e.g., ++MyObject).

{

x = x + 1; // perform the increment.

return( *this ); // return by reference.

}

MyClass operator++(int) //Postfix increment operator (e.g., MyObject++).

{

MyClass temp = *this; // temporary copy.

x = x + 1; // preform the increment.

return( temp ); // return the copy by value.

}

};

Note that the postfix increment operator creates and returns a temporary value, which calls the class copy constructor. If this temporary value is not actually needed by the caller, then there is no point in copying the current value. This applies to all types, including primitives:

for( int x = 0; x < 10; ++x ){} // preferred method.

for( int y = 0; y < 10; y++ ){} // creates temporary variables that are never actually used.

For primitive types, the difference in performance is negligible, but for classes it is vital that you avoid creating copies unnecessarily. If you get into the habit of using the prefix increment operator on all your primitives, the habit will extend to all your classes. Studies have shown that people who always use the postfix increment operator on primitives tend to do the same with their classes -- which can heavily impact on performance when those classes are large and complex.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

The difference is when the variable is incremented. Here is a simple example:

b = ++a; //Here, "a" is incremented, then the value is copied to "b".

b = a++; //Here, the value of "a" is FIRST copied to "b", then "a" is incremented.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

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 */

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between post and pre increment unary operators in c with example?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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;


Can you increment the value of a variable by 2 using any increment operator?

There is no such increment operator in C language to increment the value of a variable by 2.An increment operator only increments the value by 1. however you can apply the increment operator twice to get an increment of 3. No: you cannot: ++(++a) won't compile. Yes. Example: a += 2; but += is not an increment operator, it's a shorthand of a=a+2; just like a++ is a shorthand for a= a+1


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


Pre increment and post increment?

Both increment the value of the variable by one. The difference is the value of the increments expression itself. With preincrement value is taken after incrementing, and with postincrement value is taken before incrementing. Example: Let x have value 5. y = ++x; Both y and x are assigned value 6. Again let x have value 5. y = x++; y is assigned value 5. x is assigned value 6.


Rewrite the BNF of Example 3.4 to add the and -- unary operators of Java?

123

Related questions

Can you give an example of a sentence using the increment?

"The increment" means the distance between two things. If you measure the increment between those numbers, you can find the difference.


What is the Difference between post fix and prefix plus plus operator?

For both cases, the ++ operator increments the integer by one. The difference lies in when it makes that increment. Take the following for example: int B = 3 A = ++B // A = 4, B = 4 --------------- int B = 3 A = B++ //A = 3, B = 4 In the prefix example, the increment occurs before the assignment. In the suffix example, the increment occurs after the assignment.


Is there a difference between you plus plus and plus plus you?

you++ will return the current value of you and increment it. ++you will increment it and then return the new value of you. So, for example: int you = 0; cout &lt;&lt; you++; // this will print 0 cout &lt;&lt; you; // this will print 1 int you = 0; cout &lt;&lt; ++you; // this will print 1 cout &lt;&lt; you; // this will also print 1


What is the difference between a x a and 2a?

a x a means that a is multiplied by a. 2a means that a is multiplied by 2. Unless a happens to be 2, a x a will give a different result.The difference is a x (a-2). for example if a = 2, then the difference is zero. If a = 3, the difference is 3The operators are different: 2a = a + a (addition).


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;


Can you increment the value of a variable by 2 using any increment operator?

There is no such increment operator in C language to increment the value of a variable by 2.An increment operator only increments the value by 1. however you can apply the increment operator twice to get an increment of 3. No: you cannot: ++(++a) won't compile. Yes. Example: a += 2; but += is not an increment operator, it's a shorthand of a=a+2; just like a++ is a shorthand for a= a+1


Where can someone learn about Boolean operators?

Boolean operators are words that are used to define the relationship between other words. For example, both AND and OR are considered Boolean operators. More in depth information can be found in advanced grammatical texts.


What is the difference between has and have and give an example?

the difference between has and have is that you use has in sentences with : ( she , he and it ) for example : she has a book . but you use have in sentences with : ( I , you , we and they ) for example : you have a book , I have a book .


How do you increment a microprocessor register?

INC {register} Example: INC A


What is the difference between a nuatral and a polictical boudary give a example of each?

what is the difference between a natural and a political boundary.Give a example of each.


Difference between eg and ie?

example given......in example


How what operator is most efficient you plus plus or plus plus you?

Efficiency is the same; the difference is when the "++" is evaluated, before or after other operations. For example: a = b++ // This will first copy b to a, then increment b. a = ++b // This will first increment b, then copy it to a. If you have the "++" operator by itself, it makes no different if you use prefix or postfix. a++ is the same as ++a.