Assignment Operators in C

Assignment Operators in C
Techiio-author
Written by Shuvhojit DebFebruary 7, 2022
8 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

Today we'll know what assignment operator is in C.

Assignment Operator in C

There are different kinds of the operators, such as arithmetic, relational, bitwise, assignment, etc., in the C programming language. The assignment operator is used to assign the value, variable, and function to another variable. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *=, and %=.

Example of the Assignment Operators:

A = 5; // use Assignment symbol to assign 5 to the operand A

B = A; // Assign operand A to the B

B = &A; // Assign the address of operand A to the variable B

A = 20 \ 10 * 2 + 5; // assign equation to the variable A

Different List of Assignment Operators in C

Below is the list of Assignment operators in C

1. The simple assignment operator (=): This operator Assigns values from the right operands to the left operand.

2. Add AND operator (+=): This operator adds the right operand to the left operand and assigns the output to the left operand.

3. Subtract AND operator (-=): This operator subtracts the right operand from the left operand and assigns the result to the left operand.

4. Multiply AND operator (*=): This operator multiplies the right operand with the left operand and assigns the result to the left operand.

5. Divide AND operator (/=): This operator divides the left operand with the right operand and assigns the result to the left operand.

6. 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 Left shift AND (<<=) operator, Right shift AND operator (>>=), Bitwise AND assignment operator (&= ), Bitwise exclusive OR and assignment operator (^=), Bitwise inclusive OR and assignment operator(|=)

Example of the Assignment Operators

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:

blogpost

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:

blogpost

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:

blogpost

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:

blogpost

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:

blogpost

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:

blogpost

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:

blogpost

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:

blogpost

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:

blogpost

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:

blogpost

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:

blogpost

Assignment Operator
C Language
Program
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance