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.

Increment/Decrement operators are of two types:

  1. Prefix increment/decrement operator.
  2. 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:

means that first the increment the value of x then assign this value to y.

Similarly, the statement:

means that first the decrement the value of x then assign this value to y.

The following program demonstrates prefix increment/demonstrates operator.

Expected Output:

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:

means that first the value of x is assigned to y then x is incremented.

Similarly, the statement:

means that first the value of x is assigned to y then x is decremented.

The following program demonstrates postfix increment/demonstrates operator.

Expected Output:

Leave a Comment