Assignment Operator in C
The assignment operator (=) in C programming is used to set a variable's
value. It stores the value in the variable on its left side after it taking
from its right side. The following examples show how to use the assignment
operator:
Assignment operators are:
= Simple assignment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
*= Multiplication assignment
/= Division assignment
%= Modulo assignment
Example for Simple assignment operator:
Program:
#include<stdio.h>
int main() {
int x;
x = 14;
printf("The value is: %d",x);
return 0;
}
Output:
The value is: 14
Example for Addition operator with assignment:
Program:
#include<stdio.h>
int main() {
int x = 5;
x += 3;
printf("The value of += addition operation: %d\n",
x);
}
Output:
The value of += addition operation: 8
Example for Subtraction operator with assignment:
Program:
#include<stdio.h>
int main() {
int x = 10;
x -= 2;
printf("The value of subtract operation: %d\n",
x);
}
Output:
The value of subtract operation: 8
Example for Multiplication operator with assignment:
Program:
#include<stdio.h>
int main() {
int x = 5;
x *= 4;
printf("The value of *= multiplication operation: %d\n",
x);
}
Output:
The value of *= multiplication operation: 20
Example for Division operator with assignment:
Program:
#include<stdio.h>
int main() {
int x = 15;
x /= 5;
printf("The value of /= division operation: %d\n",
x);
}
Output:
The value of /= division operation: 3
Example for Modulo operator with assignment:
Program:
#include<stdio.h>
int main() {
int x = 10;
x %= 2;
printf("The value of modulo operation: %d\n",
x);
}
Output:
The value of modulo operation: 0