Assignment Operator
The assignment operator is used to store a value in a variable.
It takes the value on the right side and stores it in the variable on the left side.
int main()
{
int x=10;
int y=x;
printf("x=%d\t y=%d",x,y);
}
o/p:
x=10 y=10
Compound assignment operator
+=
First, it adds the value of the operand x to the right of the operator and the value of the operand y and stores the resulting value in the variable x to the left of the operator.
int main()
{
int x=10;
int y=5;
x+=y;
printf("Value of (x+=y)=%d",x);
}
-=
int main()
{
int x=10;
int y=5;
x-=y;
printf("Value of (x-=y)=%d",x);
}
/=
int main()
{
int x=10;
int y=5;
x/=y;
printf("Value of (x/=y)=%d",x);
}
*=
int main()
{
int x=10;
int y=5;
x*=y;
printf("Value of (x*=y)=%d",x);
}
%=
First, the value of the x operand on the right side of the assignment operator is divided by the value of the y operand and the resulting value is stored in the x variable on the left side of the assignment operator.
Here two operations take place. One is division operation and the other is assignment operation. First, division operation takes place.
int main()
{
int x=10;
int y=5;
x%=y;
printf("Value of (x%=y)=%d",x);
}