Lesson 18 of 60 – Assignment Operators in C
30%

Assignment Operators in C

Assignment operators are used to assign values to variables in C. The basic assignment operator is =. C also provides compound assignment operators that combine an arithmetic or bitwise operation with assignment.

Note: The assignment operator = assigns the value on the right side to the object on the left side. Operators such as +=, -=, *=, /=, and %= are called compound assignment operators.

1. What is an Assignment Operator?

An assignment operator is used to store a value in a variable or modify the value of an object.

int age;

age = 20;

Here, 20 is assigned to the variable age.

2. Basic Assignment Operator =

The = operator assigns the value of the expression on its right to the object on its left.

int number;

number = 50;

printf("%d", number);

Output:

50

3. Assignment During Declaration

A variable can be declared and initialized at the same time.

int marks = 80;

Here, the variable marks is created and initialized with 80.

4. Assigning a New Value

int age = 20;

age = 25;

printf("%d", age);

Output:

25

The old value is replaced by the new value.

5. Assigning One Variable to Another

int a = 10;
int b;

b = a;

printf("%d", b);

Output:

10

The current value of a is assigned to b.

6. Assignment with an Expression

int a = 10;
int b = 20;

int sum;

sum = a + b;

printf("%d", sum);

Output:

30

The expression on the right side is evaluated before its result is assigned to sum.

7. Assignment is Different from Equality

The = operator performs assignment, while == is a relational operator used to compare two values.

int age = 20;

if (age == 20)
{
    printf("Age is 20");
}

Do not confuse = with ==.

8. Compound Assignment Operators

Operator Example Equivalent Form
+= a += b a = a + b
-= a -= b a = a - b
*= a *= b a = a * b
/= a /= b a = a / b
%= a %= b a = a % b

9. Addition Assignment +=

int number = 10;

number += 5;

printf("%d", number);

Output:

15

The statement number += 5; is equivalent to number = number + 5;.

10. Subtraction Assignment -=

int number = 20;

number -= 5;

printf("%d", number);

Output:

15

It is equivalent to number = number - 5;.

11. Multiplication Assignment *=

int number = 10;

number *= 3;

printf("%d", number);

Output:

30

It is equivalent to number = number * 3;.

12. Division Assignment /=

int number = 20;

number /= 4;

printf("%d", number);

Output:

5

It is equivalent to number = number / 4;.

13. Remainder Assignment %=

int number = 17;

number %= 5;

printf("%d", number);

Output:

2

It is equivalent to number = number % 5;.

14. Multiple Assignments

Assignment can be chained because an assignment expression itself has a value.

int a, b, c;

a = b = c = 10;

printf("%d %d %d", a, b, c);

Output:

10 10 10

15. Assignment from a Calculation

int price = 100;
int quantity = 5;
int total;

total = price * quantity;

printf("Total = %d", total);

Output:

Total = 500

The calculation is performed first, and the result is assigned to total.

16. Assignment with Floating-Point Values

float price;

price = 99.50f;

printf("%.2f", price);

Output:

99.50

Assignment operators can also be used with floating-point variables.

17. Floating-Point Compound Assignment

float price = 100.0f;

price += 25.5f;

printf("%.2f", price);

Output:

125.50

The += operator can be used with compatible arithmetic types.

18. Assignment with Characters

char grade;

grade = 'A';

printf("%c", grade);

Output:

A

A character can be assigned to a char variable.

19. Assignment Operators in User Input

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    number += 10;

    printf("Result = %d", number);

    return 0;
}

The value entered by the user is updated using the compound assignment operator +=.

20. Assignment Operators with Counters

int count = 0;

count += 1;
count += 1;
count += 1;

printf("%d", count);

Output:

3

Compound assignment operators are useful for updating counters and totals.

21. Assignment Operators in a Loop

#include <stdio.h>

int main()
{
    int total = 0;

    for (int i = 1; i <= 5; i++)
    {
        total += i;
    }

    printf("Total = %d", total);

    return 0;
}

Output:

Total = 15

The += operator updates the total during every iteration.

22. Bitwise Compound Assignment Operators

C also provides compound assignment operators for bitwise operations.

Operator Equivalent Form
&= a = a & b
|= a = a | b
^= a = a ^ b
<<= a = a << b
>>= a = a >> b

23. Bitwise AND Assignment

unsigned int a = 7;
unsigned int b = 3;

a &= b;

printf("%u", a);

Output:

3

The statement is equivalent to a = a & b;.

24. Bitwise OR Assignment

unsigned int a = 4;
unsigned int b = 2;

a |= b;

printf("%u", a);

Output:

6

The statement is equivalent to a = a | b;.

25. Bitwise XOR Assignment

unsigned int a = 7;
unsigned int b = 3;

a ^= b;

printf("%u", a);

Output:

4

The statement is equivalent to a = a ^ b;.

26. Shift Assignment Operators

unsigned int number = 4;

number <<= 1;

printf("%u", number);

Output:

8

The left-shift assignment operator shifts the bits to the left and assigns the resulting value back to the variable.

27. Common Assignment Operator Mistakes

  • Confusing = with ==.
  • Forgetting to initialize variables when their value is needed immediately.
  • Dividing by zero when using /=.
  • Using %= with floating-point operands.
  • Using incompatible types without understanding the conversion involved.
  • Forgetting that compound assignment updates the existing value.
  • Using a variable before it has a meaningful initialized value.

28. Complete Assignment Example

#include <stdio.h>

int main()
{
    int number = 100;

    printf("Initial = %d\n", number);

    number += 20;
    printf("After += 20: %d\n", number);

    number -= 10;
    printf("After -= 10: %d\n", number);

    number *= 2;
    printf("After *= 2: %d\n", number);

    number /= 5;
    printf("After /= 5: %d\n", number);

    number %= 7;
    printf("After %%= 7: %d\n", number);

    return 0;
}

29. Assignment Operators with User Input

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    number += 5;
    number *= 2;
    number -= 10;

    printf("Final value = %d", number);

    return 0;
}

This program demonstrates how multiple compound assignments can update a variable step by step.

30. Practice with Assignment Operators

Practice the following programs:

  1. Assign a value to an integer variable.
  2. Assign one variable's value to another variable.
  3. Add a value using +=.
  4. Subtract a value using -=.
  5. Multiply a value using *=.
  6. Divide a value using /=.
  7. Find a remainder using %=.
  8. Use assignment operators inside a loop.
  9. Use bitwise assignment operators with unsigned integers.
  10. Create a simple calculator using assignment and arithmetic operators.

Practice these operators until you are comfortable with updating variable values using both simple and compound assignment.

📌 Key Points

  • = is the basic assignment operator.
  • == is used for comparison, not assignment.
  • += adds a value to the existing value.
  • -= subtracts a value from the existing value.
  • *= multiplies the existing value.
  • /= divides the existing value.
  • %= assigns the remainder of integer division.
  • C also provides bitwise compound assignment operators.
  • Compound assignment operators are useful for updating variables.
  • Always make sure variables contain appropriate values before using assignment operations.

🧠 Quick Quiz

Question: Which operator is used to add a value to an existing variable and assign the result back to the same variable?