Assignment Operator in C

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 %=.

Simple Assignment Operator (=):

It is the operator used to assign the right side operand or variable to the left side variable.

Syntax:

int a = 5;

(or) int b = a;

ch = ‘a’;

Plus and Assign Operator (+=):

The operator is used to add the left side operand to the left operand and then assign results to the left operand.

Syntax:

A += B;

(Or)

A = A + B;

Subtract and Assign Operator (-=):

The operator is used to subtract the left operand with the right operand and then assigns the result to the left operand.

Syntax:

A -= B;

(Or)

A = A — B;

Multiply and Assign Operator (*=):

The operator is used to multiply the left operand with the right operand and then assign result to the left operand.

Syntax:

A *= B;

(Or)

A = A * B;

Divide and Assign Operator (/=):

An operator is used between the left and right operands, which divides the first number by the second number to return the result in the left operand.

Syntax

A /= B;

(Or)

A = A / B;

Modulus and Assign Operator (%=):

An operator used between the left operand and the right operand divides the first number (n1) by the second number (n2) and returns the remainder in the left operand.

Syntax:

A %= B;

(Or)

A = A % B;

Example

#include<stdio.h>
main()
{
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %= Operator Example, Value of c = %d\n", c );

}

OUTPUT:

Line 1 - =  Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store