In C, assignment operators are used to assign values to variables. They combine the operation of assigning a value to a variable with an arithmetic or bitwise operation. The basic assignment operator is the equal sign (=), and there are compound assignment operators that perform the operation and assignment simultaneously.
1. Basic Assignment Operator (=):
Definition:
The basic assignment operator is used to assign the value on the right-hand side to the variable on the left-hand side.
Syntax:
variable = expression;
Example:
#include <stdio.h>
int main() {
int x, y;
x = 5; // Assigning the value 5 to variable x
y = x + 3; // Assigning the result of x + 3 to variable yprintf(“x: %dn”, x);
printf(“y: %dn”, y);return 0;
}
Output:
x: 5
y: 8
2. Compound Assignment Operators (+=, -=, *=, /=, %=):
Definition:
These operators combine an arithmetic operation with assignment.
Syntax:
variable op= expression;
Example:
#include <stdio.h>
int main() {
int a = 1, b = 0;int result = (a || b);
printf(“(%d || %d) evaluates to %dn”, a, b, result);
return 0;
}
Output:
a += b: 13
a -= b: 10
a *= b: 30
a /= b: 10
a %= b: 1
.
Compound assignment operators are a shorthand way of expressing operations followed by assignment, making the code more concise.