C – Increment/decrement Operators

C – Increment/decrement Operators


  • Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.
  • Syntax:
    Increment operator: ++var_name; (or) var_name++;
    Decrement operator: – -var_name; (or) var_name – -;
  • Example:
    Increment operator :  ++ i ;    i ++ ;
    Decrement operator :  – – i ;   i – – ;

EXAMPLE PROGRAM FOR INCREMENT OPERATORS IN C:

In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator and output is displayed as “1 2 3 4 5 6 7 8 9”.

OUTPUT:

1 2 3 4 5 6 7 8 9

EXAMPLE PROGRAM FOR DECREMENT OPERATORS IN C:

In this program, value of “I” is decremented one by one from 20 up to 11 using “i–” operator and output is displayed as “20 19 18 17 16 15 14 13 12 11”.

OUTPUT:

20 19 18 17 16 15 14 13 12 11

DIFFERENCE BETWEEN PRE/POST INCREMENT & DECREMENT OPERATORS IN C:

Below table will explain the difference between pre/post increment and decrement operators in C programming language.
Operator
Operator/Description
Pre increment operator (++i)value of i is incremented before assigning it to the variable i
Post increment operator (i++)value of i is incremented after assigning it to the variable i
Pre decrement operator (–i)value of i is decremented before assigning it to the variable i
Post decrement operator (i–)value of i is decremented after assigning it to variable i

EXAMPLE PROGRAM FOR PRE – INCREMENT OPERATORS IN C:


OUTPUT:

1 2 3 4
  • Step 1 : In above program, value of “i” is incremented from 0 to 1 using pre-increment operator.
  • Step 2 : This incremented value “1” is compared with 5 in while expression.
  • Step 3 : Then, this incremented value “1” is assigned to the variable “i”.
  • Above 3 steps are continued until while expression becomes false and output is displayed as “1 2 3 4”.

EXAMPLE PROGRAM FOR POST – INCREMENT OPERATORS IN C:

OUTPUT:

1 2 3 4 5
  • Step 1 : In this program, value of  i “0” is compared with 5 in while expression.
  • Step 2 : Then, value of “i” is incremented from 0 to 1 using post-increment operator.
  • Step 3 : Then, this incremented value “1” is assigned to the variable “i”.
  • Above 3 steps are continued until while expression becomes false and output is displayed as “1 2 3 4 5”.

EXAMPLE PROGRAM FOR PRE – DECREMENT OPERATORS IN C:

OUTPUT:

9 8 7 6
  • Step 1 : In above program, value of “i” is decremented from 10 to 9 using pre-decrement operator.
  • Step 2 : This decremented value “9” is compared with 5 in while expression.
  • Step 3 : Then, this decremented value “9” is assigned to the variable “i”.
  • Above 3 steps are continued until while expression becomes false and output is displayed as “9 8 7 6”.

EXAMPLE PROGRAM FOR POST – DECREMENT OPERATORS IN C:

 OUTPUT:

9 8 7 6 5
  • Step 1 : In this program, value of  i “10” is compared with 5 in while expression.
  • Step 2 : Then, value of “i” is decremented from 10 to 9 using post-decrement operator.
  • Step 3 : Then, this decremented value “9” is assigned to the variable “i”.
  • Above 3 steps are continued until while expression becomes false and output is displayed as “9 8 7 6 5”. 

Comments