Assignment Operators in C (original) (raw)
Last Updated : 04 Feb, 2025
In C, assignment operators are used to assign values to variables. The left operand is the variable and the right operand is the value being assigned. The value on the right must match the data type of the variable otherwise, the compiler will raise an error.
Let's take a look at an example:
C `
#include <stdio.h> int main() {
// Assigning value 10 to a
// using "=" operator
int a = 10;
printf("%d", a);
return 0;
}
`
**Explanation: In the above example, the assignment operator (**=) is used to assign the value **10 to the variable **a. The **printf() function then prints the value of a, which is 10, to the console.
Syntax
variable = value;
We can also assign the value of another variable using assignment operator.
Example
C `
#include <stdio.h> int main() { int x = 22; // Assigning value 10 to a // using "=" operator int a = x; printf("%d", a); return 0; }
`
Compound Assignment Operators
C also provides compound assignment operators that combine an operation and assignment in a single step. They make the code shorter and more efficient.
Here are the most commonly used compound assignment operators:
**1. Addition Assignment (+=)
Adds the value of the right operand to the left operand and stores the result in the left operand.
C `
#include <stdio.h> int main() { int a = 5;
// Equivalent to a = a + 3
a += 3;
printf("%d", a);
return 0;
}
`
**2. Subtraction Assignment (-=)
Subtracts the value of the right operand from the left operand and stores the result in the left operand.
C `
#include <stdio.h> int main() { int a = 10, b = 5;
// a = a - b
a -= b;
printf("%d", a);
return 0;
}
`
**3. Multiplication Assignment (*=)
Multiplies the value of the right operand by the left operand and stores the result in the left operand.
C `
#include <stdio.h> int main() { int a = 10, b = 5;
// a = a * b
a *= b;
printf("%d", a);
return 0;
}
`
**4. Division Assignment (/=)
Divides the left operand by the right operand and stores the result in the left operand.
C `
#include <stdio.h>
int main() { int a = 10, b = 5;
// a = a / b
a /= b;
printf("%d", a);
return 0;
}
`
**5. Modulus Assignment (%=)
Takes the modulus of the left operand by the right operand and stores the result in the left operand.
C `
#include <stdio.h>
int main() { int a = 10, b = 5;
// a = a % b
a %= b;
printf("%d", a);
return 0;
}
`
**6. Bitwise AND Assignment (&=)
Performs a bitwise AND operation and assigns the result.
C `
#include <stdio.h>
int main() {
// 60 = 0011 1100 in binary
int a = 60;
// 13 = 0000 1101 in binary
int b = 13;
// Bitwise AND Assignment
// a = a & b -> 60 & 13 = 12 (0000 1100 in binary)
a &= b;
printf("%d", a);
return 0;
}
`
**7. Bitwise OR Assignment (|=)
Performs a bitwise OR operation and assigns the result.
C `
#include <stdio.h>
int main() {
// 60 = 0011 1100 in binary
int a = 60;
// 13 = 0000 1101 in binary
int b = 13;
// Bitwise OR Assignment
// a = a | b -> 60 | 13 = 61 (0011 1101 in binary)
a |= b;
printf("a |= b: %d\n", a);
return 0;
}
`
**8. Bitwise XOR Assignment (^=)
Performs a bitwise XOR operation and assigns the result.
C `
#include <stdio.h> int main() {
// 60 = 0011 1100 in binary
int a = 60;
// 13 = 0000 1101 in binary
int b = 13;
// a = a ^ b -> 60 ^ 13 = 49 (0011 0001 in binary)
a ^= b;
printf("%d", a);
return 0;
}
`
**9. Bitwise 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.
C `
#include <stdio.h>
int main() {
// 60 = 0011 1100 in binary
int a = 60;
// 13 = 0000 1101 in binary
int b = 13;
// Bitwise Left Shift Assignment
// a = a << 2 -> 60 << 2 = 240 (1111 0000 in binary)
a <<= 2;
printf("%d", a);
return 0;
}
`
**10. Bitwise 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.
C `
#include <stdio.h>
int main() {
// 60 = 0011 1100 in binary
int a = 60;
// 13 = 0000 1101 in binary
int b = 13;
// Bitwise Right Shift Assignment
// a = a >> 2 -> 60 >> 2 = 15 (0000 1111 in binary)
a >>= 2;
printf("%d", a);
return 0;
}
`