Pre and post incrementation and decrementation

Pre and post incrementation and decrementation

In C, we have increment and decrement operators.
Incrementation operators (++)
Decrementation operators (--)
Before starting program we must know that:
++a It is called pre incrementaion.
a++ It is called post incrementaion.
--a It is called pre decrementation.
a-- It is called post decrementation.

Program:

#include<stdio.h>
void main()
{
int a=0,b=0,c=0;
printf("a\tb\tc\n");
printf("%d\t%d\t%d\n",a,b,c);
a=++b + ++c;
printf("%d\t%d\t%d\n",a,b,c);
a=++b + c++;
printf("%d\t%d\t%d\n",a,b,c);
a=--b + c--;
printf("%d\t%d\t%d\n",a,b,c);
z=b-- + c--;
printf("%d\t%d\t%d\n",a,b,c);
a=-a;
printf("%d\t%d\t%d\n",a,b,c);
}

Output:




Explanation:
Using --b firstly value of b is decremented and then further operations are carried on.
Using b-- firstly operation is carried out and then value of b is decremented.
Same logic for pre and post incrementation too.

No comments:

Post a Comment