Home » Assignment Operator in C

Assignment Operator in C

by Online Tutorials Library

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:

Assignment Operator in C

Simple Assignment Operator (=):

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

Syntax

Let’s create a program to use the simple assignment operator in C.

Program1.c

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 used to add the left side operand to the left operand and then assign results to the left operand.

Syntax

Let’s create a program to use the Plus and assign operator in C.

Program2.c

Output

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

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

Let’s create a program to use the Subtract and Assign (-=) operator in C.

Program3.c

Output

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

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

Let’s create a program to use the multiply and assign operator (*=) in C.

Program4.c

Output

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

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

Let’s create a program to use the divide and assign operator (/=) in C.

Program5.c

Output

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

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

Let’s create a program to use the divide and assign operator (%=) in C.

Program6.c

Output

Enter the value of n1: 23     Enter the value of n2: 5     The modulus value of n2: 3  

You may also like