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.

Assignment Operators in C

Assignment Operators

  •   “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left.

for eg:-

a=5;

b=10

a will be assigned value of 5, and b will be assigned value of 10.

  •     “+=”: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 )

Assignment Operators in C img

Implementation 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

Run

#include <stdio.h> // header files
#include <stdlib.h> // library files
int main()// main function { int a,b=10,c=5;   // initialize the value a=20; b+=10; c*=2; printf("\n%d",b); printf("\n%d",c); return 0; }

Output

20
10

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription