Assignment Operators in C Programming Language
Table of Content:
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
Operator | Example | Same as |
---|---|---|
= | a = b | a = b |
+= | a += b | a = a+b |
-= | a -= b | a = a-b |
*= | a *= b | a = a*b |
/= | a /= b | a = a/b |
%= | a %= b | a = a%b |
Assignment operator supported by C are as follows.
operator | description | example |
---|---|---|
= | assigns values from right side operands to left side operand | A=B |
+= | adds right operand to the left operand and assign the result to left | A+=B is same as A=A+B |
-= | subtracts right operand from the left operand and assign the result to left operand | A-=B is same as A=A-B |
*= | mutiply left operand with the right operand and assign the result to left operand | A*=B is same as A=A*B |
/= | divides left operand with the right operand and assign the result to left operand | A/=B is same as A=A/B |
%= | calculate modulus using two operands and assign the result to left operand | A%=B is same as A=A%B |
<<= | Left shift AND assignment operator. | A <<= 2 is same as A = A<< 2 |
>>= | Right shift AND assignment operator. | A >>= 2 is same as A = A >> 2 |
&= | Bitwise AND assignment operator. | A &= 2 is same as A = A & 2 |
^= | bitwise exclusive OR and assignment operator. | A ^= 2 is same as A = A ^ 2 |
|= | bitwise inclusive OR and assignment operator. | A |= 2 is same as A = A | 2 |
Program
// C Program to demonstrate the working of logical operators #includeint main() { int a = 5; int b = 10; int c = 0; c = a + b; printf("c = a + b = %d \n",c); c += a ; printf("c += a = %d \n",c); c -= a ; printf("c -= a = %d \n",c); c *= a ; printf("c *= a = %d \n",c); a = 10; c = 15; c /= a ; printf("c /= a = %d \n",c); a = 10; c = 15; c %= a ; printf("c %= a = %d \n",c); c &= a ; printf("c &= a = %d \n",c); c ^= a ; printf("c ^= a = %d \n",c); c |= a ; printf("c |= a = %d \n",c); c <<= 2 ; printf("c <<= 2 = %d \n",c); c >>= 2 ; printf("c >>= 2 = %d \n",c); c >>= 2 ; printf("c >>= 2 = %d \n",c); }
Output
c = a + b = 15 c += a = 20 c -= a = 15 c *= a = 75 c /= a = 1 c = a = 5 c &= a = 0 c ^= a = 10 c |= a = 10 c <<= 2 = 40 c >>= 2 = 10 c >>= 2 = 2 Press any key to continue . . .