C Tutorial C Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C - assignment operators example



The example below shows the usage of assignment and compound assignment operators:

  • = Assignment operator
  • += Addition AND assignment operator
  • -= Subtraction AND assignment operator
  • *= Multiply AND assignment operator
  • /= Division AND assignment operator
  • %= Modulo AND assignment operator
#include <stdio.h>
 
int main (){
  float a = 25;

  printf("a = %f\n", a);

  //Addition AND assignment operator
  a += 5;
  printf("a += 5; makes a = %f\n", a);

  //Subtraction AND assignment operator
  a -= 8;
  printf("a -= 8; makes a = %f\n", a);

  //Multiply AND assignment operator
  a *= 4;
  printf("a *= 4; makes a = %f\n", a);

  //Division AND assignment operator
  a /= 2;
  printf("a /= 2; makes a = %f\n", a);

  //Modulo AND assignment operator
  //works only with integral operands
  int b = 25;
  printf("\nb = %d\n", b);
  b %= 7;
  printf("b %%= 7; makes b = %d\n", b);
  return 0;
}

The output of the above code will be:

a = 25.000000
a += 5; makes a = 30.000000
a -= 8; makes a = 22.000000
a *= 4; makes a = 88.000000
a /= 2; makes a = 44.000000

b = 25
b %= 7; makes b = 4

❮ C - Operators

5