The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.
Assignment Operator is used to assigning the result of an expression to a variable. we have seen the usual Assignment Operator " =".
Example
Here the value of a + b is evaluated and substituted to the variable x
Operator |
Description |
Example |
= |
Assignment operators |
a=10; |
In addition, C has a set of shorthand assignment operators of the form.
var oper = exp;
Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as the shorthand assignment operator.
x + = 1 is same as x = x + 1
The commonly used shorthand assignment operators are as follows
Shorthand assignment operators
Statement with simple assignment operator |
Statement with shorthand operator |
a = a + 1 |
a += 1 |
a = a – 1 |
a -= 1 |
a = a * (n+1) |
a *= (n+1) |
a = a / (n+1) |
a /= (n+1) |
a = a % b |
a %= b |