This blog is under construction

Sunday, 22 April 2012

Increment and decrement operators in C

The increment operator (++) increments the value of its operand by 1.
     x = 10;
     x++;  // increments the value of x by 1.  So, the value of x is 11.

The decrement operator(--) decrements the value of its operand by 1;
     x = 10;
     x--;  // decrements the value of x by 1;  So, the value of x is 9.
 
Both the operators can be placed before or after the operand as shown above and they can be applied on primitive data types like char, int, float, double & they can also be used on pointers and enums.

Difference between postfix increment and prefix increment:
Example 1:
int a, b = 10;
a = b++;

Value of a is 10
Value of b is 11

In the above expression, the value of b is assigned to a first.  Then, 1 is added to the value of b.

Example 2:
int a, b = 10
a = ++b;

Value of a is 11
Value of b is 11

Here, the value of b is incremented(add 1 to b)  first.  Then, the incremented value of b is assigned to a.

Difference between prefix decrement and postfix decrement
Example 1:
int a, b = 10;
a = b--;

Value of a is 10
Value of b is 9

In the above expression, the value of b is assigned to a first.  Then, 1 is subtracted from the value of b.

Example 2:
int a, b = 10
a = --b;

Value of a is 9
Value of b is 9

Here, the value of b is decremented(subtract 1 from b) first.  Then, the decremented value of b is assigned to a.

Example C program on increment and decrement operators:
 
  #include <stdio.h>
  int main() {
        int x = 10, y, num;
        enum fruit {APPLE, ORANGE, BANANA, JACK};
        enum fruit f1 = APPLE;

        /*
         * incrementing the value of variable of type enum
         * and assigning the same to interger variable num
         */
        num = ++f1;
        printf("value of num is %d\n", num);
        printf("value of f1 is %d\n\n", f1);

        /* printing the original value of x */
        printf("Value of x before incrementing: %d\n", x);

        /* incrementing the value of x and printing its value */
        x++;
        printf("Value of x after incrementing: %d\n\n", x);

        /* printing the value of x before dementing */
        printf("Value of x before decrementing: %d\n", x);

        /* decrementing the value of x and priting its value */
        x--;
        printf("Value of x after decrementing: %d\n\n", x);
        printf("Tricks with increment/decrement operators\n");
        /*
          * assigns value of x to y first and
          * then increments the value of x
          */
        y = x++;
        printf("Value of x:%d\nValue of y: %d\n\n", x, y);
        y = 0;
       /*
         * increments x first and
         * then assigns the value of x to y
         */
        y = ++x;
        printf("Value of x: %d\nValue of y: %d\n\n", x, y);
        y = 0;
        /*
          * assigns the value of x to y first and
          * then decrements the value of x
          */
        y = x--;
        printf("Value of x:%d\nValue of y: %d\n\n", x, y);
        y = 0;
        /*
          * decrements the value of x first and
          * then assigns the value of x to y
          */
        y = --x;
        printf("Value of x: %d\nValue of y: %d\n\n", x, y);
        return 0;
  }


  Output:
  jp@jp-VirtualBox:~/cpgms/posts$ ./a.out
  value of num is 1
  value of f1 is 1

  Value of x before incrementing: 10
  Value of x after incrementing: 11

  Value of x before decrementing: 11
  Value of x after decrementing: 10

  Tricks with increment/decrement operators
  Value of x:11
  Value of y: 10

  Value of x: 12
  Value of y: 12

  Value of x:11
  Value of y: 12

  Value of x: 10
  Value of y: 10



No comments:

Post a Comment