Increment/decrement Operators in C Language


  • 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 language:

    • 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 language:

    • 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  language:

    • Below table will explain the difference between pre/post increment and decrement operators in C.
 S.no
Operator type
Operator
Description
1
Pre increment++i
Value of i is incremented before assigning it to variable i.
2
Post-incrementi++
Value of i is incremented after assigning it to variable i.
3
Pre decrement– –i
Value of i is decremented before assigning it to variable i.
4
Post_decrementi– –
Value of i is decremented after assigning it to variable i.

 Example program for pre – increment operators in C language:

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 language:

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 language:

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 language:

 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″. 

Continue on types of C language operators:

S.no
Types of Operators
             Description              
1
Arithmetic_operatorsThese are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus
2
Assignment operatorsThese are used to assign the values for the variables in C programs.
3
Relational operatorsThese operators are used to compare the value of two variables.
4
Logical operatorsThese operators are used to perform logical operations on the given two variables.
5
Bit wise operatorsThese operators are used to perform bit operations on given two variables.
6
Conditional(ternary) operatorsConditional operators return one value if condition is true and returns another value is condition is false.
7
Increment/decrement operatorsThese operators are used to either increase or decrease the value of the variable by one.
8
Special operators&, *, sizeof( ) and ternary operators
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

No comments: