The Increment and Decrement Operators - C Operator

C examples for Operator:Increment Decrement Operator

Introduction

C increment and decrement operators ++ and -- simplify two common operations.

The operator ++ adds 1 to its operand, and -- subtracts 1. In other words:

x = x+1; is the same as ++x; and x = x-1; is the same as x--;

Both the increment and decrement operators may either precede (prefix) or follow (postfix) the operand.

For example, x = x+1; can be written ++x; or x++;

If an increment or decrement operator precedes its operand, the increment or decrement operation is done before obtaining the value of the operand for use in the expression.

If the operator follows its operand, the value of the operand is obtained before incrementing or decrementing it. For instance,

x = 10;
y = ++x;

sets y to 11. However, if you write the code as

x = 10;
y = x++;

y is set to 10. Either way, x is set to 11; the difference is in when it happens.

Here is the precedence of the arithmetic operators:


Highest              ++ --
                     - (unary minus)
                     * / %
Lowest               + -

Related Tutorials