What Is Increment And Decrement Operators In C Language?

What Is Increment And Decrement Operators In C Language?


Hello Programmers,

Today we will discuss Increment and Decrement operators in c language. These operators are very easy to use and these are very important in c language. In the last blog we studied assignment and conditional operators but today we will discuss one of the most important operators that are increment and decrement operators.


Increment and Decrement operators are very useful operators in c language. Increment operators are (++) and decrement operators are (--). 
As the name suggests increment operators(++) add 1 to the operand or increase value 1 in operand while the decrement operators (--) decrease 1 to the operand or decrease value 1 in the operand.

Example:     i++; is equivalent to i=i+1;
                    j-- ; is equivalent to j=j-1;

int n=5;
here the value of n is 5 
n++;
after incrementing the value of n is 6.


int m=6;
here the value of m is 6
m--;
after decrementing the value of m is 5.

In c language, there is 2 form of increment and decrement operators.
In the above example, you see the postfix form of increment and decrement operators. 

Now you will see the prefix form of increment and decrement operators.


Example:    ++i; is equivalent to i=i+1;
                    -- j; is equivalent to j=j-1;


The major difference between prefix and postfix forms shown up when the operators are used as part of a large expression.

whenever you use ++j in an expression, the value of j is increment before the expression is evaluated and whenever you use j++ the value of j is increment after the expression is evaluated.

Example:  Let assume the value of variable a and b have been initialized to zero.

Now...

Example of prefix:

a = ++m + ++n; here the value of m->1, n->1 and a->2

Example of postfix:

a = m++ + n++; here the value of a->0 them m->1 and n->1

Increment and Decrement operators are very useful and most important operators.


Sharing Is Caring...     

Comments :

Post a Comment