Incrementing in C++ - When to Use X++ or ++X

Incrementing in C++ - When to use x++ or ++x?

It's not a question of preference, but of logic.

x++ increments the value of variable x after processing the current statement.

++x increments the value of variable x before processing the current statement.

So just decide on the logic you write.

x += ++i will increment i and add i+1 to x.
x += i++ will add i to x, then increment i.

Difference between &++x and &x++

In this declaration

int *p = &++x;

there are used two unary operators: pre-increment ++ and taking of address. Unary operators are executed from right to left. So at first the variable x is incremented and its address is assigned to the pointer p, The result of the pre-increment operator is lvalue of the incremented object.

So for example such an expression like this

++++x;

is correct.

In this declaration

int *p = &x++;

there are used the postfix operator post-increment ++ and the unary operator of taking address. Postfix operators have a higher priority relative to unary operators. SO at first the post-increment is executed. Its result is a temporary object that has the value of the variable x before incrementing. Then the operator of taking of address is executed.

However you may not take an address of a temporary object. So for this declaration the compiler will issue an error.

Opposite to the pre-increment operator such an expression like this

x++++;

is invalid.

From the C++ 17 Standard (5.3.2 Increment and decrement)

1 The operand of prefix ++ is modified by adding 1, or set to true if
it is bool (this use is deprecated). The operand shall be a modifiable
lvalue. The type of the operand shall be an arithmetic type or a
pointer to a completely-defined object type. The result is the
updated operand; it is an lvalue
, and it is a bit-field if the
operand is a bit-field....

And (5.2.6 Increment and decrement)

1 The value of a postfix ++ expression is the value of its operand. [
Note: the value obtained is a copy of the original value — end note
]...

In C the both operations yield a value. So in C you also may not write

++++x;

Why the value of X is not incremented while assignment

Make yourself aware of sequence point. From this [emphasis mine]:

There is a sequence point after the evaluation of all function arguments and of the function designator, and before the actual function call.

From this [emphasis mine]:

Increment operators initiate the side-effect of adding the value 1 of appropriate type to the operand. Decrement operators initiate the side-effect of subtracting the value 1 of appropriate type from the operand. As with any other side-effects, these operations complete at or before the next sequence point.

Looks at this statement:

x = printf("%d", x++);

The post increment operator increase the value of operand by 1 but the value of the expression is the operand's original value prior to the increment operation.

So, the value of x passed to printf() will be its original value which is 12 and due to sequence point, before calling printf() the value of x will be incremented by 1. The return value of printf() will be assigned to x which overwrites the last value of x which is the incremented value due to post ++ operator. Hence, after this statement the value of x is 2.

Understanding the increment operator in C

In i++, the value of i is changed. After execution, i's value is one plus its previous value. You can't store a value in w+x, though, and so you can't do any of the following, which all (if they worked) would have more or less the same effect:

w+x = w+x+1;
w+x += 1;
(w+x)++;

Something that can be placed on the left hand side of an assignment is typically called an lvalue (l is for left). The concise way of saying this is that ++ can only be applied to lvalues, and w+x isn't an lvalue. You can read more about lvalues (and other kinds of values) in this question and its answers:

  • What are rvalues, lvalues, xvalues, glvalues, and prvalues?

How does the increment operator work in an if statement?

In C, 0 is treated as false. In x++, the value of x, i.e, 0 is used in the expression and it becomes

if(0)  // It is false
printf("true\n");

The body of if doesn't get executed. After that x is now 1. Now the condition in else if, i.e, x == 1 is checked. since x is 1 , this condition evaluates to true and hence its body gets executed and prints "false".

Increment and Decrement Operators

simple explanation:

--x or ++x : Value will be modified after.

x-- or x++ : Value will be modified before

Detailed explanation:

--x or ++x: pre-decrement/increment: will first do the operation of decrementing or incrementing first, then it will assign x.

x-- or x++: post:decrement/increment: will first assign the value of x and then it will do the operation of decrementing or incrementing after.

lets write your code in a nicer format and go through your code step by step and annotate it to show you visually what happens:

main() {
//We declare the variables x, y and z, only x is given a value of 4.
int x=4,y,z;

//--x will decrement var x by 1 first THEN it will assign the value of x to y.
//so: x = 3, y = 3 and z = nothing yet.
y = --x;

//x-- will assign the value of x to z first, then var x will be decremented by 1 after.
//so: x = 2, y=3 and z = 3
z = x--;

printf ("\n %d %d %d", x,y,z);

}

Why is putting an arbitrary number of pre-increment/decrement operators together possible in C++?

Both prefix and postfix increment need a reference to the value to be incremented.

++x returns a reference to the (incremented) x, that's why prefix increment can be chained.

Postfix x++ however, increments x and returns a temporary copy of the preivous value - not a reference, so it cannot be chained.

Performance is not relevant here. ++++x and x+=2 are equivalent - some compiler might generate better code for one or the other, but that's not expected, and even then the difference will be usually negligible on most execution platforms.

While ++++x is legal, it will usually be considered bad style.


As for why other languages don't do that: I am not aware of an rationale, though "don't help programmers write weird code" might be plausible. It might also simply be a side effect of other rules. Be aware that e.g. in C#, the expression evaluation rules are notably different, e.g. x = x++ + 1; is undefine behavior in C and C++, but not in C#

RE: check for every X increment in C

I think you are looking for modulo operation (http://en.wikipedia.org/wiki/Modulo_operation):

void func(void){
static int i = 0;
int x = 13
if (i % x == 0)
{
do something
}
i++;
}


Related Topics



Leave a reply



Submit