Assignment Operators in C
Learn via video course
Overview
Operators are a fundamental part of all the computations that computers perform. Today we will learn about one of them known as Assignment Operators in C. Assignment Operators are used to assign values to variables. The most common assignment operator is =. Assignment Operators are Binary Operators.
Scope
- This article defines what are Assignment Operators in the C programming language.
- In this article, we will learn about all kinds of assignment operators in the C language using examples.
Introduction
Assignment Operators help us to assign the value or result of an expression to a variable and the value on the right side must be of the same data type as the variable on the left side. They have the lowest precedence level among all the operators, and it associates from right to left. The most commonly used Assignment Operator is =. Also, Assignment Operators fall under the category of Binary Operators.
For example, x = 4; then that means value 4 is assigned to variable x or we can say that variable x holds value 4.
Explanation
Let me explain to you more about Assignment Operators. Don't worry after this section, you will fully understand the definition of Assignment Operators in C.
Our example is x = 4, so what does it tell us?
- It simply says, " hey variable x please hold a value 4 which I give you as I wrote in the definition."
So can we say that always variables are on the Left Hand Side of the assignment operator and values are always on the Right Hand Side of the operator? YES. Please take a look at the image it will help you to understand more about the above wording.
The above diagram helps us to understand that the RHS value is assigned to the LHS variable.
The LHS and RHS are known as Operands.
So the operand on the LHS of the assignment operator must be a variable and operand on RHS must be a constant , variable or expression. For example:
As mentioned, precedence levels of assignment operators are lower than all the operators we have discussed so far and it associates from right to left.
Now you may wonder what do you mean by it associates from right to left? Let's understand this together.
For example:
This is totally correct and means we can also assign the same value to multiple variables with one single line of code.
So what do you get from the above line of code? Simply, variable_x, variable_y and variable_z hold the same value. YES!! TRUE. But how?
The main question is here how value is assigned to them? is first variable_x get 10 or variable_y or variable_z ? What do you say? This answer is given by the line: It associates from right to left.
So that means we have to read the line from the right side to the left. Like, at first 10 is given to variable_z then variable_y gets the value present in the variable_z and after that variable_x get the value present in the variable_y. So the above wording is equivalent to the following expression.
This is the simplest explanation about assignment operator associativity.
The most basic Assignment operator is =. It requires two operands for its work. For example, = x doesn't make any sense but variable = x makes sense because it clearly says the variable variable stores the value of x. Therefore, Assignment operators are Binary operators.
Hopefully, every point in the definition is now clear to all of you.
List of All Assignment Operators in C
We have 2 types of Assignment Operators in C :
- Simple Assignment operator (Example : = ).
- Compound Assignment Operators (Example : += , -= , &= ).
Simple Assignment Operator in C
It is the operator used to assign the Right Operand to Left Operand. There is only one simple Assignment Operator and that is =. The general Syntax is like Left Operand = Right Operand.
Compound Assignment Operators in C
Any Binary Operator with a simple Assignment Operator will form Compound Assignment Operators.
The general syntax is like Left operand operation = Right operand. Here, the operation is what you want + , - , *, etc.
Let's take an example:
Here read carefully. After which, you will never forget how to read the syntax of a compound assignment operator.
So we read like this FIRST ADD 10 to variable_x, THEN WHATEVER THE RESULT, ASSIGN THAT RESULT TO variable_x. That means the above line of code is equal to
List of Assignment operators in C
This is the complete list of all assignment operators in C. To read the meaning of operator please keep in mind the above example.
Operator | Meaning Of Operator | Example | Same as |
---|---|---|---|
= | Simple assignment operator | x=y | x=y |
+= | Add left operand to right operand then assign result to left operand | x+=y | x=x+y |
-= | subtract right operand from left operand then assign result to left operand | x-=y | x=x-y |
*= | multiply left operand with right operand then assign result to left operand | x*=y | x=x*y |
/= | divide left operand with right operand then assign result to left operand | x/=y | x=x/y |
%= | take modulus left operand with right operand then assigned result in left operand | x%=y | x=x%y |
<<= | Left Shift Assignment Operator means the left operand is left shifted by right operand value and assigned value to left operand | x<<=y | x=x<<y |
>>= | Right shift Assignment Operator means the left operand is right shifted by right operand value and assigned value to left operand | x>>=y | x=x>>y |
&= | Bitwise AND Assignment Operator means does AND on every bit of left operand and right operand and assigned value to left operand | x&=y | x=x&y |
|= | Bitwise inclusive OR Assignment Operator means does OR on every bit of left operand and right operand and assigned value to left operand | x|=y | x=x|y |
^= | Bitwise exclusive OR Assignment Operator means does XOR on every bit of left operand and right operand and assigned value to left operand | x^=y | x=x^y |
Example program for Assignment Operators in C
= Operator
This is a simple Assignment Operator.
Output :
+= Operator
This is the Addition Assignment Operator.
In which the left operand becomes equal to the addition of the right operand and left operand.
In this program,x+=y means x+y, so we assign the result of x+y to x.
Output :
-= Operator
This is the Subtraction Assignment Operator.
In which left operand becomes equal to the subtraction of right operator from left operand.
The program performs the subtraction of two numbers i.e. x-=y means x = x-y. So the output is :
Output :
*= Operator
The main purpose of this operator is that this left operand becomes equal to the product of the left and right operand. This is the Multiplication Assignment Operator.
The program performs the multiplication of two numbers and then the result of the multiplication is assigned to the variable x.
Output :
/= Operator
This one is Division Assignment Operator.
In this, the left operand becomes equal to the division of the left and right operand.
This program performs a division of two numbers and the result is assigned to x variable i.e. x/=y is the same as x = x/y.
Output :
%= Operator
It is well known Modulus Assignment Operator.
In this , left operand becomes equal to the modulo of left and right operand.
In this program, the user checks the remainder of two number and assign that remainder to x variable.
Output :
<<= Operator
This is called the Left Shift Assignment Operator.
For example x <<= y so in this, x becomes equal to x left shifted by y.
The program basically shifts every bit of x to the left side by y places and then assigns the result to x.
Output :
>>= Operator
This is called the Right Shift Assignment Operator.
For example x >>= y so , x becomes equal to x right shifted by y.
The program has defined the result of expression when x is right-shifted by y places and the result is going to store in x variable.
Output :
&= Operator
This operator is called the Bitwise AND Assignment Operator.
Left operand becomes equal to the bitwise AND of left and right operand.
The program performs Bitwise AND operation on every bit of x and y. After that result is going to be stored in variable x.
Output :
|= Operator
This is called the Bitwise Inclusive OR Assignment Operator
Left operand becomes equal to bitwise OR of left and right operand.
like Bitwise AND Assignment Operator, this program also performs Bitwise OR operation on every bit of x and y. And after that result is going to store in x.
Output :
^= Operator
This is called the Bitwise Exclusive OR Assignment Operator
Left operand becomes equal to bitwise XOR of left and right operand.
This will perform Bitwise XOR operation on every bit of x and y. After that result is going to store in x.
Output :
This is the detailed explanation with programs of all the assignment operators in C that we have. Hopefully, This is clear to you.
Happy Coding folks!!!
Conclusion
- Assignment operators are used to assign the result of an expression to a variable.
- There are two types of assignment operators in C. Simple assignment operator and compound assignment operator.
- Compound Assignment operators are easy to use and the left operand of expression needs not to write again and again.
- They work the same way in C++ as in C.