Press "Enter" to skip to content

Increment and Decrement Operators in C

 Program Abstraction:      This tutorial describes the Increment and Decrement Operators in C with examples.

Incrementing and decrementing a value is the function of increment and decrement operators respectively.
In assignment operator, we learnt how to assign a value for a variable.

 for example:
                     a = a+ 5;

           – It means value of a is the value of a plus 5 (a+5).
That is, the value of a is increment by 5.
Another example:
                            b = b + 1;
        -Here the value of b is the value of b plus 1.
Increment Operator 

Instead of writing b = b +1;  we can write like this:   b++; or ++b;
Both means b = b + 1;
So the symbols ++ denotes the increment operator.
Examples:
 
8++ means 8 = 8 + 1; that is 9.
10++ gets the value 11.
Also
++8 means 8 = 8 + 1; that is 9.
++10 gets the value 11
Then what is the different between 10++ and ++10 ? Let’s learn it’s answer with some subtopics.
Post-increment:

a++;
is an example of post increment.

If we assign the value of a++ to another variable b, then what will the value of b?

For example:

int a = 10;
int b;
b = a++;

Here what will the value of a and b?

Answer:

Value of a:

Initial value of a is 10, then a++ means a = a+1. There for a = 10 + 1, ie value of a is 11.

Value of b:

Here b = a++,  that is a++ was incremented as post increment. So b gets the value of a before increment (before the operation ++). So b gets the initial value of a. So the value of b is 10.

Pre-increment:

++c; is an example of pre increment operator.

If we assign the value of ++c to another variable d, then what will the value of d?

For example:

int c = 20;
int d;
d = ++c;

Here what will the value of c and d?

Answer:

Value of c:

Initial value of c is 20, and ++c means c = c+1. There for c = 20 + 1, so the value of c is 21.

Value of d:

Here d = ++c, that is  ++c was incremented as pre increment. So d gets the value of c after increment operation (after ++). So d gets the incremented value of c (c+1). So the value of c is 21.

How to Remember both Post Increment and Pre Increment without Interchanging?

Take this Example;

b = 2;
a = b++;

In line a = b++, right part is b++. First letter of b++ is b (not increment operator, so no need to increment). So a gets the value of b. ie 2.

b = 2;
a = ++b;

In line a = ++b, right part is ++b. First thing is ++ (increment operator,so we have to increment). So a gets the value of b after increment. ie 3;

Decrement Operator

Decrement operator is almost same as increment operator. Increment operator increment a value by one, whereas decrement operator decrement a value by one. The symbol — denotes the decrement operator.

–x; is an example for Increment operator.

Post-decrement and Pre-decrement:

These are like the post increment and pre increment. But this operator decrement a value by one.

Example for post decrement:

x = 3;
y = x–;
// now x is 2, and y still is 3

Example for pre decrement:

x = 3;
y = –x;
//now x is 2, and y is also 2

Mixed Program:
  #include<stdio.h>

 #include<conio.h>
  void main()
  {
   int x = 5;
   int y;
   clrscr();
   y = x–;
   printf(“x is %d and y is %d”,x,y);
   y = –x;
   printf(“n second part”);
   printf(“n x is %d and y is %d”,x,y);
   getch();
 }

Sample Output:

Post and Pre Decrement Increment operator in C
You can remember both Post decrement and Pre decrement without interchanging like the remembering method of Post increment and Pre increment mentioned above.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: