Loading, please wait...

Assignment Operators

C has several assignment operators available one simple assignment operator and several convenience assignment operators that combine arithmetic or bitwise operations with the assignment. The operators are as follows:

 

Operator

Description

Example

=

Simple assignment operator. Assigns values from right side operands to left side operands

num3 = num1 + num2 will assign the value of num1 + num2 to num3

+=

Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left

num3+= num1 is equivalent to num3 = num3 + num1

-=

Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.

num3-= num1 is equivalent to num3 = num3 - num1

*=

Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.

num3*= num1 is equivalent to num3 = num3 * num1

/=

Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.

num3/= num1 is equivalent to num3 = num3 / num1

%=

Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.

num3%= num1 is equivalent to num3 = num3 %num1

<<=

Left shift AND assignment operator.

num3<<=5 is same as num3 = num3<<5

>>=

Right shift AND assignment operator.

num3>>=5 is same as num3 = num3>>5

&=

Bitwise AND assignment operator

num3&=5 is same as num3 = num3&5

^=

Bitwise exclusive OR and assignment operator.

num3^=5 is same as num3 = num3^5

|=

Bitwise inclusive OR and assignment operator.

num3|=5 is same as num3 = num3|5

 

Example

 

/*A C program for Assignment operator */

#include <stdio.h>

main()
{
int num1 = 25;
int num3 ;

num3 = num1;
printf("Line 1 - = Value of num3 = %d\n", num3 );

num3 += num1;
printf("Line 2 - += Value of num3 = %d\n", num3 );

num3 -= num1;
printf("Line 3 - -= Value of num3 = %d\n", num3 );

num3 *= num1;
printf("Line 4 - *= Value of num3 = %d\n", num3 );
num3 /= num1;
printf("Line 5 - /= Value of num3 = %d\n", num3 );
num3 = 200;
num3 %= num1;
printf("Line 6 - %= Value of num3 = %d\n", num3 );
num3 <<= 2;
printf("Line 7 - <<= Value of num3 = %d\n", num3 );
num3 >>= 2;
printf("Line 8 - >>= Value of num3 = %d\n", num3 );
num3 &= 2;
printf("Line 9 - &= Value of num3 = %d\n", num3 );
num3 ^= 2;
printf("Line 10 - ^= Value of num3= %d\n", num3 );
num3 |= 2;
printf("Line 11 - |= Value of num3 = %d\n", num3 );
}

 

Output

Line 1 - = Value of num3 = 21

Line 2 - += Value of num3 = 42

Line 3 - -= Value of num3 = 21

Line 4 - *= Value of num3 = 441

Line 5 - /= Value of num3 = 21

Line 6 - %= Value of num3 = 11

Line 7 - <<= Value of num3 = 44

Line 8 - >>= Value of num3 = 11