Assignment Operator In C

Example

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

Simple Assignment Operator (=):

It is the operator employed to assign the value of the right-hand operand or variable to the left-hand variable.

Syntax

Example

int a = 5;
or int b = a;
ch = 'a';

Let's develop a program to utilize the basic assignment operator in the C programming language.

Program1.c

Example

#include <stdio.h>
#include <conio.h>
int main ()
{
	// initialize variables
	int n1, n2, c, x, y;
	n1 = 5;
	n2 = n1;
	c = n1 + n2;
	x = 20 / 4 * 2 + 5;
	printf (" \n The value of n1: %d", n1);
	printf (" \n The value of n2: %d", n2);
	printf (" \n The value of c: %d", c);
	printf (" \n The value of x: %d", x);
	return 0;
}

Output

Output

The value of n1: 5
 The value of n2: 5
 The value of c: 10
 The value of x: 15

Plus and Assign Operator (+=):

The operator is employed to sum the operand on the left side with the operand on the right side, and then assign the outcome to the left operand.

Syntax

Example

A += B;
Or 
A = A + B;

Let's develop a program in C that utilizes the addition and assignment operators.

Program2.c

Example

#include <stdio.h>
#include <conio.h>
int main ()
{
	// initialize variables
	int n1, n2, c;
	n1 = 5;
	n2 = 10;
	n2 += n1;
	printf (" \n The value of n1: %d", n1);
	printf (" \n The value of n2: %d", n2);

	return 0;
}

Output

Output

The value of a: 5
 The value of b: 15

Subtract and Assign Operator (-=):

The operator subtracts the value of the right operand from the value of the left operand and then stores the result back in the left operand.

Syntax

Example

A -= B;
Or 
A = A - B;

Let's develop a code snippet to implement the Subtract and Assign (-=) operator in the C programming language.

Program3.c

Example

#include <stdio.h>
#include <conio.h>
int main ()
{
	// initialize variables
	int n1, n2, c;
	n1 = 5;
	n2 = 10;
	n2 -= n1; // Use Subtract and Equal operator (b = b - a)
	printf (" \n The value of n1: %d", n1);
	printf (" \n The value of n2: %d", n2);

	return 0;
}

Output

Output

The value of n1: 5
 The value of n2: 5

Multiply and Assign Operator (*=)

The operator is employed to multiply the value of the left operand by the value of the right operand, and then assign the result back to the left operand.

Syntax

Example

A *= B;
Or 
A = A * B;

Let's develop a code snippet to utilize the multiplication assignment operator (*=) in the C programming language.

Program4.c

Example

#include <stdio.h>
#include <conio.h>
int main ()
{
	// initialize variables
	int n1, n2, c;
	n1 = 5;
	n2 = 10;
	n2 *= n1; // Use Multiply and Equal operator (b = b * a)
	printf (" \n The value of n1: %d", n1);
	printf (" \n The value of n2: %d", n2);

	return 0;
}

Output

Output

The value of n1: 5
 The value of n2: 50

Divide and Assign Operator (/=):

An operator is employed to divide the first operand by the second operand, producing the quotient in the left-hand operand.

Syntax

Example

A /= B;
Or 
A = A / B;

Let's develop a C program that utilizes the divide and assign operator (/=).

Program5.c

Example

#include <stdio.h>
#include <conio.h>
int main ()
{
	// initialize variables
	int n1, n2, c;
	n1 = 5;
	n2 = 10;
	n2 /= n1; // Use divide and Equal operator (b = b / a)
	printf (" \n The value of n1: %d", n1);
	printf (" \n The value of n2: %d", n2);

	return 0;
}

Output

Output

The value of n1: 5
 The value of n2: 2

Modulus and Assign Operator (%=):

An operator positioned between the left-hand operand and the right-hand operand divides the initial numerical value (n1) by the subsequent numerical value (n2) and provides the remainder as the result in the left operand.

Syntax

Example

A %= B;
Or 
A = A % B;

Let's develop a program in C that employs the modulo assignment operator (%=) to perform division and assignment operations.

Program6.c

Example

#include <stdio.h>
#include <conio.h>
int main ()
{
	// initialize variables
	int n1, n2, c;
	
	printf (" Enter the value of n1: ");
	scanf ("%d", &n1);
	
	printf (" \n Enter the value of n2: ");
	scanf ("%d", &n2);
	n1 %= n2; // Use modulus and Equal operator (a = a % b)
	printf (" \n The modulus value of n1: %d", n1);

	return 0;
}

Output

Output

Enter the value of n1: 23

 Enter the value of n2: 5

 The modulus value of n2: 3

Input Required

This code uses input(). Please provide values below: