Ad Code

Pre-increment and Post-increment operator in C/C++

 Pre-increment and Post-increment in C/C++ Programming Language 

In this article we will briefly discuss about what is pre increment operator and post increment operator in C/C++ programming language.



Pre-increment operator: 

A pre-increment operator is used to increment the value of a variable before using it in a expression. In the Pre-Increment, value is first incremented and then used inside the expression.
Eg: b = ++a;

 


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the first number\n");
scanf("%d",&a);
b=++a;
printf("%d",a);
printf("\n%d",b);
getch();
}


Post-increment operator: 

A post-increment operator is used to increment the value of variable after executing expression. In the Post-Increment, value is first used in a expression and then incremented.
Eg: b=a++;

 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the first number\n");
scanf("%d",&a);
b=a++;
printf("%d",a);
printf("\n%d",b);
getch();
}
Let's understand with the proper example, what is the difference between pre increment and post increment operator.

Question:

++a is pre-increment

a++ is post-increment

Means in the case of pre-increment, the value of a will increase by 1, then it will be used in expression

and in the case of post-increment, the value of a will be used in expression first, then it will be increased by 1

Let's understand with the diagram:


Hope you will understand that what is that major difference between pre increment operator and post increment operator in C programming language.

Post a Comment

0 Comments

Ad Code