Assignment Operator in C and C++ - Arithmetic Assignment, Compound Assignment
Assignment Operator in C and C++
All C programs are valid C++ programs. Hence, Assignment operators in C are valid in C++.
Assignment Operator in C programming is used to assigning a value to the variable. This is called a simple assignment operator. The arithmetic and any other operator would not be useful without an assignment operator. The assignment operator takes the value on the right-hand side and puts it into the variable on the left-hand side.
Assignment Operator in C and C++ - Syntax
variable-name = value;
Assignment Operator in C and C++ - Examples
int x=10; float y=20; String s="hello"; char gender='M'; int a; a=20;
Compound Assignment Operators in C and C++
Sometimes, the left-hand-side variable is repeated immediately on the right side of the expression. For example, x = x + y. These types of expressions can also be written in compressed form, that is x += y. This kind of (+=) operator is called a compound assignment operator. Compound assignment operators are used as shown below:
Operators | Description | Example | Meaning |
---|---|---|---|
+= | Addition and Assignment operators. It adds the value of A and B and assigns the result to A. | x += y | x = x + y |
- = | Subtraction and Assignment operators. It subtracts the value of B from A and assigns the result to A. | x -= y | x = x - y |
* = | Multiplication and Assignment operators. It multiplies the value of A and B and assigns the result to A. | x *= y | x = x * y |
/= | Division and Assignment operators. It divides the value of A by B and assigns the result to A. | x /= y | x = x / y |
%= | Modulus and Assignment operators. It divides the value of A by B and stores the remainter to A. | x %= y | x = x%y |
&= | Bitwise AND and Assignment operators. It performs the bitwise AND with A and B and assigns the result to A. | x &= y | x = x & y |
|= | Bitwise OR and Assignment operators. It performs the bitwise OR with A and B and assigns the result to A. | x |= y | x = x | y |
^= | Bitwise XOR and Assignment operators. It performs the bitwise XOR with A and B and assigns the result to A. | x ^= y | x = x ^ y |
>>= | Bitwise Right Shift and Assignment operators. It performs the bitwise right shift with A and assigns the result to A. | x >>= y | x = x >> y |
<<= | Bitwise Left Shift and Assignment operators. It performs the bitwise left shift with A and assigns the result to A. | x <<= y | x = x << y |
Operators | Description | Example | Meaning |
---|---|---|---|
+= | Addition and Assignment operators. It adds the value of A and B and assigns the result to A. | x += y | x = x + y |
- = | Subtraction and Assignment operators. It subtracts the value of B from A and assigns the result to A. | x -= y | x = x - y |
* = | Multiplication and Assignment operators. It multiplies the value of A and B and assigns the result to A. | x *= y | x = x * y |
/= | Division and Assignment operators. It divides the value of A by B and assigns the result to A. | x /= y | x = x / y |
%= | Modulus and Assignment operators. It divides the value of A by B and stores the remainter to A. | x %= y | x = x%y |
&= | Bitwise AND and Assignment operators. It performs the bitwise AND with A and B and assigns the result to A. | x &= y | x = x & y |
|= | Bitwise OR and Assignment operators. It performs the bitwise OR with A and B and assigns the result to A. | x |= y | x = x | y |
^= | Bitwise XOR and Assignment operators. It performs the bitwise XOR with A and B and assigns the result to A. | x ^= y | x = x ^ y |
>>= | Bitwise Right Shift and Assignment operators. It performs the bitwise right shift with A and assigns the result to A. | x >>= y | x = x >> y |
<<= | Bitwise Left Shift and Assignment operators. It performs the bitwise left shift with A and assigns the result to A. | x <<= y | x = x << y |
Assigning Same value to Multiple Variables
There is also possible to assign a single value to multiple variables. This kind of assignment is called nested assignment or multiple assignments.
Assignment Operator Syntax
variable 1 = variable 2 = variable 3.......variable N = single value;
Assignment Operator - Example
x = y = z = 10; a = b = k =(x+y+z);
Assignment Operator - Complete Example Program
Following is an example C Program using Assignment Operators.
#include <stdio.h> int main() { /* Simple Assignment*/ int a,k; int b,j; float c=30.0; float d=5.0; /*Nested or Multiple Assignment */ a = k = 5; b = j = 3; /*Compound Assignment*/ a += b; printf("After Add and Assign :%d \n",a); k -= j; printf("After Subtract and Assign :%d \n",k); a *= b; printf("After Multiple and Assign :%d \n",a); c /= d; printf("After Divide and Assign :%f \n",c); j %= k; printf("After Modulo and Assign :%d \n",j); j &= k; printf("After Bitwise And and Assign :%d \n",j); j |= k; printf("After Bitwise OR and Assign :%d \n",j); a ^= b; printf("After Bitwise XOR and Assign :%d \n",a); a <<= 2; printf ("After Bitwise Left Shift and Assign :%d \n",a); a >>= 3; printf ("After Bitwise Right Shift and Assign :%d \n",a); return(0); }
Output
After Add and Assign :8 After Subtract and Assign :2 After Multiple and Assign :24 After Divide and Assign :6.000000 After Modulo and Assign :1 After Bitwise And and Assign :0 After Bitwise OR and Assign :2 After Bitwise XOR and Assign :27 After Bitwise Left Shift and Assign :108 After Bitwise Right Shift and Assign :13