Assignment Operator in C

Using assignment operators, we can assign value to the variables.

Equality sign (=) is used as an assignment operator in C.


Example

int var = 5;

Here, value 5 has assigned to the variable var.


Example

#include<stdio.h>

int main()
{
    int a = 10;
    int b = a;

    printf("a = %d \t b = %d\n",a,b);

    return 0;
}

Here, value of a has assigned to the variable b. Now, both a and b will hold value 10.


Basically, the value of right-side operand will be assigned to the left side operand.

Pictorial Explanation

How assignment works



Compound assignment operators

Operator

Meaning

Example
(a = 10 , b = 5)

+=

L=L+R
add left and right operand and assign result in left

a+=b;same as a=a+b
after execution a will hold 15

-=

L=L-R
subtract right operand from left operand and assign result in left

a-=b;same as a=a-b
after execution a will hold 5

*=

L=L*R
multiply both right and left operand and store result in left

a*=b;same as a=a*b
after execution a will hold 50

/=

L=L/R
divides left operand by right operand and store result in left

a/=b;same as a=a/b
after execution a will hold 2

%=

L=L%R
After left and right operand division, the remainder will be stored in left

a%=b;same as a=a%b
after execution a will hold 0




Sample Program

Example

#include<stdio.h>

int main()
{
    int a = 10, b = 5;

    a+=b; // same as a=a+b
    printf("value of a+b = %d\n",a); // a will hold the result

    return 0;
}