Published by: Nuru
Published date: 21 Jun 2021
Increment & Decrement Operators are as follows;
x = x + 1; y = y - 1;
x = 10; y = ++x;
Here value of x is incremented first i.e. x becomes 11 and the incremented value is assigned to y.
x = 10; y = x++;
Some examples of Increment & Decrement Operators are as follows;
#include
int a, b;
main()
{
/* Set a and b both equal to 5 */
a = b = 5;
/* Print them, decrementing each time. */
/* Use prefix mode for b, postfix mode for a */
printf("\n%d %d", a--, --b);
printf("\n%d %d", a--, --b);
printf("\n%d %d", a--, --b);
printf("\n%d %d", a--, --b);
printf("\n%d %d\n", a--, --b);
return 0;
}
#include
#include
void main()
{
int y, m=5, x, l=5;
clrscr();
y=++m; /*A prefix operator first adds 1 to the the operand and the result is assigned to variable on left,*/
printf("\n %d",m);
printf("\n %d",y);
x=l++; /*A postfix operator first assigns the value to the variable on left, and then adds 1 to the operand*/
printf("\n %d",l);
printf("\n %d",x);
getch();
}