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.
= assigns the
value on the right side to the object on the left side. Operators such as
+=, -=, *=, /=, and
%= are called compound assignment operators.
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.
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
A variable can be declared and initialized at the same time.
int marks = 80;
Here, the variable marks is created and initialized with
80.
int age = 20;
age = 25;
printf("%d", age);
Output:
25
The old value is replaced by the new value.
int a = 10;
int b;
b = a;
printf("%d", b);
Output:
10
The current value of a is assigned to b.
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.
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 ==.
| 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 |
int number = 10;
number += 5;
printf("%d", number);
Output:
15
The statement number += 5; is equivalent to
number = number + 5;.
int number = 20;
number -= 5;
printf("%d", number);
Output:
15
It is equivalent to number = number - 5;.
int number = 10;
number *= 3;
printf("%d", number);
Output:
30
It is equivalent to number = number * 3;.
int number = 20;
number /= 4;
printf("%d", number);
Output:
5
It is equivalent to number = number / 4;.
int number = 17;
number %= 5;
printf("%d", number);
Output:
2
It is equivalent to number = number % 5;.
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
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.
float price;
price = 99.50f;
printf("%.2f", price);
Output:
99.50
Assignment operators can also be used with floating-point variables.
float price = 100.0f;
price += 25.5f;
printf("%.2f", price);
Output:
125.50
The += operator can be used with compatible arithmetic types.
char grade;
grade = 'A';
printf("%c", grade);
Output:
A
A character can be assigned to a char variable.
#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 +=.
int count = 0;
count += 1;
count += 1;
count += 1;
printf("%d", count);
Output:
3
Compound assignment operators are useful for updating counters and totals.
#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.
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 |
unsigned int a = 7;
unsigned int b = 3;
a &= b;
printf("%u", a);
Output:
3
The statement is equivalent to a = a & b;.
unsigned int a = 4;
unsigned int b = 2;
a |= b;
printf("%u", a);
Output:
6
The statement is equivalent to a = a | b;.
unsigned int a = 7;
unsigned int b = 3;
a ^= b;
printf("%u", a);
Output:
4
The statement is equivalent to a = a ^ b;.
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.
= with ==./=.%= with floating-point operands.#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;
}
#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.
Practice the following programs:
+=.-=.*=./=.%=.Practice these operators until you are comfortable with updating variable values using both simple and compound assignment.
= 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.Question: Which operator is used to add a value to an existing variable and assign the result back to the same variable?