Assignment Operators

Assignment Operators

They are used to assign the result of an expression to a variable.

Syntax

identifier = Expression

There are five assignment operators. They are,

a=a+1 a+=1

a=a- 1 a-= 1

a=a*2 a*=2

a=a/2 a/= 2

a=a%2 a%=2

Advantages:

  • Left-hand side operator need not repeated.
  • Easy to Read
  • More Efficient

Example For C assignment operator

#include <stdio.h>

// C assignment operator Example Program

void main(){
  int a = 10;

  // = Operator
  int b = a;
  printf("b = %d\n",b);

  // += Operator
  b += 10;
  printf("b += 10;b = %d\n",b);

  // -= Operator
  b -=5;
  printf("b -=5;b = %d\n",b);

  // *= Operator
  b *=4;
  printf("b *=4;b = %d\n",b);

  // /= Operator
  b /=2;
  printf("b /=2;b = %d\n",b);
}

Sample Output:

b = 10

b += 10;b = 20

b -=5;b = 15

b *=4;b = 60

b /=2;b = 30