Assignment Operators are used to assign a value of an expression or a value of a variable to another variable.
The most commonly used assignment operator is "=". There are two different Assignment Operators are available in C 1.Compound Assignment, 2.Multiple Assignment.
Syntex
variable = expression (or) value;The '=' operator is the simple Assignment operator. Example:
#include<stdio.h>
int main()
{
int x; // Declaration of a variable
x=5; // Assignment Operator
printf("x=%d",x);
return 0;
}
Compound Assignment
Compound Assignment Operator is to assign a value to a variable in order to assign a new value to a variable arter performing a specifed operation.Operator | Example | Meaning |
+ = | X + = Y | X = X + Y |
- = | X - = Y | X = X - Y |
* = | X * = Y | X = X * Y |
/ = | X / = Y | X = X / Y |
% = | X % = Y | X = X % Y |
#include<stdio.h>
int main()
{
int x=5,y=5; // Declaration of a variable
x+=y; // Compound Assignment X = X + Y
printf("x=%d",x);
return 0;
}
Multiple Assignment
Using Multiple Assignment we can assign a single value or an expression to multiple variables. Example:#include<stdio.h>
int main()
{
int x,y,z=5; // Declaration of a variable
x=y=z; // Multiple Assignment X=Y=Z(5)
printf("x=%d,y=%d,z=%d",x,y,z);
return 0;
}