Increment and Decrement Operators in C Language


Increment and Decrement Operators :

Increment Operator is represented by ++. It increment the operand by 1.
Decrement Operator is represented by --. It decrements the operand by 1.

Operator Meaning
a++ post-increment
++a pre-increment
a-- post-decrement
--a pre-decrement
Note :
Increment and Decrement operators can not be applied on floating points.

Rules for ++ and -- operators :

  • Increment and Decrement operators are unary operators and require variables as operands.
  • When postfix ++ (or --) is used with a variable in an expression, the expression is evaluated first using the original value of the variable and then the variable is incremented (or decremented) by one.
  • When prefix ++ (or --) is used with a variable in an expression, the variable is incremented (or decremented) first and then the expression is evaluated using the new value of the variable.
  • The precedence and associativity of ++ and -- operators are the same as those of unary + and unary -.

We use the increment and decrement statements in for and while loops extensively. These two operators are unary operators, they operate on only one operand.
They follow right to left associativity.

Examples :
1. post - increment :
     int a = 8;
     y = a++;
     The value of y will be 8.
2. pre - increment :
     int a = 8;
     y = ++a;
     The value of y will be 9.
3. post - decrement :
     int a = 5;
     y = a--;
     The value of y will be 5.
4. pre - decrement :
     int a = 5;
     y = --a;
     The value of y will be 4.
0 Komentar untuk "Increment and Decrement Operators in C Language"

Back To Top