In C language, assignment operators are used to assign values to variables. The most basic assignment operator is =, but there are also compound assignment operators that combine an arithmetic operation with assignment, making the code more concise.

Basic Assignment Operator

  1. = (Simple Assignment Operator):
    • Assigns the value on the right-hand side to the variable on the left-hand side.
    • Example:
      int a; a = 10; // Assigns the value 10 to variable a

Compound Assignment Operators

Compound assignment operators combine an arithmetic or bitwise operation with an assignment. These operators are shorthand for the operation and assignment combined.

  1. += (Addition Assignment):

    • Adds the right operand to the left operand and assigns the result to the left operand.
    • Equivalent to a = a + b.
    • Example:
      int a = 5; a += 3; // a becomes 8
  2. -= (Subtraction Assignment):

    • Subtracts the right operand from the left operand and assigns the result to the left operand.
    • Equivalent to a = a - b.
    • Example:
      int a = 5; a -= 2; // a becomes 3
  3. *= (Multiplication Assignment):

    • Multiplies the left operand by the right operand and assigns the result to the left operand.
    • Equivalent to a = a * b.
    • Example:
      int a = 5; a *= 2; // a becomes 10
  4. /= (Division Assignment):

    • Divides the left operand by the right operand and assigns the result to the left operand.
    • Equivalent to a = a / b.
    • Example:
      int a = 10; a /= 2; // a becomes 5
  5. %= (Modulus Assignment):

    • Computes the remainder of dividing the left operand by the right operand and assigns the result to the left operand.
    • Equivalent to a = a % b.
    • Example:
      int a = 10; a %= 3; // a becomes 1
  6. &= (Bitwise AND Assignment):

    • Performs a bitwise AND operation on the left operand with the right operand and assigns the result to the left operand.
    • Equivalent to a = a & b.
    • Example:
      int a = 6; // 0110 in binary a &= 3; // 0011 in binary // a becomes 2 (0010 in binary)
  7. |= (Bitwise OR Assignment):

    • Performs a bitwise OR operation on the left operand with the right operand and assigns the result to the left operand.
    • Equivalent to a = a | b.
    • Example:
      int a = 6; // 0110 in binary a |= 3; // 0011 in binary // a becomes 7 (0111 in binary)
  8. ^= (Bitwise XOR Assignment):

    • Performs a bitwise XOR operation on the left operand with the right operand and assigns the result to the left operand.
    • Equivalent to a = a ^ b.
    • Example:
      int a = 6; // 0110 in binary a ^= 3; // 0011 in binary // a becomes 5 (0101 in binary)
  9. <<= (Left Shift Assignment):

    • Shifts the bits of the left operand to the left by the number of positions specified by the right operand and assigns the result to the left operand.
    • Equivalent to a = a << b.
    • Example:
      int a = 3; // 0011 in binary a <<= 1; // Shifts bits to the left by 1 position // a becomes 6 (0110 in binary)
  10. >>= (Right Shift Assignment):

    • Shifts the bits of the left operand to the right by the number of positions specified by the right operand and assigns the result to the left operand.
    • Equivalent to a = a >> b.
    • Example:
      int a = 8; // 1000 in binary a >>= 2; // Shifts bits to the right by 2 positions // a becomes 2 (0010 in binary)

Example Program

#include <stdio.h> int main() { int a = 10; a += 5; // a becomes 15 printf("a += 5: %d\n", a); a -= 3; // a becomes 12 printf("a -= 3: %d\n", a); a *= 2; // a becomes 24 printf("a *= 2: %d\n", a); a /= 4; // a becomes 6 printf("a /= 4: %d\n", a); a %= 5; // a becomes 1 printf("a %%= 5: %d\n", a); return 0; }

Summary of Assignment Operators:

  • =: Assigns a value to a variable.
  • +=: Adds and assigns the result.
  • -=: Subtracts and assigns the result.
  • *=: Multiplies and assigns the result.
  • /=: Divides and assigns the result.
  • %=: Takes modulus and assigns the result.
  • &=: Bitwise AND and assigns the result.
  • |=: Bitwise OR and assigns the result.
  • ^=: Bitwise XOR and assigns the result.
  • <<=: Left shifts and assigns the result.
  • >>=: Right shifts and assigns the result.

Assignment operators help make code concise and easier to read by combining operations with assignment in a single step, saving time and reducing redundancy.