Increment Operators in C | Pre increment and Post Increment operators in C Language
Increment Operators in C Language:
- The Increment Operators – Increments the Value of the variable by 1( by adding 1 to it’s Current Value ).
- Increment Operator is an Unary operator. That means Increment operator is operates on only one Operand.
- Increment Operator have Highest priority than all Binary Operators. Because Unary operators have higher priority than Binary operators. You can see Full priority table here C operators Priority table.
- Increment Operator won’t work on Constants. It can be apply only on Variables.
- Increment Operator Denoted by ++ Symbol.
- Ex : ++X or X++.
Different Types of Increment operators in C :
- pre-increment operator (or) prefix increment operator.
- post-increment operator (or) postfix increment operator.
Pre-incremet Operator :
- In pre-increment operator, Operator is written before the operand.
- Pre-increment operator Increments the value of variable first, then incremented value is used to evaluate the Expression.
-
a= 5;
-
y = ++a;
Post-Increment Operator :
- In post-increment operator, Operator is written after the Operand.
- Post-increment operator first expression is Evaluated and then value of Variable is Incremented. that means Incremented value is not used in expression.
-
a= 5;
-
y = a++;
Example program 1 : C program to Understand Increment operators
-
#include<stdio.h>
-
int main(void)
-
{
-
int x=8,y=10;
-
printf(“x=%dt“,x);
-
printf(“x=%dt“,++x); /*Prefix increment*/
-
printf(“x=%dt“,x);
-
-
printf(“y=%dt“,y);
-
printf(“y=%dt“,y++); /*Postfix increment*/
-
printf(“y=%dt“,y);
-
return 0;
-
}
Program Output :
1 2 |
x=8 x=9 x=9 y=10 y=10 y=11 |
Example 2 : program to understand Increment Operator in C
-
#include<stdio.h>
-
void main()
-
{
-
int a,b,c=5,d=5;
-
a = c++;
-
b = ++d;
-
printf(“Value of a : %d”,a);
-
printf(“Value of b : %d”,b);
-
printf(“Value of c : %d”,c);
-
printf(“Value of d : %d”,d);
-
}
Program Output :
1 2 3 4 |
Value of a : 5 Value of b : 6 Value of c : 6 Value of d : 6 |
Example 3 : Increment Operators can not be used on Constants
-
#include<stdio.h>
-
void main()
-
{
-
int a;
-
a = 10++;
-
}
Program Output :
1 2 |
prog.c: In function ‘main’: prog.c:5:8: error: lvalue required as increment operand |
Also Read :
- Start Here – Step by step tutorials to Learn C programming Online with Example Programs
- Modulo operator in C explained with Examples.
- In-depth examples on Arithmetic operators.
- Arithmetic operators in C language.
- Compilation Stages in C language.
- Identifiers in C language and Rules for naming Identifiers.
- Structure of C Language.
Increase value vs address
Increase value vs address
#include
int main(void)
{
int *p, j;
p = &j;
j = 1;
printf("%p ", p);
(*p)++; /* now j is incremented and p is unchanged */
printf("%d %p", j, p);
return 0;
}