In this tutorial, you will learn about the assignment operations in C and their usage and examples. Assignment operations widely used in C and add a great taste of math and linear algebra to the programming world.
Assignment Operations in C with Example
The table below illustrates the Assignment Operators in C, a brief description, and a simple example.
Operator | Description | Example |
---|---|---|
= | To assigns values from right side operands to left side operand | Z = X + Y |
+= | To adds the right operand to the left operand and assign the result to the left operand. | Z += Y |
-= | To subtracts the right operand from the left operand and assigns the result to the left operand. | Z -= Y |
*= | To subtracts the right operand from the left operand and assigns the result to the left operand. | Z * =Y |
/= | To multiplies the right operand with the left operand and assigns the result to the left operand. | Z / =Y |
%= | To takes modulus using two operands and assigns the result to the left operand. | Z % = Y |
<<= | Left shift AND assignment operator. | Z<<=Y |
>>= | Right shift AND assignment operator. | Z>>=Y |
&= | Bitwise AND assignment operator. | Z &= 2 |
^= | Bitwise exclusive OR and assignment operator. | Z ^= 2 |
|= | Bitwise inclusive OR and assignment operator. | Z |= 2 |
Example I of Assignment Operators in C
“=” Operator
int x; x = 10;
+= Operator
int a; x = 10; x + = 1; // same as x = x + 1
-= Operator
int x; x = 10; x - = 1; // same as x = x - 1
*= Operator
int x; x = 10; x * = 2; // same as x = x * 2
/= Operator
int x; x = 10; x / = 2; // same as x = x / 2
Example II of Assignment Operators in C
#include <stdio.h> int main() { int x = 10; printf("Value of x is %d\n", x); x += 10; printf("Value of x += 10 is %d\n", x); x -= 10; printf("Value of x -= 10 is %d\n", x); x *= 10; printf("Value of x *= 10 is %d\n", x); x /= 10; printf("Value of x /= 10 is %d\n", x); return 0; }
Output
Value of x is 10 Value of x += 10 is 20 Value of x -= 10 is 10 Value of x *= 10 is 100 Value of x /= 10 is 10