• Home
  • Assignment Operators in C language

Assignment Operators in C language

In C, assignment operators are used to assign a value to a variable. The following are the assignment operators in C:

  1. = (assignment): The = operator is used to assign a value to a variable. For example:
int a = 5; // a is now 5
a = 10; // a is now 10
  1. += (add and assign): The += operator is used to add a value to a variable and assign the result to the variable. For example:
int a = 5; // a is now 5
a += 10; // a is now 15
  1. -= (subtract and assign): The -= operator is used to subtract a value from a variable and assign the result to the variable. For example:
int a = 5; // a is now 5
a -= 2; // a is now 3
  1. *= (multiply and assign): The *= operator is used to multiply a variable by a value and assign the result to the variable. For example:
int a = 5; // a is now 5
a *= 2; // a is now 10
  1. /= (divide and assign): The /= operator is used to divide a variable by a value and assign the result to the variable. For example:
int a = 10; // a is now 10
a /= 2; // a is now 5
  1. %= (modulus and assign): The %= operator is used to find the remainder of a division and assign the result to the variable. For example:
int a = 10; // a is now 10
a %= 3; // a is now 1

It is important to use the correct assignment operator based on the type of assignment you want to perform. Using the wrong operator can result in errors or unexpected results in your code.