Tuesday, April 8, 2014

Increment and Decrement Operators in C

Increment Operators
  • Symbol ++
  • It is unary operator.
  • It is used to increase the value of variable by 1.
  • It can be used before and after the variable.
  • It can be used with constants and expression .only the variables van is incremented.
  1.  Post-fix increment operator.
  2.  Pre-fix increment operator.

Post-fix increment operator
  • When ++ is follows its operand (variable), it is called post-fix increment operator.
  • Post-fix increase the value of variable after the execution of the statement it first uses the current value of variable in the statement and after the completion of the statement it increases the value by one.

Example:
int a=100,b;
b=a++;
Output
a: 101
b: 100
Decrement Operator

  • Symbol - -
  • It is unary operator.
  • It is use to decrease the value of the variable by one.
  • It can be used before or after the variable name.
  • It cannot be used with constants and expressions. Only the variables can be decremented.

There are two types of decrement operator

  1. Postfix Decrement Operator
  2. Prefix Decrement Operator

Postfix Decrement Operator

  • When - -is follows its operand, it is called postfix decrement operator.
  • In postfix decrement, - - decrease the value of variable after the execution of the statement
  • i.e . It first uses the current value of the variable in the statement and after the completion of the statement it decreases the value by one.

Example:
int a=100,b;
b=a--;
Output
a: 99
b: 100
Postfix Decrement Operator

  • When - - is precedes its operand, it is called prefix decrement, - -decreases the value of the variable before the execution of the statement i.e. it first decreases the current value of the variable by one and then new value is used in the statement.

Example:
int a=100,b;
b=--a;
Output
a: 99
b: 99

Related Posts by Categories

0 comments:

Post a Comment