Discuss increment and decrement operators with example.

This answer is restricted. Please login to view the answer of this question.

Login Now

C has two special unary operators called increment (++) and decrement (--) operators. These operators increment and decrement value of a variable by 1.

++x is same as x = x + 1 or x += 1
--x is same as x = x - 1 or x -= 1

Increment and decrement operators can be used only with variables. They can’t be used with constants or expressions.

Increment/Decrement operators are of two types:

  1. Prefix increment/decrement operator.
  2. Postfix increment/decrement operator.

1. Prefix increment/decrement operator

The prefix increment/decrement operator immediately increases or decreases the current value of the variable. This value is then used in the expression. Let’s take an example:

y = ++x;

Here first, the current value of x is incremented by 1. The new value of x is then assigned to y. Similarly, in the statement:

y = --x;

The following program demonstrates prefix increment/decrement operator in action:

#include<stdio.h>

int main()
{
    int x = 12, 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 this new value to y

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

    y = --x; // decrement the value of x by 1 then assign this new value to y

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

    // Signal to operating system everything works fine
    return 0;
}

The output of above program is

Initial value of x = 12
Initial value of y = 1

After incrementing by 1: x = 13
y = 13

After decrementing by 1: x = 12
y = 12

2. Postfix Increment/Decrement operator

The postfix increment/decrement operator causes the current value of the variable to be used in the expression, then the value is incremented or decremented. For example:

y = x++;

Here first, the current value of x is assigned to y then x is incremented.

Similarly, in the statement:

y = x--;

the current value of x is assigned to y then x is decremented.

The following program demonstrates postfix increment/decrement operator in action:

#include<stdio.h>

int main()
{
    int x = 12, 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++; // use the current value of x then increment it by 1

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

    y = x--; // use the current value of x then decrement it by 1

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

    // Signal to operating system everything works fine
    return 0;
}

The output of above program is

Initial value of x = 12
Initial value of y = 1

After incrementing by 1: x = 13
y = 12

After decrementing by 1: x = 12
y = 13

 

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .