In C Programming , Unary operators are having higher priority than the other operators. Unary Operators are executed before the execution of the other operators. Increment and Decrement are the examples of the Unary operator in C.
Increment Operator in C Programming
- Increment operator is used to increment the current value of variable by adding integer 1.
- Increment operator can be applied to only variables.
- Increment operator is denoted by ++.
Different Types of Increment Operation
In C Programming we have two types of increment operator i.e Pre-Increment and Post-Increment Operator.
A. Pre Increment Operator
Pre-increment operator is used to increment the value of variable before using in the expression. In the Pre-Increment value is first incremented and then used inside the expression.
b=++ y;
b=
In this example suppose the value of variable ‘y’ is 5 then value of variable ‘b’ will be 6 because the value of ‘y’ gets modified before using it in a expression.
B. Post Increment Operator
Post-increment operator is used to increment the value of variable as soon as after executing expression completely in which post increment is used. In the Post-Increment value is first used in a expression and then incremented.
b= x++;
b= x++;
In this example suppose the value of variable ‘x’ is 5 then value of variable ‘b’ will be 5 because old value of ‘x’ is used.
Sample program
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,x=10,y=10;
a=x++;
b=++y;
printf("value of a :%d",a);
printf("value of b :%d",b);
}
OUTPUT :
value of a :10
value of b :11