C Assignment Operators

In this lesson, you will learn about Assignment Operators in C, and their usage, along with examples to better understand the topic.


What are assignment Operators?

The assignment operator is the sign equal to =. However, it indicates that the left operand takes and allocates the value of the expression on the right operand. That is called value assigned. For Example,  x = 14 is to assign the variable x the value 14.

The table below illustrates the Assignment Operators in C

Assignment Operators SymbolExplanationExample
=a = b + c will allocate the value of b + c into
a
a = b + c
+=a += b is same to a = b + aa+=b
-=a -= b is same to a = a - b a-=b
*=a *= b is same to a = a * ba*=b
/=a /= b is same to a = a / ba/=b
%=a %= b is same to a = a % ba%=b
.=a .= b is same to a = a . ba.=b
&=a &= b is same to a = a & ba&=b
|=a |= b is same to a = a | ba|=b
^=a ^= b is same to a = a ^ ba^=b
<<=a <<= b is same to a = a << ba <<= b
>>=a >>= b is same to a = a >> ba >>= b

Example of Assignment Operators in C

#include <stdio.h>

int a, b, c;
int main() {
  printf("C Assignment Operator Example");
  printf("\n");
  //Add and += operator adds right operand to the left operand and allocate the result to left operand
  a = 84;
  c = 0;
  printf("a = %d \nc = %d\n", a, c);
  c += a;
  printf("Addition and operation-> +=\n");
  printf("Expression -> c += a\n");
  printf("value of a added to c and allocated to c\n");
  printf("Result -> %d\n", c);
  //Subtract and -= operator subtract right operand to the left operand and allocate the result to left operand
  a = 3;
  c = 83;
  printf("a = %d \nc = %d\n", a, c);
  c -= a;
  printf("Subtract and operation-> -=\n");
  printf("Expression -> c -= a\n");
  printf("value of a subtracted to c and allocated to c\n");
  printf("Result -> %d\n", c);
  //Multiply and -= operator multiply right operand to the left operand and allocate the result to left operand
  a = 11;
  c = 3;
  printf("a = %d \nc = %d\n", a, c);
  c *= a;
  printf("Multiply and operation-> -=\n");
  printf("Expression -> c *= a\n");
  printf("value of a multiplied to c and allocated to c\n");
  printf("Result -> %d\n", c);
  //Division and /= operator divide right operand to the left operand and allocate the result to left operand
  a = 5;
  c = 10;
  printf("a = %d \nc = %d\n", a, c);
  c /= a;
  printf("Division and operation-> /=\n");
  printf("Expression -> c /= a\n");
  printf("value of a divided to c and allocated to c\n");
  printf("Result -> %d\n", c);
  //Modulus and %= operator modulus right operand to the left operand and allocate the result to left operand
  a = 5;
  c = 3;
  printf("a = %d \nc = %d\n", a, c);
  c %= a;
  printf("Division and operation-> %=\n");
  printf("Expression -> c %= a\n");
  printf("value of a modulus to c and allocated to c\n");
  printf("Result -> %d\n", c);
  return 0;
}

Output

C Assignment Operator Example
a = 84
c = 0
Addition and operation-> +=
Expression -> c += a
value of a added to c and allocated to c
Result -> 84
a = 3
c = 83
Subtract and operation-> -=
Expression -> c -= a
value of a subtracted to c and allocated to c
Result -> 80
a = 11
c = 3
Multiply and operation-> *=
Expression -> c *= a
value of a multiplied to c and allocated to c
Result -> 33
a = 5
c = 10
Division and operation-> /=
Expression -> c /= a
value of a divided to c and allocated to c
Result -> 2
a = 5
c = 3
Modulus and operation-> %=
Expression -> c %= a
value of a modulus to c allocated to c
Result -> 3

In the next lesson, you will learn in detail about the if statement, a conditional statement we have seen in this course a few times.