Wednesday, January 27, 2016

Increment and Decrement operators ( ++, --)

There are two special arithmetic operators called increment and decrement operators available C. They are unary operators unlike all other arithmetic operators. The operators are denoted as : '++' (increment operator), '--' (decrement operator).
OperatorNameDescriptionExample
++Increment OperatorIncrements the value of operand by 1a++ : This is equivalent to a = a + 1
--Decrement OperatorDecrements the value of operand by 1a-- : This is equivalent to a = a - 1

Variants of Increment and Decrement Operators - Pre Increment, Post Increment, Pre Decrement and Post Decrement

There are two different variants of increment and decrement operators known as pre increment, post increment, pre decrement and post decrement operators.
  • Pre Increment Operators: When '++' is used as prefix of the operand, it's called pre increment operator. Pre increment operator will increment the value of operand before using it in the expression.
  • Post Increment Operators: When '++' is used as postfix of the operand, it's called post increment operator. Post increment operator will increment the value of operand after using it in the expression. ie; The current expression will use non-incremented value of operand.
  • Pre Decrement Operators: When '--' is used as prefix of the operand, it's called pre decrement operator. Pre decrement operator will decrement the value of operand before using it in the expression.
  • Post Decrement Operators: When '--' is used as postfix of the operand, it's called post decrement operator. Post decrement operator will decrement the value of operand after using it in the expression. ie; The current expression will use non-decrement value of operand.

Examples of pre increment, post increment, pre decrement and post decrement operators are given below
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int a = 5;
  5. // Pre increment operator. Value of 'a' will be incremented first and then
  6. // used in the expression. So b is assigned value after a gets incremented.
  7. int b = ++a;
  8. printf("a = %d, b = %d\n", a, b);
  9. a = 5;
  10. // Post increment operator. The value of 'a' will be incremented only after
  11. // it gets used in the expression. That means, first b will get assigned the
  12. // value of 'a' and then value of 'a' will be incremented .
  13. b = a++;
  14. printf("a = %d, b = %d\n", a, b);
  15. a = 5;
  16. // Pre decrement operator. Similar to pre increment operator, pre decrement
  17. // operator decrements the value of operand before using it in the expression.
  18. b = --a;
  19. printf("a = %d, b = %d\n", a, b);
  20.  
  21. a = 5;
  22. // Post decrement operator. Similar to post increment operator, post decrement
  23. // operator decrements the value of operand after using it in the expression.
  24. b = a--;
  25. printf("a = %d, b = %d\n", a, b);
  26. return 0;
  27. }
  28.  
  29. Output:
  30. a = 6, b = 6
  31. a = 6, b = 5
  32. a = 4, b = 4
  33. a = 4, b = 5

No comments: