![]() |
VOOZH | about |
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.
10
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.
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:
Adds the value of the right operand to the left operand and stores the result in the left operand.
a = 8
Subtracts the value of the right operand from the left operand and stores the result in the left operand.
5
Multiplies the value of the right operand by the left operand and stores the result in the left operand.
50
Divides the left operand by the right operand and stores the result in the left operand.
2
Takes the modulus of the left operand by the right operand and stores the result in the left operand.
0
Performs a bitwise AND operation and assigns the result.
12
Performs a bitwise OR operation and assigns the result.
a |= b: 61
Performs a bitwise XOR operation and assigns the result.
Shifts the bits of the left operand to the left by the number of positions specified by the right operand and assigns the result.
240
Shifts the bits of the left operand to the right by the number of positions specified by the right operand and assigns the result.
15