Assignment Operators in C

The Assignment operators in C are some of the C Programming Operator, which are used to assign the values to the declared variables.

Equals (=) operator is the most commonly used assignment operator in C. For example:

int i = 10;

Below table displays all the assignment operators present in C Programming with an example.

Assignment Operators in C

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 C Program, We are using two integer variables a, 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 C Programming Language

/* Program for Assignment Operators in C*/
#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;
}

OUTPUT

ANALYSIS In this Assignment Operators in C program example, We declared 2 integer values a Total and we assigned values 7 and 21 respectively.

Below printf statements will perform C Assignment operations on a and Total and then the output of the result will be displayed

Let us see the functionality

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)