There exist different assignment operators in C which are used to form assignment expressions that assign the value of an expression to an identifier. The general form of the simple assignment operator is given below:
identifier = expression;
Here, ‘=’, which is the simple assignment operator, assigns the value of expression
to the identifier
.
- Simple Assignment Operator: Simple assignment operator is the most commonly used assignment operator denoted by equals to ‘=’ sign. For example, when the expression
a = b;
is written, it means that the value of b is assigned to a. The assignment operation always takes place from right to left. The following examples show the use of simple assignment operator in C programming:
int a, b, c, x, y;
a = 90; /* value of variable a becomes 5 */
a = 95 + 10; /* value of variable a becomes 105 */
a = 55 + b; /* value of a becomes 55 + value of b */
a = b + c; /* value of a becomes value of b + value of c */
x = 1000; /* 1000 is assigned to x */
y = x * 4; /* value of x * 4 is assigned to y */
- Conversion rule in assignment: When an expression of one type is assigned to a variable of another type, a process called type conversion occurs. Type conversion follows a specific rule to convert an expression from one type to another. The rule is that the value of the right side, i.e. the expression side, of the assignment is converted into the type of the left side, i.e. the target variable. The following example illustrates the conversion rule in assignment:
char ch;
int x;
float f;
ch = 'a';
x = 2000;
f = 100.03;
ch = x; /* the left high-order bits of x are chopped off, leaving ch with the lower 8 bits */
x = f; /* x will be assigned the non-fractional part of f */
f = ch; /* f will convert the 8 bit integer value stored in ch to the same value in the floating point constant */
f = x; /* f will convert an integer value into floating point constant */
Multiple assignments, which are of the form
identifier_1 = identifier_2 = ... = expression;
are available to use in C programming. For example,a = b = c = 90;
In multiple assignment, the assignments are carried out from right to left.
In the example mentioned above, the expressionc = 90
is carried out first. This causes the value 90 to be placed in c with the value of the whole expression being 90. This expression value is then taken and assigned by the next assignment operator on the left, i.e.a = b = (c = 90);
- Conversion rule in assignment: When an expression of one type is assigned to a variable of another type, a process called type conversion occurs. Type conversion follows a specific rule to convert an expression from one type to another. The rule is that the value of the right side, i.e. the expression side, of the assignment is converted into the type of the left side, i.e. the target variable. The following example illustrates the conversion rule in assignment:
- Compound Assignment: Compound assignment is another type of assignment operator that makes coding simple by using a certain type of assignment operation. C programming contains the following assignment operators: +=, -=, /=, *= and %=. They are known as compound assignment operators. The table given below summarizes the assignment operators and their meanings:
Operators Meanings Usage += Adds the right-hand quantity to the left-hand variable. x += 100;
is equivalent tox = x + 100;
-= Subtracts the right-hand quantity from the left-hand variable. x -= 100;
is equivalent tox = x - 100;
*= Multiplies the left-hand variable by the right-hand quantity. x *= 100;
is equivalent tox = x * 100;
/= Divides the left-hand variable by the right hand quantity. x /= 100;
is equivalent tox = x / 100;
%= Gives the remainder obtained after dividing the left-hand variable by the right hand quantity. x %= 100;
is equivalent tox = x % 100;