Friday, July 29, 2016

C Basics - Compound Assignment Operators in C

The following table shows all the compound or augmented assignment operators supported by the C language. Assume variable x holds 21, y holds 10 and variable z holds 0 then.

Operator Name Syntax Description Meaning
Addition assignment x += y It adds the right operand to the left operand and assigns the result to the left operand. x =x + y
Subtraction assignment x -= y It subtracts the right operand from the left operand and assigns the result to the left operand. x =x - y
Multiplication assignment x *= y It multiplies the right operand with the left operand and assigns the result to the left operand. x =x * y
Division assignment x /= y It divides the left operand with the right operand and assigns the result to the left operand. x =x / y
Modulo assignment x %= y It takes modulus using two operands and assigns the result to the left operand. x =x % y
Bitwise AND assignment x &= y Bitwise AND assignment operator. x =x & y
Bitwise OR assignment x |= y Bitwise OR and assignment operator. x =x | y
Bitwise XOR assignment x ^= y Bitwise exclusive OR and assignment operator. x =x ^ y
Bitwise left shift assignment x <<= y Left shift and assignment operator. x =x << y
Bitwise right shift assignment x >>= y Right shift and assignment operator. x =x >> y

#include <stdio.h>

int main() 
{
 int x = 21;
 int y = 10;
 int z = 0;
 
 printf("Compound Assignment operators demo\n");
 printf("Value of x is %d\n", x );
        printf("Value of y is %d\n", y );
 printf("Value of z is %d\n", z );
 
 x += y;
 printf("Addition Assignment (x += y): %d\n", x );
 
 x = 21;
 x -= y;
 printf("Subtraction Assignment (x -= y): %d\n", x );
 
 x = 21;
 x *= y;
 printf("Multiplication Assignment (x *= y): %d\n", x );
 
 x = 21;
 x /= y;
 printf("Division Assignment (x /= y): %d\n", x );
 
 x = 21;
 x %= y;
 printf("Modulo Assignment (x mod= y): %d\n", x );
 
 x = 21;
 x <<= 2;
 printf("Bitwise left shift Assignment (x <<= 2): %d\n", x );
 
 x = 21;
 x >>= 2;
 printf("Bitwise right shift Assignment (x >>= 2): %d\n", x );
 
 x = 21;
 x &= y;
 printf("Bitwise AND Assignment (x &= y): %d\n", x );
 
 x = 21;
 x |= y;
 printf("Bitwise OR Assignment (x |= y): %d\n", x );
 
 x = 21;
 x ^= y;
 printf("Bitwise XOR Assignment (x ^= y): %d\n", x );
 return 0;
}
The output of the above program would be:

Compound Assignment operators demo
Value of x is 21
Value of y is 10
Value of z is 0
Addition Assignment (x += y): 31
Subtraction Assignment (x -= y): 11
Multiplication Assignment (x *= y): 210
Division Assignment (x /= y): 2
Modulo Assignment (x mod= y): 1
Bitwise left shift Assignment (x <<= 2): 84
Bitwise right shift Assignment (x >>= 2): 5
Bitwise AND Assignment (x &= y): 0
Bitwise OR Assignment (x |= y): 31
Bitwise XOR Assignment (x ^= y): 31



Related topics:
Overview of Operators in C   |   Arithmetic Operators in C   |   Relational Operators in C   |   Logical Operators in C   |   Bitwise Operators in C   |   Conditional Operators in C   |   Miscellaneous Operators in C   |   Operator Precedence and Associativity in C

List of topics: C Programming

No comments:

Post a Comment