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:
In C programming language, if a = 0, the value of the expression (++a + a++) is 3, while as that of (a++ + ++a) is 2. Why?
Answer:
++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.


0 Comments