Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Assignment Operator in C
2.1.
Examples of Assignment Operator in C
3.
Simple Assignment Operator in C
3.1.
C
3.2.
Plus and Assign Operator (+=):
3.3.
C
4.
Subtract and Assign Operator (-=):
4.1.
C
4.2.
Multiply and Assign Operator (*=)
4.3.
C
4.4.
Divide and Assign Operator (/=)
4.5.
C
4.6.
Modulus and Assign Operator (%=)
4.7.
C
5.
Frequently Asked Questions
5.1.
Is it a == assignment operator?
5.2.
Why is the assignment operator used?
5.3.
What is the assignment expression?
5.4.
What happens when an assignment operator is used with an uninitialized variable in C?
5.5.
Can an assignment operator be used to compare two values in C?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Assignment Operator in C

Author Muskan Sharma
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

In programming, an assignment operator assigns a value to the variable. An assignment operator in C is a binary operator that assigns a value of the right operand to the left operand.

Assignment Operator in C

Alright! Let us learn about the assignment operator in C programming with syntax and examples. So without any further ado, let's learn.

Assignment Operator in C

The assignment operator, denoted by the equal sign (=), is a fundamental element in numerous programming languages. It serves the purpose of assigning a specific value to a variable. This principle holds true for the C programming language as well.

Examples of Assignment Operator in C

1. Assigning a Constant Value to a Variable:

int x;          // Declare an integer variable named 'x'
x = 5;         // Assign the value 5 to the variable 'x'

 

2. Assigning the Result of an Expression to a Variable:

int a = 2;
int b = 1;
int result;
result = a + b; // Assign the result of the addition to 'result'

 

3. Combining Assignment with Other Operators (e.g., Addition and Multiplication):

int num = 8;
num += 6;  // Equivalent to num = num + 6; (Add 6 to 'num')
num *= 3;  // Equivalent to num = num * 3; (Multiply 'num' by 3)
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Simple Assignment Operator in C

The simple assignment operator (=) in C assigns the value of the right operand to the left operand. It is a binary operator, meaning it operates on two operands. The left operand must be a modifiable lvalue, which means it must be a variable or an array element. The right operand can be any expression with a value and a type compatible with the left operand.

Also read - Bit stuffing program in c


Syntax

leftOperand = rightOperand;


Here, the value of rightOperand is assigned to the variable or array element leftOperand. The type of rightOperand must be compatible with the type of leftOperand.


Program

  • C

C

#include <stdio.h>
int main() {
int x; // Declare an integer variable named 'x'
x = 8; // Assign the value 8 to 'x'

printf("The value of x is: %d\n", x); // Output: The value of x is: 8

return 0;
}

 

Output

8

 

Explanation

In this example, we declare an integer variable x and then use the assignment operator to give it the value 8. Finally, we print the value of x, which will be 10.

Plus and Assign Operator (+=):

Many programming languages, including C, use the compound assignment operator known as the "Plus and Assign" operator (+=). In a single operation, it combines addition and assignment.


Syntax

variable += expression;

 

Program

  • C

C

#include <stdio.h>
int main() {
int x = 5;
x += 3; // Equivalent to: x = x + 3;

printf("The value of x is: %d\n", x); // Output: The value of x is: 8

return 0;
}


Output

8

 

Explanation

In this example, x += 3; adds 3 to the current value of x, which is 5. The result (8) is then assigned back to x. The variable x now holds the value 8.

Subtract and Assign Operator (-=):

In several programming languages, including C, the "Subtract and Assign" operator (-=) is a compound assignment operator. It combines assignment and subtraction into a single operation.

Syntax

variable -= expression;

 

Program

  • C

C

#include <stdio.h>
int main() {
int x = 5;
x -= 3; // Equivalent to: x = x - 3;

printf("The value of x is: %d\n", x); // Output: The value of x is: 2

return 0;
}

 

Output

2

 

Explanation

In this example, x -= 3; subtracts 3 to the current value of x, which is 5. The result (2) is then assigned back to x. The variable x now holds the value 2.

Multiply and Assign Operator (*=)

In several programming languages, including C, the "Multiply and Assign" operator (*=) is a compound assignment operator. It performs a single operation that combines multiplication and assignment.

Syntax

variable *= expression;

 

Program

  • C

C

#include <stdio.h>
int main() {
int x = 5;
x *= 3; // Equivalent to: x = x*3;

printf("The value of x is: %d\n", x); // Output: The value of x is: 15

return 0;
}

 

Output

15

 

Explanation

In this example, x *= 3; multiply 3 to the current value of x, which is 5. The result (15) is then assigned back to x. The variable x now holds the value 15.

Divide and Assign Operator (/=)

Many programming languages, including C, use the "Divide and Assign" operator (/=), a compound assignment operator. In a single operation, it combines division and assignment. 

Syntax

variable /= expression;

 

Program

  • C

C

#include <stdio.h>
int main() {
int x = 5;
x /= 5; // Equivalent to: x = x/5;

printf("The value of x is: %d\n", x); // Output: The value of x is: 1

return 0;
}

 

Output

1

 

Explanation

In this example, x /= 5; divide 5 to the current value of x, which is 5. The result (1) is then assigned back to x. The variable x now holds the value 1.

Modulus and Assign Operator (%=)

Many computer languages, including C, use the "Modulus and Assign" operator (%=), a compound assignment operator. The assignment and modulus operations are combined into a single operation.

Syntax

variable %= expression;

 

Program

  • C

C

#include <stdio.h>
int main() {
int x = 5;
x %= 5; // Equivalent to: x = x%5;

printf("The value of x is: %d\n", x); // Output: The value of x is: 0

return 0;
}

 

Output

0

 

Explanation

In this example, x %= 5; divide 5 to the current value of x, and give the remainder which is 0. The result (0) is then assigned back to x. The variable x now holds the value 0.

Also see, Floyd's Triangle in C

Frequently Asked Questions

Is it a == assignment operator?

No, the "a == assignment operator" is incorrect. Many computer languages employ the comparison operator "==" to see if two values are equal. On the other hand, the assignment operator is used to give a value to a variable and is indicated by the equal sign (=).

Why is the assignment operator used?

Programmers use the assignment operator (=) to give a value to a variable or data structure. This procedure is essential for managing and storing data inside of a program. 

What is the assignment expression?

An assignment expression is a statement used in programming to assign a value to a variable using the assignment operator (=). It incorporates a variable on the left with an expression on the right.

What happens when an assignment operator is used with an uninitialized variable in C?

Using an uninitialized variable with an assignment operator in C can lead to undefined behaviour. The variable's value is unpredictable and can be any garbage value, depending on the memory location where the variable is stored.

Can an assignment operator be used to compare two values in C?

No, we cannot use an assignment operator to compare two values in C. The assignment operator is used to assign a value to a variable, not to compare two values.

Conclusion

As we have reached the end of this blog, let us see what we have discussed so far. In this blog, we have discussed the assignment operator in C. We also learnt precedence, associativity and the importance of assignment operators in C. Now that you have learnt about the assignment operator in C, you can refer to other similar articles.


You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninja!

Live masterclass