The Assignment operators in C are some of the Programming language operators, that are useful to assign the values to the declared variables. The equals (=) operator is the most commonly used assignment operator. For example:
int i = 10;
The below table displays all the assignment operators present in C Programming language with an example.
Assignment Operators | Example | Explanation |
---|---|---|
= | x = 25 | Value 25 is assigned to x |
+= | x += 25 | This is same as x = x + 25 |
-= | x -= 25 | This is same as x = x – 25 |
*= | x *= 25 | This is same as x = x * 25 |
/= | x /= 25 | This is same as x = x / 25 |
%= | x%= 25 | This is same as x = x % 25 |
Assignment Operators in C Example
In this assignment operators Program, We are using two integer variables a, and Total, and their values are 7 and 21. Next, we are going to use these two variables to show you the working functionality of all the Assignment Operators in this Programming Language
/* Program for Assignment Operators */
#include <stdio.h>
int main()
{
int a = 7;
int Total = 21;
printf(" Value of the Total = %d \n", Total += a );
printf(" Value of the Total = %d \n", Total -= a );
printf(" Value of the Total = %d \n", Total *= a );
printf(" Value of the Total = %d \n", Total /= a );
printf(" Value of the Total = %d \n", Total %= a );
return 0;
}
The below printf statements will perform C Programming Assignment operations on a and Total and then the output of the result will be displayed. Let us see the C Programming Operator functionality in this C Program
printf(" Value of the Total = %d \n ", Total += a );
Total += a means
Total = Total + a = 21 + 7 = 28
printf(" Value of the Total = %d \n", Total -= a );
Total -= a means
Total = Total – a = 28 – 7 = 21
printf(" Value of the Total = %d \n", Total *= a );
Total *= a means
Total = Total * a = 21 * 7 = 147
printf(" Value of the Total = %d \n", Total /= a );
Total /= a means
Total = Total / a = 147 / 7 = 21.
printf(" Value of the Total = %d \n", Total %= a );
Total %= a means
Total = Total + a = 21 % 7 = 0 (Remainder of 21/7 is = 0).