Assignment Operators in C

Assignment Operators in C

Assignment Operators in C are the symbols that are used to store values in

variables. These operators perform simple value assignments and also combine with arithmetic and bitwise operators. In this article, we will discuss what an assignment operator in C is, its types with examples in C, chaining assignment operators, and multiple assignment operators in C with different operations.

Table of Contents:

What is an Assignment Operator in C?

The assignment operator in C is an operator that is used to assign a value to a variable. It takes the value on the right side of the operator and stores it in the variable on the left side.

Syntax:

variable = expression;

Here, the = symbol is the assignment operator, and the expression is evaluated, then its result is assigned to the variable on the left.

<

Types of Assignment Operators in C

There are various types of assignment operators in C, which are made up by combining the arithmetic and assignment operators and the bitwise and assignment operators.

Operator NameSymbolDescriptionSyntax Example
Simple Assignment=Assigns the value of b to a.a = b;
Addition and Assignment+=Adds b to a and stores the result in a.a += b;
Subtraction and Assignment-=Subtracts b from a and stores the result in a.a -= b;
Multiplication and Assignment*=Multiplies a by b and stores the result in a.a *= b;
Division and Assignment/=Divides a by b and stores the result in a.a /= b;
Modulus and Assignment%=Stores the remainder of a divided by b in a.a %= b;
Left Shift and Assignment<<=Shifts the bits of a to the left by b positions and stores the result in a.a <<= b;
Right Shift and Assignment>>=Shifts the bits of a to the right by b positions and stores the result in a.a >>= b;
Bitwise AND and Assignment&=Performs bitwise AND between a and b, and stores the result in a.a &= b;
Bitwise XOR and Assignment^=Performs bitwise XOR between a and b and stores the result in a.a ^= b;
Bitwise OR and Assignment|=Performs bitwise OR between a and b and stores the result in a.a |= b

Let’s discuss each of the types in brief with the examples in C:

Simple Assignment (=) Operator 

The simple assignment operator (=) in C is used to assign a value to a variable. It assigns the value to the variable on the left side.

Example:

C

Output:

Simple Assignment

The code shows how the simple assignment operator assigns values to the variables x and y, calculates their sum into z, and then prints the values to the console.

Addition and Assignment (+=) Operator 

The addition and assignment operator (+=) in C adds the right-hand value to the left-hand variable and then stores the result in that same variable.

Example:

C

Output:

Addition and Assignment

The code shows how the addition and assignment operator is used to add the value 10 to the variable a and also to update the value from 5 to 15.

Subtraction and Assignment (-=) Operator

The subtraction and assignment operator (-=) in C subtracts the right-hand value from the left-hand variable and then stores the result in the same variable.

Example:

C

Output:

Subtraction and Assignment

The code shows how the subtraction and assignment operator is used to subtract the value 4 from the variable a, and then the result 16 gets printed to the console.

Multiplication and Assignment (*=) Operator

The multiplication and assignment operator (*=) in C multiplies the left-hand side variable by the right-hand side value and then stores the result in the same variable.

Example:

C

Output:

Multiplication and Assignment

The code shows how the multiplication and assignment operator is used to multiply the variable a by the value 4 and then prints the result, 24, to the console.

Division and Assignment (/=) Operator

The division and assignment (/=) operator in C divides the left-hand variable by the right-hand side value and then stores the result in the same variable.

Example:

C

Output:

image 758

The code shows how the division and assignment operator is used to divide the left-hand variable a by the right-hand side value 4, and then the result 5 gets printed to the console.

Modulus and Assignment (%=) Operator 

The modulus and assignment operator (%=) in C calculates the remainder when the left-hand variable is divided by the right-hand value and stores the result in the same variable.

Example:

C

Output:

Modulus and Assignment

The code shows how the modulus assignment operator is used to store the remainder 2 of the division in a variable a and then printed it to the console as an output.

Left Shift and Assignment (<<=) Operator 

The left shift and assignment operator (<<=) in C shifts the bits of the left-hand variable to the left by the number of positions specified on the right-hand side and stores the result in the variable.

Example:

C

Output:

Left Shift and Assignment

The code shows how the left-shift and assignment operator is used to shift the bits of the variable a to the left by one position, and then the result 10 is printed to the console.

Right Shift and Assignment (>>=) Operator 

The right shift and assignment operator (>>=) in C shifts the bits of the left-hand variable to the right by the number of positions specified on the right-hand side and stores the result back in the variable.

Example:

C

Output:

right shift and assignment

The code shows how the right-shift and assignment operator is used to shift the variable a by two positions to the right, and then the result 5 is printed to the console.

Bitwise AND and Assignment (&=) Operator

The bitwise AND and assignment operator (&=) in C is used to perform a bitwise AND operation between the left-hand variable and the right-hand value and then store the result back in the variable.

Example:

C

Output:

Bitwise AND and Assignment

The code shows how the bitwise AND assignment operator is used to perform a bitwise AND between a and b and to store the result 8 in a.

Bitwise XOR and Assignment (^=) Operator 

The bitwise XOR and assignment operator (^=) in C is used to perform a bitwise XOR operation between the left-hand variable and the right-hand value and then store the result back in the variable.

Example:

C

Output:

Bitwise XOR and Assignment

The code shows how the bitwise XOR assignment operator is used to perform a bitwise XOR between a and b and to store the result 6 in a.

Bitwise OR and Assignment (|=) Operator 

The bitwise OR and assignment operator (|=) in C is used to perform a bitwise OR operation between the left-hand variable and the right-hand value and then store the result back in the variable.

Example:

C

Output:

Bitwise OR and Assignment

The code shows how the bitwise OR assignment operator is used to perform a bitwise OR between a and b and to store the result, 14 in a.

Chaining Assignment Operator in C

A chaining assignment in C is used to assign the same value to multiple variables in a single statement using the simple assignment operator (=). The assignment operator can be chained because it returns the value that is assigned to it.

Example:

C

Output:

Chaining Assignment

The code shows how the value 10 is assigned to the variables c, b, and a in a single statement by chaining the simple assignments.

Multiple Assignments in C with Different Operations

You can perform multiple assignments in a single statement where each variable is assigned a different value, thus, you can do different operations.

Example:

C

Output:

Bitwise OR and Assignment

The code shows how multiple assignments with different operations are calculated using a single statement:
a = 5. Then, b is calculated as a+10, and c is calculated as b*2, and the results are printed to the console.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

From the above discussion, we can conclude that assignment operators in C programming play an important role as they help you to store values in variables and perform operations. There are simple and compound types of assignment operators that help to optimize the code and make it readable. So, by understanding these operators with the C examples, you can easily write efficient code using the assignment operators.

Q1. What is = in C?

It is the assignment operator, which is used to assign a value to a variable.

Q2. Is = the same as ==?

No, they are not the same, as = assigns a value, and == checks equality.

Q3. Can I assign the same value to multiple variables?

Yes, you can assign the same value to multiple variables using chained assignment like a = b = c = 10;.

Q4. Are assignment operators right-associative?

Yes, the assignment operators are right-associative, as they evaluate from right to left.

Q5. Can I use assignment operators with all data types?

Yes, but you can use bitwise and shift operators only for integers.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner