# Increment and Decrement Operators in C

Increment and decrement operators in C – C language contains two unary operators referred to as increment (++) and decrement (–) operators.

## The two unary arithmetic operators in C

1. Increment operator (++)
2. Decrement operator (- -)

The increment operator increments the variable by one and decrement operator decrements the variable by one. These operators can be written in two forms i.e. before a variable or after a variable.

These operators can be written in two forms i.e. before a variable or after a variable.

If an increment/decrement operator is written before a variable, it is referred to as pre-increment / pre-decrement operators and if it is written after a variable, it is referred to as post-increment / post-decrement operator.

For example,

a++ or ++a is equivalent to a = a+1 and
a– or – -a is equivalent to a = a -1

The importance of pre and post operator occurs while they are used in the expressions.

Pre-incrementing (Predecrementing) a variable causes the variable to be incremented (decremented) by 1, then the new value of the variable is used in the expression in which it appears.

Post-incrementing (postdecrementing) the variable causes the current value of the variable is used in the expression in which it appears, then the variable value is incremented (decrement) by 1.

The explanation is given in the table below:

 Increment and decrement operators in C Expression Explanation ++a Increment a by 1, then use the new value of a a++ Use value of a, then increment a by 1 –b Decrement b by 1, then use the new value of b b– Use the current value of b, then decrement by 1

The precedence of these operators is right to left. Let us consider the following examples:

Let us consider the following examples:

int a = 2, b=3;
int c;
c = ++a – b- -;
printf (“a=%d, b=%d,c=%d\n”,a,b,c);

OUTPUT
a = 3, b = 2, c = 0.

Since the precedence of the operators is right to left, first b is evaluated, since it is a post-decrement operator, current value of b will be used in the expression i.e. 3 and then b will be decremented by 1.

Then, a pre-increment operator is used with a, so first a is incremented to 3. Therefore, the value of the expression is evaluated to 0.

Let us take another example,

int a = 1, b = 2, c = 3;
int k;
k = (a++)*(++b) + ++a – –c;
printf(“a=%d,b=%d, c=%d, k=%d”,a,b,c,k);

OUTPUT
a = 3, b = 3, c = 2, k = 6
The evaluation is explained below:
k = (a++) * (++b)+ ++a – –c
= (a++) * (3) + 2 – 2 step1
= (2) * (3) + 2 – 2 step2
= 6 final result