Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In programming, an assignment operator assigns a value to the variable. An assignment operators in C is a binary operator that assigns a value of the right operand to the left operand.
Alright! Let us learn about the assignment operators in C programming with syntax and examples. So without any further ado, let's learn.
What is the 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 Operators 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)
Table of Assignment Operators in C
Operator Name
Operator
Equivalent Of
Simple Assignment
=
a = b
Plus and Assign
+=
a = a + b
Subtract and Assign
-=
a = a - b
Multiply and Assign
*=
a = a * b
Divide and Assign
/=
a = a / b
Modulus and Assign
%=
a = a % b
Bitwise AND and Assign
&=
a = a & b
Bitwise OR and Assign
`
=`
Bitwise XOR and Assign
^=
a = a ^ b
Left Shift and Assign
<<=
a = a << b
Right Shift and Assign
>>=
a = a >> b
Types of Assignment Operators in
Simple Assignment Operator in C
Plus and Assign Operator (+=)
Subtract and Assign Operator (-=)
Multiply and Assign Operator (*=)
Divide and Assign Operator (/=)
Modulus and Assign Operator (%=)
Bitwise XOR assignment ( ^= )
Bitwise OR assignment ( |= )
Bitwise AND assignment ( &= )
Bitwise right shift assignment ( >>= )
Bitwise left shift assignment ( <<= )
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.
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
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
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
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
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
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
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.
The Bitwise XOR Assignment operator (^=) in C performs a bitwise XOR operation between the variable and the given expression, then assigns the result back to the variable.
Syntax
variable ^= expression;
Program
#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: 6
return 0;
}
Explanation In this example, x ^= 3; performs a bitwise XOR operation between x (binary 0101) and 3 (binary 0011), resulting in 6 (binary 0110). The value 6 is then assigned back to x.
Bitwise OR Assignment (|=)
The Bitwise OR Assignment operator (|=) in C performs a bitwise OR operation between the variable and the expression, then assigns the result back to the variable.
Syntax
variable |= expression;
Program
#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: 7
return 0;
}
Explanation In this example, x |= 3; performs a bitwise OR operation between x (binary 0101) and 3 (binary 0011), resulting in 7 (binary 0111). The value 7 is then assigned back to x.
Bitwise AND Assignment (&=)
The Bitwise AND Assignment operator (&=) in C performs a bitwise AND operation between the variable and the expression, then assigns the result back to the variable.
Syntax
variable &= expression;
Program
#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: 1
return 0;
}
Explanation In this example, x &= 3; performs a bitwise AND operation between x (binary 0101) and 3 (binary 0011), resulting in 1 (binary 0001). The value 1 is then assigned back to x.
Bitwise Right Shift Assignment (>>=)
The Bitwise Right Shift Assignment operator (>>=) in C shifts the bits of the variable to the right by the specified number of positions, then assigns the result back to the variable.
Syntax
variable >>= number_of_positions;
Program
#include <stdio.h>
int main() {
int x = 8;
x >>= 2; // Equivalent to: x = x >> 2;
printf("The value of x is: %d\n", x); // Output: The value of x is: 2
return 0;
}
Explanation In this example, x >>= 2; shifts the bits of x (binary 1000) two positions to the right, resulting in 2 (binary 0010). The value 2 is then assigned back to x.
Bitwise Left Shift Assignment (<<=)
The Bitwise Left Shift Assignment operator (<<=) in C shifts the bits of the variable to the left by the specified number of positions, then assigns the result back to the variable.
Syntax
variable <<= number_of_positions;
Program
#include <stdio.h>
int main() {
int x = 2;
x <<= 3; // Equivalent to: x = x << 3;
printf("The value of x is: %d\n", x); // Output: The value of x is: 16
return 0;
}
Explanation In this example, x <<= 3; shifts the bits of x (binary 0010) three positions to the left, resulting in 16 (binary 10000). The value 16 is then assigned back to x.
Frequently Asked Questions
Which can not be used as assignment operator * 1 point += -= *= ==?
The == operator cannot be used as an assignment operator, as it is a comparison operator used to check equality, not assign values.
What are assignment operators used for?
Assignment operators are used to assign values to variables, often combining an operation (like addition or multiplication) with the assignment in a single step.
What is the assignment operator 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.