Assignment Operator


What is operator ?

The operator is a sign used to perform some specific operations on data. There are three basic types of operators, which are as follows.

  • Operator is a sign which is used to perform some specific operations on data. There are three basic types of operators which are as follows.
  • Binary Operator
  • Ternary Operator
  • Unary Operator: This operator has only one operand. E.g. Increment or Decrement Operator. (++, --).
  • Binary Operator: This operator contains two operands at its both sides. E.g. Arithmetic Operator, Assignment Operator, Logical Operator, Relational Operator.
  • Ternary Operator: In this operator, there are three operands. E.g. Conditional Operator.

Assignment Operator

The assignment operator is a binary operator where two operands are used. The (=) sign denotes the assignment operator. This operator is used to assign some value to any variable or copy value from one variable to another.

E.g. A = 10; // Here value 10 gets assigned to variable A.
B = A; // Here value of variable A gets copied in variable B.

Combination Operator using “=” Operator

  • += Operator : This is a combination of + and = operator.
    E.g. a+=b; // Can be written as a = a+b;
  • -= Operator : This is a combination of - and = operator.
    E.g. a-=b; // Can be written as a = a-b;
  • *= Operator : This is a combination of * and = operator.
    E.g. a*=b; // Can be written as a = a*b;
  • /= Operator : This is a combination of / and = operator.
    E.g. a/=b; // Can be written as a = a/b;

Example of Assigment operator:

#include<stdio.h> #include<conio.h> void main() { int total=0,n; for(n=0;n<10;n++) { total+=n; /* total=total+n */ } printf("total = %d",total); getch(); }

output:

total = 45