Jul 12
C has special shorthand that simplifies coding of certain type of assignment statements.
Example
a = a + 2;
can be written as
a + =2; compound assignment statement
This operator += tells the compiler that a is assigned the value of a+2.
This shorthand works for all binary operators in C.
General form:
<variable> < operator> = <variable/constant/expression>
| Operator |
Example |
Meaning |
| += |
a+=2 |
a=a+2 |
| - = |
a-=2 |
a=a-2 |
| *= |
a*=2 |
a=a*2 |
| /= |
a/=2 |
a=a/2 |
| %= |
a%=2 |
a=a%2 |
| &&= |
a&&=c |
a=a&&c |
| ||= |
a||=c |
a=a||c |
written by Shweta
Jun 22
Assignment Operators
The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.
Example
x = a + b
Here the value of a + b is evaluated and substituted to the variable x.
In addition, C has a set of shorthand assignment operators of the form.
var oper = exp;
Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as shorthand assignment operator
* Only 1 variable is allowed on the left side of ‘=’.
Example :
z= k * i; is valid statement.
where k * i= z; is invalid statement.
- If a value is assigned to left side of ‘=’ , error message will be displayed on screen.
Example :
a+b=c;
Error : Lvalue required.
- An arithmetic instruction can be used for storing characters constants in character variables.
char a,b,d;
a = ‘F’;
b = ‘G’;
c = ‘+’;
When we do this, the ASCII value of the characters are stored in the variables. As ‘a’ stores 70(ASCII value of ‘F’), ‘b’ stores 71(G=71) and so on.
written by Shweta
\\ tags: Assignment
Recent Comments