C programming += – Assignment Operators in C Programming

C programming +=: Assignment Operators in C is used to assign a value to a variable. “=” is called simple assignment operator of C, it assigns values from right side operands(R value) to left side operand (L value).

The general syntax of assignment operator is:

variable_name

=

 expression;

For Example

  • value = 1234;
  • value = 4/2;

C also supports compound assignment operators or shorthand assignment operators. These operators are combination of arithmetic and assignment operators.
They first perform arithematic operation between left and right operand and then assign the result to left operand. It first add 5 to A, and then assign result to A.

For Example

  • A+=5; is equivalent to A = A+5;
    It first add 5 to A, and then assign result to A.

Below is the list of Shorthand Assignment Operators Supported in C.

Operator Example Equivalent Expression Description
+= A += B; A = A+B; It Adds A and B, then assign result to A
-= A -= B; A = A-B; It subtract B from A, then assign result to A
/= A /= B; A = A/B; It divides A by B, then assign result to A
*= A *= B; A = A*B; It multiply A and B, then assign result to A
%= A %= B; A = A%B; It takes modulus of A by B, then assign result A
&= A &= B; A = A&B; It does bitwise AND between A and B, then assign result to A
|= A |= B; A = A|B; It does bitwise OR between A and B, then assign result to A
^= A ^= B; A = A^B; It does bitwise XOR between A and B, then assign result to A
>>= A >>= B; A = A>>B; It does bitwise right shift, then assign result to A
<<= A <<= B; A = A<<b;< td=””></b;<> It does bitwise left shift, then assign result to A

C Program to show use of Shorthand Assignment Operators

Assignment Operators in C Programming

#include<stdio.h>
#include<conio.h>
 
int main() {
   int left_Op;
 
   left_Op = 10;
   /* A+=B; is equivalent to A = A+B;*/
   left_Op += 5;
   printf("Result = %d\n", left_Op);
 
   left_Op = 10;
   /* A-=B; is equivalent to A = A-B;*/
   left_Op -= 5;
   printf("Result = %d\n", left_Op);
 
   left_Op = 10;
   /* A/=B; is equivalent to A = A/B;*/
   left_Op /= 5;
   printf("Result = %d\n", left_Op);
 
   left_Op = 10;
   /* A%=B; is equivalent to A = A%B;*/
   left_Op %= 5;
   printf("Result = %d", left_Op);
 
   getch();
   return 0;
}

Output

Result = 15
Result = 5
Result = 2
Result = 0