Assignment operators

Assignment operator is used to assign a value in a variable.

The left side operand of the assignment operator is variable and right side operand of the assignment operator is value.

For example : num = 10 . Here num is variable and 10 is value.

The value 10 is assigned to variable num.

The assignment operators are :

= Simple assignment
+= Add AND assignment
-= Subtract AND assignment
*= Multiple AND assignment
/= Division AND assignment
%= MOdulus AND assignment
<<= Left shift AND
>>= Right shift AND
&= Bitwise AND
^= Bitwise exclusive OR
|= Bitwise inclusive OR

Assignment operations program

        
#include <stdio.h>

int main()
{
    int a = 21;
    int b ;
                
    b = a;
    printf("= Operator : Value of b = %d\n", b);
                
    b += a;
    printf("+= Operator : Value of b = %d\n", b);
                
    b -= a;
    printf("-= Operator : Value of b = %d\n", b);
                
    b *= a;
    printf("*= Operator : Value of b = %d\n", b);
                
    b /= a;
    printf("/= Operator : Value of b = %d\n", b);
                
    // Now value of b is 200
    b = 200;
    b %= a;
    printf("%%= Operator : Value of b = %d\n", b);
                
    b <<= 2;
    printf("<<= Operator : Value of b = %d\n", b);
                
    b >>= 2;
    printf(">>= Operator : Value of b = %d\n", b);
                
    b &= 2;
    printf("&= Operator : Value of b = %d\n", b);
                
    b ^= 2;
    printf("^= Operator : Value of b = %d\n", b);
                
    b |= 2;
    printf("|= Operator : Value of b = %d\n", b);
                
    return 0;
}
                
        
    

Output

        
= Operator : Value of b = 21
+= Operator : Value of b = 42
-= Operator : Value of b = 21
*= Operator : Value of b = 441
/= Operator : Value of b = 21
%= Operator : Value of b = 11
<<= Operator : Value of b = 44
>>= Operator : Value of b = 11
&= Operator : Value of b = 2
^= Operator : Value of b = 0
|= Operator : Value of b = 2
        
    

Post a Comment

0 Comments