In programming it may happen that you need to increase or decrease value of a variable by one. Increment and decrement operator are used for that purpose. Let’s take “a” as an integer variable, in Increment Operator it is written as “a++” or “++a” which have same mining as “a=a+1”. In Decrement Operator it is written as “a--” or “--a” which have same mining as “a=a-1”.
Now when we write “++a” called as Prefix Increment and for “a++” it is called as Postfix Increment. Prefix Increment make immediate increment of variable value where postfix make increment in next call.
Same way when we write “--a” called as Prefix Decrement and for “a--” it is called as Postfix Decrement. Prefix Decrement make immediate decrement of variable value where postfix make decrement in next call.
Now let’s see the example
Here for “a--” the value is not decreased in first call but in next call it’s decrease by 1. Now for “--a“ the value is increased in first call and in next call it show same result.
Now when we write “++a” called as Prefix Increment and for “a++” it is called as Postfix Increment. Prefix Increment make immediate increment of variable value where postfix make increment in next call.
Same way when we write “--a” called as Prefix Decrement and for “a--” it is called as Postfix Decrement. Prefix Decrement make immediate decrement of variable value where postfix make decrement in next call.
Now let’s see the example
#include<stdio .h>
void main()
{
int a;
a=1;//lets take value of a as 1
printf("The Value Of a is %d\n",a++);
printf("The Value Of a is %d\n",a);
a=1;//reset the value of a
printf("The Value Of a is %d\n",++a);
printf("The Value Of a is %d\n",a);
a=1;//reset the value of a
printf("The Value Of a is %d\n",a--);
printf("The Value Of a is %d\n",a);
a=1;//reset the value of a
printf("The Value Of a is %d\n",--a);
printf("The Value Of a is %d\n",a);
}
To Know how increment operator works we must see outputvoid main()
{
int a;
a=1;//lets take value of a as 1
printf("The Value Of a is %d\n",a++);
printf("The Value Of a is %d\n",a);
a=1;//reset the value of a
printf("The Value Of a is %d\n",++a);
printf("The Value Of a is %d\n",a);
a=1;//reset the value of a
printf("The Value Of a is %d\n",a--);
printf("The Value Of a is %d\n",a);
a=1;//reset the value of a
printf("The Value Of a is %d\n",--a);
printf("The Value Of a is %d\n",a);
}
The Value Of a is 1
The Value Of a is 2
The Value Of a is 2
The Value Of a is 2
The Value Of a is 1
The Value Of a is 0
The Value Of a is 0
The Value Of a is 0
Here for “a++” the value is not increased in first call but in next call it’s increase by 1. Now for “++a“ the value is increased in first call and in next call it show same result.The Value Of a is 2
The Value Of a is 2
The Value Of a is 2
The Value Of a is 1
The Value Of a is 0
The Value Of a is 0
The Value Of a is 0
Here for “a--” the value is not decreased in first call but in next call it’s decrease by 1. Now for “--a“ the value is increased in first call and in next call it show same result.

0 comments:
Post a Comment