C Assignment Operators

In C, assignment operators are used to assign values to variables. The most commonly used assignment operator is the simple assignment operator =, but there are also compound assignment operators like +=, -=, *=, and others that perform an operation and assign the result in a single step.


List of Assignment Operators

Operator NameOperator SymbolDescription
Simple Assignment=Assigns the right-hand value to the left-hand variable.
Addition Assignment+=Adds the right-hand value to the left-hand variable and assigns the result.
Subtraction Assignment-=Subtracts the right-hand value from the left-hand variable and assigns the result.
Multiplication Assignment*=Multiplies the left-hand variable by the right-hand value and assigns the result.
Division Assignment/=Divides the left-hand variable by the right-hand value and assigns the result.
Modulus Assignment%=Finds the remainder when the left-hand variable is divided by the right-hand value and assigns the result.
Bitwise AND Assignment&=Performs a bitwise AND operation between the left-hand and right-hand values and assigns the result.
Bitwise OR Assignment|=Performs a bitwise OR operation between the left-hand and right-hand values and assigns the result.
Bitwise XOR Assignment^=Performs a bitwise XOR operation between the left-hand and right-hand values and assigns the result.
Left Shift Assignment<<=Shifts the bits of the left-hand variable left by the number of positions specified by the right-hand value and assigns the result.
Right Shift Assignment>>=Shifts the bits of the left-hand variable right by the number of positions specified by the right-hand value and assigns the result.

Examples of Assignment Operators

1. Simple Assignment Operator

In this example, we will use the simple assignment operator = to assign a value to a variable.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a;
    a = 10; // Assigning value to variable

    printf("Value of a: %d\n", a);
    return 0;
}

Explanation:

  1. We declare an integer variable a.
  2. We use the assignment operator = to assign 10 to a.
  3. The value of a is printed.

Output:

Value of a: 10

2. Addition Assignment Operator

In this example, we will use the addition assignment operator += to add a value to an existing variable.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 5;
    a += 3; // Equivalent to a = a + 3;

    printf("Value of a: %d\n", a);
    return 0;
}

Explanation:

  1. We initialize a with the value 5.
  2. We use += to add 3 to a.
  3. The new value of a is printed.

Output:

Value of a: 8

3. Multiplication Assignment Operator

In this example, we will use the multiplication assignment operator *= to multiply a variable by a value.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 4;
    a *= 2; // Equivalent to a = a * 2;

    printf("Value of a: %d\n", a);
    return 0;
}

Explanation:

  1. We initialize a with the value 4.
  2. We use *= to multiply a by 2.
  3. The new value of a is printed.

Output:

Value of a: 8

Conclusion

In this tutorial, we explored the assignment operators in C:

  1. The simple assignment operator = assigns a value to a variable.
  2. Compound assignment operators like +=, *=, and others perform an operation and assign the result in one step.
  3. These operators provide a shorthand way to update variable values efficiently.