## Assignment Operators in C Languages

Assignment Operators : Assignment operators are used to assign the results of an expression to a variable. In addition to usual assignment operator, C has a set of shorthand assignment operators.

syntax for assignment operator : var = exp
syntax for shorthand assignment operator : var op = exp

where var is variable, exp is an expression and op is a C binary arithmetic operator. The operator op = is known as the shorthand assignment operators.

the assignment t statement : var = var op exp is equivalent to : var op = exp

Assignment Operator Shorthand Assignment Meaning of both operators
a = a + 1 a += 1 adds 1 to a and assigns the value to a
a = a - 1 a -= 1 subtracts 1 from a and assigns the value to a
a = a/(b + 5) a/= (b+5) divides a by b+5 and assigns the value to a
a = a * (b+5) a *= (b+5) multiplies a by b+5 and assigns the value to a

Invalid assignment statements : 2 = 5 + 3; 2 = b;
Assignment operator is a binary operator.
Assignment operator follows right to left associativity.

The use of shorthand assignment operators has three advantages :

1. What appears on the left-hand side need not be repeated and therefore it becomes easier to write.
2. The statement is more concise and easier to read.
3. The statement is more efficient.
##### 0 Komentar untuk "Assignment Operators in C Languages" 