In C, assignment operators are used to assign values to variables.
The most basic assignment operator is =, but C provides several compound assignment operators that combine assignment with arithmetic or bitwise operations.
1. Basic Assignment Operator (=)
The = operator assigns the value on its right to the variable on its left.
Example: Simple Assignment
#include <stdio.h> int main() { int a = 10; // Assigns 10 to variable 'a' printf("Value of a: %d\n", a); a = 20; // Re-assigns 20 to 'a' printf("New value of a: %d\n", a); return 0; }
In this example:
- a = 10 assigns 10 to a.
- a = 20 changes a's value to 20.
Output:
Value of a: 10 New value of a: 20
2. Compound Assignment Operators
Compound assignment operators combine basic arithmetic or bitwise operations with assignment. These operators provide a shorthand way to update a variable's value based on its current value.
List of Compound Assignment Operators
Operator | Equivalent To | Description |
---|---|---|
+= | a = a + b | Adds and assigns |
-= | a = a – b | Subtracts and assigns |
*= | a = a * b | Multiplies and assigns |
/= | a = a / b | Divides and assigns |
%= | a = a % b | Modulus and assigns |
&= | a = a & b | Bitwise AND and assigns |
` | =` | `a = a |
^= | a = a ^ b | Bitwise XOR and assigns |
<<= | a = a << b | Left shift and assigns |
>>= | a = a >> b | Right shift and assigns |
3. Examples of Arithmetic Compound Assignment Operators
Example: Using += and -= Operators
#include <stdio.h> int main() { int a = 5; a += 3; // Equivalent to a = a + 3 printf("After += 3, a = %d\n", a); a -= 2; // Equivalent to a = a - 2 printf("After -= 2, a = %d\n", a); return 0; }
In this example:
- a += 3 adds 3 to a, resulting in 8.
- a -= 2 subtracts 2 from a, resulting in 6.
Output:
After += 3, a = 8 After -= 2, a = 6
Example: Using *= and /= Operators
#include <stdio.h> int main() { int b = 10; b *= 2; // Equivalent to b = b * 2 printf("After *= 2, b = %d\n", b); b /= 4; // Equivalent to b = b / 4 printf("After /= 4, b = %d\n", b); return 0; }
In this example:
- b *= 2 multiplies b by 2, resulting in 20.
- b /= 4 divides b by 4, resulting in 5.
Output:
After *= 2, b = 20 After /= 4, b = 5
Example: Using %= Operator
#include <stdio.h> int main() { int c = 17; c %= 5; // Equivalent to c = c % 5 printf("After %= 5, c = %d\n", c); return 0; }
In this example:
- c %= 5 finds the remainder when 17 is divided by 5, which is 2.
Output:
After %= 5, c = 2
4. Examples of Bitwise Compound Assignment Operators
Bitwise compound assignment operators are useful for low-level bit manipulation.
Example: Using &= Operator
#include <stdio.h> int main() { int x = 6; // Binary: 0110 int y = 3; // Binary: 0011 x &= y; // Equivalent to x = x & y printf("After &= with y, x = %d\n", x); // Result: 2 (Binary: 0010) return 0; }
Explanation:
- x &= y performs a bitwise AND, resulting in 2 (0010 in binary).
Output:
After &= with y, x = 2
Example: Using |= and ^= Operators
#include <stdio.h> int main() { int m = 4; // Binary: 0100 int n = 1; // Binary: 0001 m |= n; // Equivalent to m = m | n printf("After |= with n, m = %d\n", m); // Result: 5 (Binary: 0101) m ^= n; // Equivalent to m = m ^ n printf("After ^= with n, m = %d\n", m); // Result: 4 (Binary: 0100) return 0; }
Explanation:
- m |= n results in 5 (0101 in binary) by performing bitwise OR.
- m ^= n returns 4 (0100 in binary) by performing bitwise XOR.
Output:
After |= with n, m = 5 After ^= with n, m = 4
5. Examples of Shift Compound Assignment Operators
Shift compound assignment operators move bits to the left or right.
Example: Using <<= Operator (Left Shift)
#include <stdio.h> int main() { int num = 3; // Binary: 0011 num <<= 2; // Equivalent to num = num << 2 printf("After <<= 2, num = %d\n", num); // Result: 12 (Binary: 1100) return 0; }
Explanation:
- num <<= 2 shifts num two bits to the left, effectively multiplying it by 2^2 (or 4), resulting in 12.
Output:
After <<= 2, num = 12
Example: Using >>= Operator (Right Shift)
#include <stdio.h> int main() { int num = 16; // Binary: 10000 num >>= 2; // Equivalent to num = num >> 2 printf("After >>= 2, num = %d\n", num); // Result: 4 (Binary: 00100) return 0; }
Explanation:
- num >>= 2 shifts num two bits to the right, effectively dividing it by 2^2 (or 4), resulting in 4.
Output:
After >>= 2, num = 4
6. Using Compound Assignment Operators in Expressions
Compound assignment operators can be used in complex expressions and even inside loops.
Example: Compound Assignment in Expressions
#include <stdio.h> int main() { int a = 5, b = 10, result; result = (a += 3) * (b -= 2); // a is 8, b is 8, so result = 8 * 8 printf("a = %d, b = %d, result = %d\n", a, b, result); return 0; }
Explanation:
- a += 3 changes a to 8.
- b -= 2 changes b to 8.
- result = 8 * 8, resulting in 64.
Output:
a = 8, b = 8, result = 64
7. Using Compound Assignment Operators in Loops
Compound assignment operators can simplify loop counters and operations.
Example: Using += in a for Loop
#include <stdio.h> int main() { int sum = 0; for (int i = 1; i <= 10; i += 2) { sum += i; // Adds each odd number to sum } printf("Sum of odd numbers from 1 to 10: %d\n", sum); return 0; }
Explanation:
- i += 2 increments i by 2 each time, adding only odd numbers to sum.
- sum += i updates sum by adding i's value at each step.
Output:
Sum of odd numbers from 1 to 10: 25
Summary Table
Operator | Equivalent To | Description |
---|---|---|
= | a = b | Assigns b to a |
+= | a = a + b | Adds b to a |
-= | a = a – b | Subtracts b from a |
*= | a = a * b | Multiplies a by b |
/= | a = a / b | Divides a by b |
%= | a = a % b | Assigns the remainder of a / b to a |
&= | a = a & b | Performs bitwise AND and assigns |
` | =` | `a = a |
^= | a = a ^ b | Performs bitwise XOR and assigns |
<<= | a = a << b | Left shifts a by b positions |
>>= | a = a >> b | Right shifts a by b positions |
Complete Example: Compound Assignment in Various Operations
This program demonstrates each compound assignment operator.
#include <stdio.h> int main() { int num = 10; num += 5; printf("After += 5, num = %d\n", num); num -= 3; printf("After -= 3, num = %d\n", num); num *= 2; printf("After *= 2, num = %d\n", num); num /= 4; printf("After /= 4, num = %d\n", num); num %= 3; printf("After %%= 3, num = %d\n", num); num &= 2; printf("After &= 2, num = %d\n", num); num |= 4; printf("After |= 4, num = %d\n", num); num ^= 1; printf("After ^= 1, num = %d\n", num); num <<= 1; printf("After <<= 1, num = %d\n", num); num >>= 2; printf("After >>= 2, num = %d\n", num); return 0; }
This example demonstrates the effect of each compound assignment operator, showing how they modify num’s value in different ways.