Assignment operators are used to assign values to variables. The most common operator is the simple assignment operator (=), but C also provides several compound assignment operators that perform an operation and an assignment in a single step.
The = operator assigns the value on the right side (R-value) to the variable on the left side (L-value).
Syntax: variable = expression;
Example: int x = 10; assigns the integer 10 to the variable x.
These are shorthand operators that combine an arithmetic or bitwise operation with assignment.
| Operator | Description | Equivalent Expression |
| += | Add and assign | x = x + y |
| -= | Subtract and assign | x = x - y |
| *= | Multiply and assign | x = x * y |
| /= | Divide and assign | x = x / y |
| %= | Modulus and assign | x = x % y |
| <<= | Left shift and assign | x = x << y |
| >>= | Right shift and assign | x = x >> y |
| &= | Bitwise AND and assign | x = x & y |
| ^= | Bitwise XOR and assign | x = x ^ y |
| ` | =` | Bitwise OR and assign |
The following program demonstrates how these operators function in a real C script:
L-Value Requirement: The left side of an assignment operator must be a variable (a memory location), not a constant (e.g., 10 = x; is an error).
Right-to-Left Associativity: Assignment operators are evaluated from right to left. For example, a = b = c = 50; assigns 50 to c, then c to b, and finally b to a.
Efficiency: Compound operators are not just shorter to write; in some older compilers, they could be slightly more efficient, though modern compilers optimize both forms equally.
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION