Incrementing (adding 1 to) and decrementing (subtracting 1 from) a variable are so common that they have their own operators in C. There are actually two version of each operator — a prefix version and a postfix version.
| Operator | Symbol | Form | Operation |
|---|---|---|---|
| Prefix increment | ++ | ++x | Increment x, then evaluate x |
| Prefix decrement | –– | ––x | Decrement x, then evaluate x |
| Postfix increment | ++ | x++ | Evaluate x, then increment x |
| Postfix decrement | –– | x–– | Evaluate x, then decrement x |
The prefix increment/decrement operators are very straightforward. The value of x is incremented or decremented, and then x is evaluated. For example:
int x = 5; int y = ++x; // x is now equal to 6, and 6 is assigned to y
The postfix increment/decrement operators are a little more tricky. The compiler makes a temporary copy of x, increments x, and then evaluates the temporary copy of x.
int x = 5; int y = x++; // x is now equal to 6, and 5 is assigned to y
In the second line of the above example, x is incremented from 5 to 6, but y is assigned the value of the copy of x, which still has the original value of 5.
Here is another example showing the difference between the prefix and postfix versions:
int x = 5, y = 5; cout << x << " " << y << endl; cout << ++x << " " << --y << endl; // prefix cout << x << " " << y << endl; cout << x++ << " " << y-- << endl; // postfix cout << x << " " << y << endl;
This produces the output:
5 5 6 4 6 4 6 4 7 3
On the third line, x and y are incremented/decremented before they are evaluated, so their new values are printed by cout. On the fifth line, a temporary copy of the original values (x=6, y=4) is sent to cout, and then the original x and y are incremented. That is why the changes from the postfix operators don’t show up until the next line.
Side effects
A side effect is a result of an operator, expression, statement, or function that persists even after the operator, expression, statement, or function has finished being evaluated.
Side effects can be useful:
x = 5;
The assignment operator has the side effect of changing the value of x permanently. Even after the statement has finished executing, x will have the value 5.
Side effects can also be dangerous:
int x = 5; int nValue = Add(x, ++x);
C++ does not define the order in which function parameters are evaluated. If the left parameter is evaluated first, this becomes a call to Add(5, 6), which equals 11. If the right parameter is evaluated first, this becomes a call to Add(6, 6), which equals 12!
As a general rule, it is a good idea to avoid the use operators that cause side effects inside of compound expressions. This includes all assignment operators, plus the increment and decrement operators. Any operator that causes a side effect should be placed in it’s own statement.
Note that side effects are not confined to operators, expressions, and statements. Functions can also have side effects, which we will discuss in the section on global variables (and why they are evil).
3.4 — Sizeof, comma, and arithmetic if operators
|
Index
|
3.2 — Arithmetic operators
|
3.4 — Sizeof, comma, and arithmetic if operators
Index
3.2 — Arithmetic operators
you have written the statement for the post fix increment as;
The postfix increment/decrement operators are a little more tricky.
The compiler makes a temporary copy of x, increments x,
and then evaluates the temporary copy of x.
but compiler should first evaluate the temporary copy of x and then it should increment x.
please correct me if i am wrong!!
thanks
Som Shekhar
I believe what I wrote is correct. If you find something that seems to indicate otherwise, let me know.
CAN YOU EXPLAIN THE BELOW ASSIGMENT OPERATOR, PLEASE:
INT SUM, CTR;
SUM=12;
CTR=4;
SUM = SUM + (++CTR)
THE VALUE IS 15 …..HOW?
SUM = SUM + (–CTR)
THE VALUE IS 15 …..HOW?
SUM = SUM + (CTR++)
THE VALUE IS 16 …..HOW?
SUM = SUM + (CTR–)
THE VALUE IS 16 …..HOW?
thanx in advance
Tried to evaluate the first expression and I get the expected result SUM = 17.
Have not tried the other exampels but assume I will get the expected results as well. No idea how you end up with your results?!
At face value this looks strange…I will go over them in order.
1. This one should evaluate to 17, like Peter P said. There is no way it can evaluate to 15, since it will become: (SUM = 12 + 5).
2. again, something is wrong. This one should be (I think), SUM = 12 + (-4) = 8.
3. This one should be correct, as the postfix ++ will increment CTR AFTER the expression is evaluated (I think).
4. I have never seen a single – postfixed like this, I dont believe it should even compile.
Use your compiler to try and compile each statement, and see what happens. I have just written my observations here, I did not compile the code.
after the first line sum = 15 and ctr = 5
so the second equation “SUM = SUM + (–CTR)” is SUM = 15 – 5 = 10
It says above that… “C++ does not define the order in which function parameters are evaluated.”
But in the section “Precedence and associativity” it shows that () “Parenthesis”, () “Function calls”, () “Implicit assignments”, and , “comma” have an associative of left to right… what is the difference?
I don’t know where to put this question so I will just put it here.
I wanted to create a function that would return the Oct number of a char value. So, considering the pattern of the Oct numbers in relation to the ASCII codes, I created the following code:
int Oct() { char OctC = 0; int NOct = 0; while (OctC != Data) { if ((NOct - 77) % (100) == 0) NOct = NOct + 33; else if ((NOct - 7) % (10) == 0) NOct = NOct + 3; else ++NOct; ++OctC; } return NOct; }The OctC value being the ASCII code of Data (which is a global variable), and NOct being the Oct number of Data, will this function return a proper result? Even if it does, is there a easier way or a shortcut to do this (such as a function in C++ that will do the job for you) because I’m only 13 and this took me a while to figure out.
I’m sorry, but the place I wrote
in the 8th line was actually meant to be
.
hi Alex!
I tried to test the precedence of the ++ operator, both prefix and postfix.
The code i typed in is:
#include <iostream>; int main() { using namespace std; int x = 1; int y = (++x++); cout << "x= "<<x<<endl; cout << "++x++= "<<y; } I have the following error: Line 7: non-lvalue increment.Why the is ++x++ illicit?
Thanks for the nice tutorial.
oh no . complecated. need to learn easiest way.
Hi, I teach C++ to 12th graders. I encountered this problem in the lab wherein the statement
I would expect the value 13, but my students got 14 as the output. any pointers?
what is funny is that the following code gives the value 13.
The only difference being in the way x is initialized. I am baffled!!!
answer to the second code snippet is also = 14
Remember that operator precedence requires that ++ be evaluated before + and =. So what happens is ++y is evaluated twice with y becoming 6 then 7. The addition 7+7 is then performed before finally assigning result 14 to x.
Hi,
I am a XII grade student. I happened to encounter the same problem
as Smitha ma’m. I too got 14 for the first case and 13 for second case
Moreover, I repeated trying for various combinations and i feel that
all pre-increment operators are evaluated before evaluating the
expression and all post-increment operators after evaluating the
entire expression in the first case.
In case of dynamic initialisation, I got the values incremented
in between evaluation of the expression.
int x= ++y + ++y
evaluted as 6+7 =13
but
int x;
x= ++y + ++y
evaluated as 7+7=14
Am I right? Pls tell me why this is so.
Thanks in advance.
this also depends upon the compiuer. turbo c compiler will give answer 14 in both cases which is infact correct.