Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error.

Symbol

Operator

Description

Syntax

=

Simple Assignment

Assign the value of the right operand to the left operand.

a = b

+=

Plus and assign

a = a + b (adds values of a to b and assign this value to a)

a += b

-=

Minus and assign

a = a - b (subtracts values of b from a and assign this value to a)

a -= b

*=

Multiply and assign

a = a * b (Multiplies a with b and assign the value to a)

a *= b

/=

Divide and assign

a = a / b (divides a by b and assigns the value to a)

a /= b

%=

Modulus and assign

a = a%b (divides a by b and assigns the value of the remainder to a)

a %= b

&=

AND and assign

Performs bitwise AND and assigns this value to the left operand.

a &= b

|=

OR and assign

Performs bitwise OR and assigns this value to the left operand.

a |= b

^=

XOR and assign

Performs bitwise XOR and assigns this value to the left operand.

a ^= b

>>=

Rightshift and assign

Performs bitwise Rightshift and assign this value to the left operand.

a >>= b

<<=

Leftshift and assign

Performs bitwise Leftshift and assign this value to the left operand.

a <<= b

 

SIMPLE C PROGRAMMING USING ASSIGNMENT OPERATORS

// C program to illustrate the arithmatic operators

#include <stdio.h>

int main()

{

            int a = 25, b = 5;

            // using operators and printing results

            printf("a = b : %d\n", a = b);

            printf("a += b : %d\n", a += b);

            printf("a -= b : %d\n", a -= b);

            printf("a *= b : %d\n", a *= b);

            printf("a /= b : %d\n", a /= b);

            printf("a %= b : %d\n", a %= b);

            printf("a &= b : %d\n", a &= b);

            printf("a |= b : %d\n", a |= b);

            printf("a >>= b : %d\n", a >> b);

            printf("a <<= b : %d\n", a << b);

            return 0;

}

        Output

                a = b : 5

                a += b : 10

                a -= b : 5

                a *= b : 25

                a /= b : 5

                a %= b : 0

                a &= b : 0

                a |= b : 5

                a >>= b : 0

                a <<= b : 160