OverIQ.com

Assignment Operator in C

Last updated on July 27, 2020


We have already used the assignment operator ( = ) several times before. Let's discuss it here in detail. The assignment operator ( = ) is used to assign a value to the variable. Its general format is as follows:

variable = right_side

The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:

1
2
3
x = 18  // right operand is a constant
y = x    // right operand is a variable
z = 1 * 12 + x   // right operand is an expression

The precedence of the assignment operator is lower than all the operators we have discussed so far and it associates from right to left.

We can also assign the same value to multiple variables at once.

x = y = z = 100

here x, y and z are initialized to 100.

Since the associativity of the assignment operator ( = ) is from right to left. The above expression is equivalent to the following:

x = (y = (z = 100))

Note that expressions like:

1
2
3
x = 18
y = x
z = 1 * 12 + x

are called assignment expression. If we put a semicolon(;) at the end of the expression like this:

1
2
3
x = 18;
y = x;
z = 1 * 12 + x;

then the assignment expression becomes assignment statement.

Compound Assignment Operator #

Assignment operations that use the old value of a variable to compute its new value are called Compound Assignment.

Consider the following two statements:

1
2
x = 100;
x = x + 5;

Here the second statement adds 5 to the existing value of x. This value is then assigned back to x. Now, the new value of x is 105.

To handle such operations more succinctly, C provides a special operator called Compound Assignment operator.

The general format of compound assignment operator is as follows:

variable op= expression

where op can be any of the arithmetic operators (+, -, *, /, %). The above statement is functionally equivalent to the following:

variable = variable op (expression)

Note: In addition to arithmetic operators, op can also be >> (right shift), << (left shift), | (Bitwise OR), & (Bitwise AND), ^ (Bitwise XOR). We haven't discussed these operators yet.

After evaluating the expression, the op operator is then applied to the result of the expression and the current value of the variable (on the RHS). The result of this operation is then assigned back to the variable (on the LHS). Let's take some examples: The statement:

x += 5;

is equivalent to x = x + 5; or x = x + (5);.

Similarly, the statement:

x *= 2;

is equivalent to x = x * 2; or x = x * (2);.

Since, expression on the right side of op operator is evaluated first, the statement:

x *= y + 1;

is equivalent to x = x * (y + 1).

The precedence of compound assignment operators are same and they associate from right to left (see the precedence table).

The following table lists some Compound assignment operators:

Operator Description
+= x += 5 equivalent to x = x + 5
-= y -= 5 equivalent to y = y - 5
/= z /= 3 equivalent to z = z / 5
%= m %= 10 equivalent to m = m % 10

The following program demonstrates Compound assignment operators in action:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>

int main(void)
{

    int i = 10;

    char a = 'd';
    printf("ASCII value of %c is %d\n", a, a); // print ASCII value of d

    a += 10; // increment a by 10;
    printf("ASCII value of %c is %d\n", a, a); // print ASCII value of n

    a *= 5; // multiple a by 5;
    printf("a = %d\n", a); 

    a /= 4; // divide a by 4;
    printf("a = %d\n", a); 

    a %= 2; // remainder of a % 2;
    printf("a = %d\n", a); 

    a *= a + i;  // is equivalent to  a = a * (a + i)
    printf("a = %d\n", a);

    return 0; // return 0 to operating system
}

Expected Output:

1
2
3
4
5
6
ASCII value of d is 100
ASCII value of n is 110
a = 38
a = 9
a = 1
a = 11