Wednesday, July 10, 2013

Compound Assignment Operator in C

We know what is Assignment Operator in C. Compound assignment Operator is the advancement of Assigning. Compound assignment operator is the short notation of an operation if the operation is performed on the variable itself.

For example, 
x+=1 means x=x+1
The first notation is compound assignment where as the second notation is the notation we already know. The following table gives you more types of compound assignment Operators.


Compound Notation
Normal Notation
x+=1
x=x+1
x-=2
x=x-2
x*=3
x=x*3
x/=4
x=x/4
x%=5
x=x%5

The above shown table gives you five different types of compound assignment operators. We can write the equations even more compound by mixing with a expression on the right side.

For example,
x+=y*z which means x=x+y*z;
Now, you might get a question that what happens if the equation is
x=5;
x*=2+3;
If we resolve the equation, the equation is x=x*2+3 as we know. Based on the Operator Precedence, the result is calculating the multiplication first and then the summation.

To know what happens, Click here