# Increment and Decrement Operators in C

C has two two special unary operators called increment (`++`

) and decrement (`--`

) operators. These operators increment and decrement value of a variable by `1`

.

`++x`

is same as `x = x + 1`

or `x += 1`

`--x`

is same as `x = x - 1`

or `x -= 1`

Increment and decrement operators must be used only with a variable not with constants or expressions.

```
int x = 1, y = 1;
++x; // valid
++5 // invalid - increment operator operating on a constant value
++(x+y) // invalid - increment operating on an expression
```

## Increment/Decrement operators are of two types: #

- Prefix increment/decrement operator.
- Postfix increment/decrement operator.

Let's start with the first one.

### Prefix increment/decrement operator #

In Prefix increment/decrement operator first, the value of the variable is incremented/decremented then the new value is used in the operation. Let's take an example:

The statement:

```
y = ++x;
```

means that first the increment the value of `x`

then assign this value to `y`

.

Similarly, the statement:

```
y = --x;
```

means that first the decrement the value of `x`

then assign this value to `y`

.

The following program demonstrates prefix increment/demonstrates operator.

```
#include<stdio.h>
int main()
{
int x = 12, y = 1;
printf("Initial value of x = %d\n", x); // print the initial value of y
printf("Initial value of y = %d\n\n", y); // print the initial value of y
y = ++x; // increment the value of x by 1 then assign this new value to y
printf("After incrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
y = --x; // decrement the value of x by 1 then assign this new value to y
printf("After decrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
// Signal to operating system everything works fine
return 0;
}
```

**Expected Output:**

```
Initial value of x = 12
Initial value of y = 1
After incrementing by 1: x = 13
y = 13
After decrementing by 1: x = 12
y = 12
```

## Postfix Increment/Decrement operator #

In postfix Increment/Decrement operator, first the value of the variable is used in the operation then increment/decrement takes places. For e.g:

The statement:

```
y = x++;
```

means that first the value of `x`

is assigned to `y`

then `x`

is incremented.

Similarly, the statement:

```
y = x--;
```

means that first the value of `x`

is assigned to `y`

then `x`

is decremented.

The following program demonstrates postfix increment/demonstrates operator.

```
#include<stdio.h>
int main()
{
int x = 12, y = 1;
printf("Initial value of x = %d\n", x); // print the initial value of y
printf("Initial value of y = %d\n\n", y); // print the initial value of y
y = x++; // first assign the value of x to y, then increment it by 1
printf("After incrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
y = x--; // first assign the value of x to y, then decrement it by 1
printf("After decrementing by 1: x = %d\n", x);
printf("y = %d\n\n", y);
// Signal to operating system everything works fine
return 0;
}
```

**Expected Output:**

```
Initial value of x = 12
Initial value of y = 1
After incrementing by 1: x = 13
y = 12
After decrementing by 1: x = 12
y = 13
```