Assignment Operators in C
Assignment Operator in C
Simple assignment operator is used to assign a value to a variable. The assignment operation evaluates to the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables.
- “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.
5 value assign will be a, and 10 value assign will be b.
This operator is shorted assignment.
- “+=”:This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on right and then assigns the result to the variable on the left.
eg; ( a+=b ) can written as( a=a+b )
- ” -=” This operator is combination ‘-‘ and ‘=’ operator. This operator subtracts the value on right from the current value of the variable on left and then assigns the result to the variable on the left.
eg; ( a-=b ) can written as( a=a-b )
Implement of Assignment Operator
- First of all in this program we will initialize the value in the variable, the we will assign the value of the right to the variable.
- like a=20 means 20 will assign to the a variable.
- b+=10 means b=b+10 and b=10 initialize so b=10+10 it means b=20 so 20 will assign to b.
- c*=2 means c=c*2 that means multiply of 2*5 because the value of 5 is c so c=10 ,then 10 will assign to the C.
Code for Assignment operator in C
#include <stdio.h>// header files
#include<stdlib.h> // library files
int main()// main function
int a,b=10,c=5; // initialize the value
Output 20 10