Navigate back to the homepage

Assignment operators in C

Vishnu Damwala
April 12th, 2020 · 1 min read

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.

1int 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.

1char 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.

1char IndianRupeeSymbol = '₹';
2int x = 12;
3x = 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:

1/**
2 * Short Hand Assignment Operators
3 */
4#include <stdio.h>
5int main()
6{
7 int a, b;
8 printf(" Enter a & b : ");
9 scanf("%d %d", &a, &b);
10
11 printf(" Value of a : %d", a);
12 printf("\n Value of b : %d", b);
13
14 a += 6;
15 printf("\n a += 6 : %d", a);
16
17 a -= 4;
18 printf("\n a -= 4 : %d", a);
19
20 a *= b;
21 printf("\n a *= b : %d", a);
22 printf("\n Value of b : %d", b);
23
24 a /= 3;
25 printf("\n a /= 3 : %d", a);
26
27 a %= 2;
28 /**
29 * We need to escape the % sign with %%
30 * As there is no %= format specifier
31 */
32 printf("\n a %%= 2 : %d", a);
33
34 b += a;
35 printf("\n b += a : %d", b);
36 printf("\n Value of a : %d", a);
37 printf("\n");
38 return 0;
39}

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

More articles from MeshWorld

JavaScript - String split() function

A tutorial showing the usage of split() function in JavaScript

April 5th, 2020 · 2 min read

Chrome 80 brings support for let and class re-declarations in the Console

Re-declarations of let and class keyword are now allowed in console in Chrome 80 makes debugging more developer friendly

April 3rd, 2020 · 1 min read
© 2017–2020 MeshWorld
Link to $https://twitter.com/meshworldindiaLink to $https://facebook.com/meshworldindiaLink to $https://instagram.com/meshworld.india/