
Assignment Operators in C: Usage and Examples
☰Fullscreen
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 . . .
- Question 1: What is the difference between the = symbol and == symbol?
- Question 2: What is assignment operator in C?
- Question 3: What is the difference between pre increment operator and post increment operator?
- Question 4: What is the difference between pre decrement operator and post decrement operator?
Related Questions
- Assignment 1: = Assignment Operators in C, Simple assignment operator. Assigns values from right side operands to left side operand.
- Assignment 2: += Assignment Operators in C, Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.
- Assignment 3: += Assignment Operators in C, Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.
- Assignment 4: *= Assignment Operators in C, Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.
- Assignment 5: /= Assignment Operators in C, Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.
- Assignment 6: /= Assignment Operators in C, Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.
- Assignment 7: <<= Assignment Operators in C, Left shift AND assignment operator.
- Assignment 8: >>= Assignment Operators in C, Right shift AND assignment operator.
- Assignment 9: &= Assignment Operators in C, Bitwise AND assignment operator.
- Assignment 10: ^= Assignment Operators in C, Bitwise exclusive OR and assignment operator.
- Assignment 11: ^= Assignment Operators in C, Bitwise inclusive OR and assignment operator.
- Assignment 12: *= Assignment Operators in C, Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.
- Assignment 13: /= Assignment Operators in C, Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.
- Assignment 14: Assignment Operators in C, all in one
- Assignment 15: /= Assignment Operators in C, Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.
- Assignment 16: <<= Assignment Operators in C, Left shift AND assignment operator.
- Assignment 17: >>= Assignment Operators in C, Right shift AND assignment operator.
- Assignment 18: &= Assignment Operators in C, Bitwise AND assignment operator.
- Assignment 19: ^= Assignment Operators in C, Bitwise exclusive OR and assignment operator.
- Assignment 20: ^= Assignment Operators in C, Bitwise inclusive OR and assignment operator.
- Assignment 21: Assignment Operators in C, all in one
- Assignment 22: address of operator, Returns the address of a variable.
- Assignment 23: Write a program to demonstrate the results obtained by using the increment operators on operands with pre- and post- effect.
- Assignment 24: Write a program to demonstrate the results obtained by using the increment operators on operands in post- mode in an arithmetic expression.
- Assignment 25: Write a program to demonstrate the results obtained by using the increment operators along-with logical operators on operands.
- Assignment 26: Example of Increment and Decrement operator in cprogramming langauage