Assignment and shorthand assignment operator in c
Assignment and shorthand operator in C
In C programming, assignment operator =
is used to assign value to an variable. For Example: If the vlaue 10
is to be assigned for the variable x
it can be assigned as x = 10
Assignment operator always assign value of its right operand to left operand. That is, x = y which assign the value of y to x. Assignment operator is denoted by =
(equal to) sign. It is a binary operator.
Let's see the the below assignment table:
Operation | Description |
---|
x = 10 | Assigns 10 to variable x |
y = (5 + 4) / 2 | Evaluates expression (5+4)/2 and assign result to y |
z = x + y | Evaluates x + y and assign result to z |
5 = x | Error, you cannot re-assign a value to a constant |
10 = 10 | Error, you cannot re-assign a value to a constant |
Remember: The RHS of assignment operator must be a constant, expression or variable. Whereas LHS must be a variable.
Shorthand assignment operator:
C Provides some special shorthand assignment operators, also known as compound assignment operators. It's called shorthand because it provides a short way to assign an expression to a variable. this operator can be used to connect Arithmetic or Bitwise operators with an Assignment operator. For Example: Expressiona = a + 5;
can also be written in short a += 5;
Similarly, there are many shorthand assignment operators. Below is a list of shorthand assignment operators in C.
Shorthand assignment operator | Example | Meaning |
---|
+= | a += 10 | a = a + 10 |
-= | a -= 10 | a = a - 10 |
*= | a *= 10 | a = a * 10 |
/= | a /= 10 | a = a / 10 |
%= | a %= 10 | a = a % 10 |
&= | a &= 10 | a = a & 10 |
|= | a |= 10 | a = a | 10 |
^= | a ^= 10 | a = a ^ 10 |
~= | a ~= 10 | a = a ~ 10 |
<<= | a <<= 10 | a = a << 10 |
>>= | a >>= 10 | a = a >> 10 |
Help others by sharing this page.
Ahmad Irshad
Author & Editor
I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.