Assignment Operators in C
In this tutorial, we will learn about different assignment operators in the C programming language. Let’s first understand what an assignment operator is.
In this tutorial, we will learn about different assignment operators in the C programming language. Let’s first understand what an assignment operator is.
An assignment operator is a binary operator which is used to assign the right operand to the left operand. The left side of the assignment must be a variable or a memory location (called lvalue), while the right side can be any expression.
For example,x = 8 + 9 * 3
In the above example, x is a variable (lvalue) and the expression 8 + 9 * 3
evaluates to 35. So, the value 35 is assigned to x. The equal to (=) operator is the most basic assignment operator. In addition to this, there are also compound assignment operators. Let’s take a look at those.
The following table lists all the assignment operators available in the C programming language:
Operator | Meaning | Example | Equivalent to |
---|---|---|---|
= | Simple assignment | x = 5 | x = 5 |
+= | Add and assign | x += 5 | x = x + 5 |
-= | Subtract and assign | x -=5 | x = x – 5 |
*= | Multiply and assign | x *=5 | x = x * 5 |
/= | Divide and assign | x /=5 | x = x / 5 |
%= | Modulus and assign | x %= 5 | x = x % 5 |
<<= | Left shift and assign | x <<= 5 | x = x << 5 |
>>= | Right shift and assign | x >>= 5 | x = x >> 5 |
&= | Bitwise AND and assign | x &= 5 | x = x & 5 |
|= | Bitwise OR and assign | x |= 5 | x = x | 5 |
^= | Bitwise XOR and assign | x ^= 5 | x = x ^ 5 |
Except for the first operator shown in the table above, all the other operators are compound assignment operators. These operators are created by combining the assignment operator with another binary operator. Understanding how one operator works is enough to understand how the others work. Let’s explore the +=
operator using a simple example.
Example: x += 5
The above expression is the same as writing x = x + 5
. This means that we first add the value of x to 5 and then assign the result back to x. If we look closely, the +=
operator is saying the same thing. The plus symbol appears before the simple assignment operator, indicating that addition should be performed first before the assignment. By understanding this, it becomes easier to understand the other operators in the table.
Among all the operators, assignment operators have the lowest precedence. In the table below, you can see the precedence and associativity of assignment operators, as well as arithmetic, relational, and logical operators in C. We have already covered these operators in our previous tutorials.
Precedence | Operators | Associativity |
---|---|---|
1st | Unary + and -, Logical negation (!) | Right to left |
2nd | *, /, % | Left to right |
3rd | +, – | Left to right |
4th | <, <=, >, >= | Left to right |
5th | ==, != | Left to right |
6th | && | Left to right |
7th | || | Left to right |
8th | =, +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^= | Right to left |
Please note that we haven’t covered the shift operators and bitwise operators in our previous tutorials. So, for now, let’s ignore how these operators work. In the future, we will have dedicated tutorials specifically explaining the shift and bitwise operators.
Leave a comment