Broad Network


Assignment Operators in the C Computer Language

Part 11 of Complete C Course

Foreword: Assignment Operators in the C Computer Language

By: Chrysanthus Date Published: 3 Jun 2024

Introduction

Assignment operators are:

=  *=  /=  %=  +=  -=  >>=  <<=  &=  ^=  |=

These are discussed in this section.

Simple Assignment Operator (=)
The simple assignment operator is, =. The following statement shows an example:

    float myFlt = 3.5;

We say the float number 3.5 is assigned to the object, identified by myFlt. The left operand to = is myFlt. The right operand is 3.5. The left and right operands should be of the same type.

For all assignment operators, the operation is from right to left; that is, from the right operand to the left operand. If the reader is trying to make similarity with mathematics, then the previous sentence (right to left) may not make sense; avoid making similarity with mathematics. There are left-to-right and right-to-left operators in C.

For the simple assignment, the right operand may be another identifier, or even a function call, that returns a value \96 see later.

E1 op= E2 Expressions
In the following generalized statement, E1 is an object identifier, op is an operator (e.g. +), and E2 is another object identifier; = is the simple assignment operator:

    E1 op= E2

This generalized statement that involves the simple assignment operator is equivalent to,

    E1 = E1 op E2

The rest of the assignment operators in the list above follow this generalized rule as explained below. The rest of the assignment operators are each made up of an operator the reader might already know (from above), and the simple assignment operator.

The combination of an operator with =, forms a compound assignment operator.

The *= Operator
Consider the following statement:

    int int1 = int1 * int2;

Here, int1 and int2 are object identifiers. Note that int1 is found on both the left side and right side, of the simple assignment operator. There is also the multiplication operator, * in the right operand of the simple assignment operator. In the above statement the result of multiplying int1 by int2 is assigned to int1. int1 on the right has the old value, while int1 on the left has the new value. The statement can be re-written as:

    int int1 *= int2;

Also, int2 can be replaced by a number as in,

    int int1 *=3;

The /= Operator
Consider the following statement:

    int int1 = int1 / int2;

Here, int1 and int2 are object identifiers. The statement can be re-written as:

    int int1 /= int2;

Also, int2 can be replaced by a number as in,

    int int1 /=4;

The following program that outputs 5, illustrates this:

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
        int int1 = 20;
        int int2 = 4;

        int1 /= int2;
        
        printf("%i\n", int1);
        
        return 0;
    }

The %= Operator
Consider the following statement:

    int int1 = int1 % int2;

Here, int1 and int2 are object identifiers. Note that int1 is found on both the left and right sides of the assignment operator. There is also the modulus operator, % on the right side of the assignment operator. In the above statement, the remainder of dividing int1 by int2 is assigned to int1. The statement can be re-written as:

    int int1 %= int2;

The += Operator
Consider the following statement:

    int int1 = int1 + int2;

Here, int1 and int2 are object identifiers. The statement can be re-written as:

    int int1 += int2;

The -= Operator
Consider the following statement:

    int int1 = int1 - int2;

Here, int1 and int2 are object identifiers. The statement can be re-written as:

    int int1 -= int2;

Bitwise Assignment Operators
>>=  <<=  &=  ^=  and |= are known as bitwise assignment operators. These are used with bits in integers. These compound operators are not addressed in this chapter.


Related Links

More Related Links

Cousins

BACK NEXT

Comments