Assignment operators in C

A tutorial showing usage of assignment operators in C - language

An assignment operator assigns a value from its right hand side expression, constant, or another variable to its left hand side operand.

The basic assignment operator is equal (=), which simply assigns the value of its right operand into its left operand. That is, x = y which assigns the value of y to x.

There are two kinds of assignment operations:

  • Simple assignment, in which the value of the second operand is stored in the object specified by the first operand.
  • Compound assignment, in which an arithmetic, shift, or bitwise operation is performed prior to storing the result.

For example, consider following C statements for int type.

int a = 12;

Here, 12 an integer value on RHS is stored in variable of integer(int) type a on LHS.

For example, consider following C statements for char type.

char IndianRupeeSymbol = '₹';

Here, '₹' a single character on RHS is stored in variable of char type IndianRupeeSymbol on LHS.

Simple assignment operator

OperatorMeaningExample
=Store the value of the second operand into the first operand.a = b

For example, consider following C statements.

char IndianRupeeSymbol = '₹';
int x = 12;
x = x + 2;

Compound assignment operators

C also has a shorter syntax for carrying out assignment tasks, using operators known as Shorthand Assignment Operators.

OperatorMeaningExample
+=Add the value of the second operand to the value of the first operand; store the result in the first operand.a += b
-=Subtract the value of the second operand from the value of the first operand; store the result in the first operand.a -= b
*=Multiply the value of the first operand by the value of the second operand; store the result in the first operand.a *= b
/=Divide the value of the first operand by the value of the second operand; store the result in the first operand.a /= b
%=Take modulus of the first operand specified by the value of the second operand; store the result in the first operand.a %= b
«=Shift the value of the first operand left the number of bits specified by the value of the second operand; store the result in the first operand.a «= b
»=Shift the value of the first operand right the number of bits specified by the value of the second operand; store the result in the first operand.a »= b
&=Obtain the bitwise AND of the first and second operands; store the result in the first operand.a &= b
^=Obtain the bitwise exclusive OR of the first and second operands; store the result in the first operand.a ^= b
|=Obtain the bitwise inclusive OR of the first and second operands; store the result in the first operand.a |= b

The expression var = var + 2 is equivalent to var += 2. In fact any expression var = var + x can be also written as var += x;

Working example

/**
 * Short Hand Assignment Operators
 */
#include <stdio.h>
int main()
{
    int a, b;
    printf(" Enter a & b : ");
    scanf("%d %d", &a, &b);

    printf(" Value of a : %d", a);
    printf("\n Value of b : %d", b);

    a += 6;
    printf("\n a += 6 : %d", a);

    a -= 4;
    printf("\n a -= 4 : %d", a);

    a *= b;
    printf("\n a *= b : %d", a);
    printf("\n Value of b : %d", b);

    a /= 3;
    printf("\n a /= 3 : %d", a);

    a %= 2;
    /**
     * We need to escape the % sign with %%
     * As there is no %= format specifier
     */
    printf("\n a %%= 2 : %d", a);

    b += a;
    printf("\n b += a : %d", b);
    printf("\n Value of a : %d", a);
    printf("\n");
    return 0;
}

Note: While displaying the symbol in printf, we need to escape the % sign with %%. As there is no %= format specifier

Vishnu Damwala
Vishnu Damwala

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳

Next
Previous

Related