Assignment Operators In C
Assignment operators is a binary operator which is used to assign values in a variable, with its right and left sides being a one-one operand. The operand on the left side is variable in which the value is assigned and the right side operands can contain any of the constant, variable, and expression.
x = 18 // right operand is a constant y = x // right operand is a variable z = 1*12+x // right operand is an expression
The Assignment operator is a lower priority operator. its priority has much lower than the rest of the other operators. Its priority is more than just the comma operator. The priority of all other operators is more than the assignment operator.
We can assign the same value to multiple variables simultaneously by the assignment operator.
x = y = z = 100
Here x, y, and z are initialized to 100.
In C language, the assignment operator can be divided into two categories.
- Simple assignment operator
- Compound assignment operators
1. Simple Assignment Operator In C
This operator is used to assign left-side values to the right-side operands, simple assignment operators are represented by (=).
Variable = constant_variable_anyExpration
x = 10 // right operand is a constant y = x // right operand is a variable z = 1*15+x // right operand is an expression
2. Compound Assignment Operators In C
Compound Assignment Operators use the old value of a variable to calculate its new value and reassign the value obtained from the calculation to the same variable.
Examples of compound assignment operators are: (Example: + =, – =, * =, / =,% =, & =, ^ =)
Look at these two statements:
x = 100;
x = x + 5;
Here in this example, adding 5 to the x variable in the second statement is again being assigned to the x variable.
Compound Assignment Operators provide us with the C language to perform such operation even more effecient and in less time.
Syntax of Compound Assignment Operators
variable op = expression
Here op can be any arithmetic operators (+, -, *, /,%).
x = 100; x+= 5;
The above statement is equivalent to the following depending on the function:
x = x + 5;
Let us now know about some important compound assignment operators one by one.
“+ =” -: This operator adds the right operand to the left operand and assigns the output to the left operand.
“- =” -: This operator subtracts the right operand from the left operand and returns the result to the left operand.
“* =” -: This operator multiplies the right operand with the left operand and assigns the result to the left operand.
“/ =” -: This operator splits the left operand with the right operand and assigns the result to the left operand.
“% =” -: This operator takes the modulus using two operands and assigns the result to the left operand.
There are many other assignment operators such as left shift and (<< =) operator, right shift and operator (>> =), bitwise and assignment operator (& =), bitwise OR assignment operator (^ =)
List of Assignment Operators In C
|=||sum = 101;101 is assigned to variable sum|
|+=||sum += 101; This is same as sum = sum + 101|
|-=||sum -= 101; This is same as sum = sum – 101|
|*=||sum *= 101; This is same as sum = sum * 101|
|/=||sum /= 101; This is same as sum = sum/101|
|%=||sum %= 101; This is same as sum = sum % 101|
|&=||sum&=101; This is same as sum = sum & 101|
|^=||sum ^= 101; This is same as sum = sum ^ 101|
Friends, I hope you have found the answer of your question and you will not have to search about the Assignment operators in C Language
However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Assignment operators in C Language
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.