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.


What is an Assignment Operator?

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.


Compound Assignment Operators

The following table lists all the assignment operators available in the C programming language:

OperatorMeaning ExampleEquivalent to
=Simple assignmentx = 5x = 5
+=Add and assignx += 5x = x + 5
-=Subtract and assignx -=5x = x – 5
*=Multiply and assignx *=5x = x * 5
/=Divide and assignx /=5x = x / 5
%=Modulus and assignx %= 5x = x % 5
<<=Left shift and assignx <<= 5x = x << 5
>>=Right shift and assignx >>= 5x = x >> 5
&=Bitwise AND and assignx &= 5x = x & 5
|=Bitwise OR and assignx |= 5x = x | 5
^=Bitwise XOR and assignx ^= 5x = 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.


Precedence and Associativity of Assignment Operators

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.

PrecedenceOperatorsAssociativity
1stUnary + 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

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.