Assignment Operators in C

We use this type of operator to transform as well as assign the values to any variable in an operation. In any given assignment operator, the right side is a value, and the left side is a variable. The value present on the right side of the operator must have the same data type as that of the variable present on the left side. In any other case, the compiler raises an error.

In this article, we will take a look into the Assignment Operators in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Types of Assignment Operators in C

An assignment operator is basically a binary operator that helps in modifying the variable to its left with the use of the value to its right. We utilize the assignment operators to transform and assign values to any variables.

Here is a list of the assignment operators that you can find in the C language:

  • basic assignment ( = )
  • subtraction assignment ( -= )
  • addition assignment ( += )
  • division assignment ( /= )
  • multiplication assignment ( *= )
  • modulo assignment ( %= )
  • bitwise XOR assignment ( ^= )
  • bitwise OR assignment ( |= )
  • bitwise AND assignment ( &= )
  • bitwise right shift assignment ( >>= )
  • bitwise left shift assignment ( <<= )

Working of Assignment Operators in C

Here is a table that discusses, in brief, all the Assignment operators that the C language supports:

Operator name Operator Description Equivalent of Example
basic assignment = p becomes equal to q N/A p = q
addition assignment += The addition of p and q becomes equal to p p = p + q p += q
subtraction assignment -= The subtraction of q from p becomes equal to p p = p – q p -= q
multiplication assignment *= The product of p and q becomes equal to p p = p * q p *= q
division assignment /= The division of p by q becomes equal to p p = p / q p /= q
modulo assignment %= The remainder of p divided by q becomes equal to p p = p % q p %= q
bitwise AND assignment &= The bitwise AND of p and q becomes equal to p p = p & q p &= q
bitwise OR assignment |= The bitwise OR of p and q becomes equal to p p = p | q p |= q
bitwise XOR assignment ^= The bitwise XOR of p and q becomes equal to p p = p ^ q p ^= q
bitwise left shift assignment <<= p left shifted by q becomes equal to p p = p << q p <<= q
bitwise right shift assignment >>= p right shifted by q becomes equal to p p = p >> q p >>= q

Example of Assignment Operators in C

Let us look at an example to understand how these work in a code:

#include <stdio.h>

main() {

int x = 21;

int y ;

y = x;

printf(“Line A – = Example of the Value of y = %d\n”, y );

y -= x;

printf(“Line B – -= Example of the Value of y = %d\n”, y );

y += x;

printf(“Line C – += Example of the Value of c = %d\n”, c );

y /= x;

printf(“Line D – /= Example of the Value of y = %d\n”, y );

y *= x;

printf(“Line E – *= Example of the Value of y = %d\n”, y );

y <<= 2;

printf(“Line F – <<= Example of the Value of y = %d\n”, y );

y = 200;

y %= x

printf(“Line G – %= Example of the Value of y = %d\n”, y );

y &= 2;

printf(“Line H – &= Example of the Value of y = %d\n”, y );

y >>= 2;

printf(“Line I – >>= Example of the Value of y = %d\n”, y );

y |= 2;

printf(“Line J – |= Example of the Value of y = %d\n”, y );

y ^= 2;

printf(“Line K – ^= Example of the Value of y = %d\n”, y );

}

The compilation and execution of the program mentioned above will produce a result as follows:

Line A – = Example of the Value of y = 21

Line B – -= Example of the Value of y = 21

Line C – += Example of the Value of y = 42

Line D – /= Example of the Value of y = 21

Line E – *= Example of the Value of y = 441

Line F – <<= Example of the Value of y = 44

Line G – %= Example of the Value of y = 11

Line H – &= Example of the Value of y = 2

Line I – >>= Example of the Value of y = 11

Line J – |= Example of the Value of y = 2

Line K – ^= Example of the Value of y = 0

Here is another example of how the assignment operators work in the C language:

#include

int main()

{

int x = 5;

int y = 10;

int z = 0;

z = x + y;

printf(“z = x + y = %d \n”,z);

z += x ;

printf(“z += x = %d \n”,z);

z -= x ;

printf(“z -= x = %d \n”,z);

z *= x ;

printf(“z *= x = %d \n”,z);

x = 10;

z = 15;

z /= x ;

printf(“z /= x = %d \n”,z);

x = 10;

z = 15;

z %= x ;

printf(“z %= x = %d \n”,z);

c &= x ;

printf(“c &= x = %d \n”,z);

z ^= x ;

printf(“z ^= x = %d \n”,z);

z |= x ;

printf(“z |= x = %d \n”,z);

z <<= 2 ;

printf(“z <<= 2 = %d \n”,z);

z >>= 2 ;

printf(“z >>= 2 = %d \n”,z);

z >>= 2 ;

printf(“z >>= 2 = %d \n”,z);

}

The output generated here will be:

z = x + y = 15

z += x = 20

z -= x = 15

z *= x = 75

z /= x = 1

z = x = 5

z &= x = 0

z ^= x = 10

z |= x = 10

z <<= 2 = 40

z >>= 2 = 10

z >>= 2 = 2

Practice Problems on Assignment Operators in C

1. What would be the output obtained from the program given below?

#include<stdio.h>

int main()

{

int p = 2;

p += p += p += 3;

printf(“%d”,p);

return 0;

}

A. 20

B. 30

C. 11

D. 9

Answer – A. 20

p+=p+=p+=3; it can written as p+=p+=p=p+3; p=2; Or, p+=p+=5; p=5; Or, p+=p=5+5; p=5; Or, p+=10; p=10; Or, p=p+10; p=10; Or, p=20. So, finally p=20.

2. Which of these is an invalid type of assignment operator?

A. x |= 10

B. x %= 10

C. x /= 10

D. None of these

Answer – D. None of these

All of these are valid types of assignment operators.


FAQs

How does the /= operator work? Is it a combination of two other operators?

Yes, the /+ operator is a combination of the = and / operators. The / operator divides the current value of the available variable first on the left using the available value on the right. It then assigns the obtained result to the available variable on the left side.

What is the most basic operator among all the assignment operators available in the C language?

The = operator is the most basic one used in the C language. We use this operator to assign the value available in the right to the value mentioned on the left side of the operator.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Leave a Comment

Your Mobile number and Email id will not be published.

*

*