Updated June 21, 2023
Introduction to Assignment Operators in C
Assignment operators are used for assigning value to the variable. Like any other operator, C also supports Assignment Operator which is a binary operator that operates on any two operands. It has two values such as the right value and the left value. It has lower precedence than all available operators but has higher precedence than the comma operator.
Different List of Assignment Operators in C
Below is the list of Assignment operators in C
- The simple assignment operator (=): This operator Assigns values from the right operands to the left operand.
- Add AND operator (+=): This operator adds the right operand to the left operand and assigns the output to the left operand.
- Subtract AND operator (-=): This operator subtracts the right operand from the left operand and assigns the result to the left operand.
- Multiply AND operator (*=): This operator multiplies the right operand with the left operand and assigns the result to the left operand.
- Divide AND operator (/=): This operator divides the left operand with the right operand and assigns the result to the left operand.
- Modulus AND operator (%=): This operator takes modulus using two operands and assigns the result to the left operand.
There are many other assignment operators such as the Left shift AND (<<=) operator, Right shift AND operator (>>=), Bitwise AND assignment operator (&= ), Bitwise exclusive OR and assignment operator (^=), Bitwise inclusive OR and assignment operator(|=)
Examples of Assignment Operators in C
Examples of Assignment Operators are given below:
Example #1
Program to implement the use of = operator:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y, total;
printf("Enter the value of X: ");
scanf("%d",&X);
printf("Enter the value of Y: ");
scanf("%d",&Y);
total = X + Y;
printf("%d", total);
return 0;
}
Output:
Example #2
Program to implement the use of Add AND operator (+=) in C:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
printf("Enter the value of Y: ");
scanf("%d",&Y);
Y += X;
printf("%d", Y);return 0;
}
Output:
Example #3
Program to use Subtract AND operator (- =) in C:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
printf("Enter the value of Y: ");
scanf("%d",&Y);
Y -= X;
printf("%d", Y);return 0;
}
Output:
Example #4
Program to use Multiply AND operator (*=) in C:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
printf("Enter the value of Y: ");
scanf("%d",&Y);
Y *= X;
printf("%d", Y);
return 0;
}
Output:
Example #5
Program to use Divide AND operator (/=) in C:
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
printf("Enter the value of Y: ");
scanf("%d",&Y);
Y /= X;
printf("%d", Y);
return 0;
}
Output:
Example #6
Program to use Modulus AND operator (%=) in C
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
printf("Enter the value of Y: ");
scanf("%d",&Y);
Y %= X;
printf("%d", Y);
return 0;
}
Output:
Example #7
Program to use Left shift AND (<<=) operator in C
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
X <<= 2;
printf("%d", X);
return 0;
}
Output:
Example #8
Program to use Right shift AND (>>=) operator in C
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
X >>= 2;
printf("%d", X);
return 0;
}
Output:
Example #9
Program to use Bitwise AND assignment operator (&= ) in C
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
X &= 2;
printf("%d", X);
return 0;
}
Output:
Example #10
Program to use Bitwise exclusive OR and assignment operator (^=)
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
X ^= 2;
printf("%d", X);
return 0;
}
Output:
Example #11
Program to use Bitwise inclusive OR and assignment operator (|=) in C
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int X, Y;
printf("Enter the value of X: ");
scanf("%d",&X);
X |= 2;
printf("%d", X);
return 0;
}
Output:
Recommended Articles
We hope that this EDUCBA information on “Assignment Operators in C” was beneficial to you. You can view EDUCBA’s recommended articles for more information.