{ A destination to learn coding }

Operators


Increment and Decrement Operators

These operators are used to either add or subtract 1 unit from the value.

Increment Operator

Syntax:
a++;

Decrement Operator

Syntax:
a--;

a++ is same as a=a+1 and a-- is same as a=a-1

Try This
#include<stdio.h>
int main()
{
int a=1;
printf("%d\n",a);

a++;
printf("%d\n",a);

a--;
printf("%d\n",a);


return 0;
}
		

pre and post Increment/Decrement

a++ is post increment
++a is pre increment

a++ and ++a are same as a=a+b;

but b=a++ and b=++a are not same

Look at the example below
#include<stdio.h>
int main()
{
 int a=5,b;
 b=a++;    /*line one*/
 printf("%d\n",b);
 printf("%d\n",a);

 b=++a;    /*line two*/
 printf("%d\n",b);
 printf("%d\n",a);

 return 0;
}
		

On line one, b gets the value a and then a is incremented. Thats why the value of b is 5 and that of a is 6(after incrementation)

Now a (having value as 6) is incremented again on line two. This time, b gets the incremented value because a is pre-incremented. That is why the value of both a and b is 7.
post and pre decrement works the same way.

Try the example below
#include<stdio.h>
int main()
{
int a=20,b;
b=a--;
printf("Value of a is %d",a);
printf("Value of b is %d",b);

b=--a;
printf("Value of a is %d",a);
printf("Value of b is %d",b);

return 0;
}	
C Codes