# 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 A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳
{"ft_lang_label":"__label__en","ft_lang_prob":0.79695827,"math_prob":0.99512094,"size":3589,"snap":"2021-43-2021-49","text_gpt3_token_len":923,"char_repetition_ratio":0.23124129,"word_repetition_ratio":0.2759124,"special_character_ratio":0.29283923,"punctuation_ratio":0.14206897,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99720067,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-07T19:46:08Z\",\"WARC-Record-ID\":\"<urn:uuid:a617fc61-3fe4-4b84-b113-c02090366731>\",\"Content-Length\":\"21026\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:70ec3347-a9ec-4006-b9b3-b7fad9ed188e>\",\"WARC-Concurrent-To\":\"<urn:uuid:18225d0a-dac7-4470-81cf-2ebac7c3dfb8>\",\"WARC-IP-Address\":\"172.67.181.73\",\"WARC-Target-URI\":\"https://meshworld.in/assignment-operators-in-c/\",\"WARC-Payload-Digest\":\"sha1:IGHZDQ6KH3KSC6HAEJPJ62XSCWQSKGXQ\",\"WARC-Block-Digest\":\"sha1:ZUGWXXYLLJ63YLBY65WJKTG3C4N2IE5L\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363405.77_warc_CC-MAIN-20211207170825-20211207200825-00071.warc.gz\"}"}