May 03, 2018

Increment and Decrement Operators in C


   The increment(++) and decrement(--) operators are unary operators that increment and decrement value of a variable by 1.




     ++x    is equivalent to    x = x + 1
      - -x    is equivalent to    x = x - 1


Increment and decrement operators must be used only with a variable not with constants or expressions. For example:

int x = 1, y = 1, z = 1;

++x; /* valid - increment operator operating on a variable */

++7 /* invalid - increment operator operating on a constant value */

++(x-y+z) /* invalid - increment operator operating on an expression */




Increment/Decrement operators are of two types: 

  • Prefix increment/decrement operator
  • Postfix increment/decrement operator



Increment/Decrement Operators


Prefix Increment/Decrement Operator

In Prefix increment/decrement operator, first the value of variable is incremented/decremented then the new value is used in the operation. 


Example: Prefix Increment

Let's take a variable x whose value is 5.

The statement:

y = ++x;

means first increment the value of x by 1 then assign the value of x to y.  This single statement is equivalent to these two statements:

x = x + 1;
y = x;

Now value of x is 6 and value of y is also 6.


Example: Prefix Decrement

Let's take a variable x whose value is 5.

The statement:

y = --x;

means first decrement the value of x by 1 then assign the value of x to yThis single statement is equivalent to these two statements:

x = x - 1;
y = x;

Now value of x is 4 and value of y is also 4.

The following program demonstrates prefix increment/decrement operator.

#include <stdio.h>

int main(void)
{
    int x = 5, y = 1;

    printf("Initial value of x = %d\n", x); /* Print the initial value of x */
    printf("Initial value of y = %d\n\n", y); /* Print the initial value of y */

       
    y = ++x; /* Increment the value of x by 1 then assign the value of x to y */

       
    printf("After incrementing by 1: x = %d\n", x);
    printf("Value of y = %d\n\n", y);

    y = --x; /* Decrement the value of x by 1 then assign the value of x to y */

       
    printf("After decrementing by 1: x = %d\n", x);
    printf("Value of y = %d\n\n", y);

       
    return 0;
}

Output:

Initial value of x = 5
Initial value of y = 1

After incrementing by 1: x = 6
Value of y = 6

After decrementing by 1: x = 5
Value of y = 5



Postfix Increment/Decrement operator

In postfix Increment/Decrement operator, first the value of variable is used in the operation and then increment/decrement takes place.


Example: Postfix Increment

Let's take a variable x whose value is 5.

The statement:

y = x++;

means first the value of x is assigned to y and then x is incremented. This single statement is equivalent to these two statements:

y = x;
x = x + 1;

Now value of x is 6 and value of y is 5.


Example: Postfix Decrement

Let's take a variable x whose value is 5.

The statement:

y = x--;

means first the value of x is assigned to y and then x is decremented. This single statement is equivalent to these two statements:

y = x;
x = x - 1;

Now value of x is 4 and value of y is 5.

The following program demonstrates postfix increment/decrement operator.


#include <stdio.h>

int main(void)
{
    int x = 5, y = 1;

    printf("Initial value of x = %d\n", x); /* Print the initial value of x */
    printf("Initial value of y = %d\n\n", y); /* Print the initial value of y */

       
    y = x++; /* First assign the value of x to y and then increment the value of x by 1 */

       
    printf("After incrementing by 1: x = %d\n", x);
    printf("Value of y = %d\n\n", y);

    y = x--; /* First assign the value of x to y and then decrement the value of x by 1  */

       
    printf("After decrementing by 1: x = %d\n", x);
    printf("Value of y = %d\n\n", y);

       
    return 0;
}

Output:

Initial value of x = 5
Initial value of y = 1

After incrementing by 1: x = 6
Value of y = 5

After decrementing by 1: x = 5
Value of y = 6


next    Relational Operators in C
top    Index
prev    Arithmetic operators in C 



No comments:

Post a Comment