< Prev
Next >

# C - Compound Assignment Operators

There are four compound assignment operators in C -
• +=
• -=
• *=
• /=

## += operator

This operator performs two operations in sequence -
2. Assignment of the result of add operation.

Understanding += operator with a code -
``````int i=2;        //initializing i to 2
i+=2;		//equals to, i = i+2;
``````
• Statement i+=2 is equal to i=i+2, hence 2 will be added to the value of i, which gives us 4.
• Finally, the result of addition, 4 is assigned back to i, updating its original value from 2 to 4.

A special case scenario for all the compound assigned operators
``````int i=2;
i+=2*2;		//equals to, i = i+(2*2);
``````
In all the compound assignment operators, the expression on the right side of = is always calculated first and then the compound assignment operator will start its functioning. Hence in the last code, statement i+=2*2; is equal to i=i+(2*2), which results in i=i+4, and finally it returns 6 to i.

## Example with += operator

``````/* Example with += operator */

#include<stdio.h>
int main()
{
char c='a';
printf("Original char : %c \n", c);
c+=10; 		/* c = c+10; */
printf("char after +=10 operation :  %c \n", c);

short s=10;
printf("Original short : %d \n", s);
s+=5*10; 	/* s = s+(5*10); */
printf("short after +=5*10 operation : %d \n", s);

int i=10;
printf("Original int : %d \n", i);
i+=10; 		/* i = i+10; */
printf("int after +=10 operation : %d \n", i);

float f=10.5f;
printf("Original float : %f \n", f);
f+=20; 		/* f = f+20; */
printf("float after +=20 operation : %f \n", f);

double d=22.5;
printf("Original double : %lf \n", d);
d+=30; 		/* d = d+30; */
printf("double after +=30 operation :  %lf ", d);

return 0;
}``````
Output is
``````Original char : a
char after +=10 operation :  k
Original short : 10
short after +=10 operation : 60
Original int : 10
int after +=10 operation : 20
Original float : 10.500000
float after +=20 operation : 30.500000
Original double : 22.500000
double after +=30 operation :  52.500000``````

## -= operator

This operator performs two operations in sequence -
1. Subtraction operation.
2. Assignment of the result of subtract operation.

Understanding -= operator with a code -
``````int i=2;
i-=2;
``````
• Statement i-=2 is equal to i=i-2, hence 2 will be subtracted from the value of i, which gives us 0.
• Finally, the result of subtraction i.e. 0 is assigned back to i, updating its value to 0.

• ## Example with -= operator

``````/* Example with += operator */

#include<stdio.h>
int main()
{
char c='k';
printf("Original char : %c \n", c);
c-=10; 		/* c = c-10; */
printf("char after -=10 operation :  %c \n", c);

short s=10;
printf("Original short : %d \n", s);
s-=5*10; 	/* s = s-(5*10); */
printf("short after -=5*10 operation : %d \n", s);

int i=10;
printf("Original int : %d \n", i);
i-=10; 		/* i = i-10; */
printf("int after -=10 operation : %d \n", i);

float f=10.5f;
printf("Original float : %f \n", f);
f-=20; 		/* f = f-20; */
printf("float after -=20 operation : %f \n", f);

double d=22.5;
printf("Original double : %lf \n", d);
d-=30*2; 	/* d = d-(30*2); */
printf("double after -=30*2 operation :  %lf ", d);

return 0;
}``````
Output is
``````Original char : k
char after -=10 operation :  a
Original short : 10
short after -=10 operation : -40
Original int : 10
int after -=10 operation : 0
Original float : 10.500000
float after -=20 operation : -9.500000
Original double : 22.500000
double after -=30 operation :  -37.500000``````

## *= operator

This operator performs two operations in sequence -
1. Multiplication operation.
2. Assignment of the result of multiplication operation.

Understanding *= operator with a code -
``````int i=2;       //initializing i to 2
i*=2;
``````
• Statement i*=2 is equal to i=i*2, hence 2 will be multiplied with the value of i, which gives us 4.
• Finally, the result of multiplication, 4 is assigned back to i, updating its value to 4.

• ## Example with *= operator

``````/* Example with *= operator */

#include<stdio.h>
int main()
{
char c='a';
printf("Original char : %c \n", c);
c*=1; 		/* c = c*1; */
printf("char after *=1 operation :  %c \n", c);

short s=10;
printf("Original short : %d \n", s);
s*=5+10; 	/* s = s*(5+10); */
printf("short after *=5+10 operation : %d \n", s);

int i=10;
printf("Original int : %d \n", i);
i*=10; 		/* i = i+10; */
printf("int after *=10 operation : %d \n", i);

long l=100000;
printf("Original int : %ld \n", l);
l*=1000; 	/* l = l*1000; */
printf("long after *=1000 operation : %ld \n", l);

float f=10.5f;
printf("Original float : %f \n", f);
f*=20; 		/* f = f*20; */
printf("float after *=20 operation : %f \n", f);

double d=22.5;
printf("Original double : %lf \n", d);
d*=30; 		/* d = d*30; */
printf("double after *=30 operation :  %lf ", d);

return 0;
}``````

## Output -

``````Original char : a
char after *=1 operation :  a
Original short : 10
short after *=5+10 operation : 150
Original int : 10
int after *=10 operation : 100
Original int : 100000
long after *=1000 operation : 100000000
Original float : 10.500000
float after *=20 operation : 210.000000
Original double : 22.500000
double after *=30 operation :  675.000000``````

## /= operator

This operator performs two operations in sequence -
1. Division operation.
2. Assignment of the result of division operation.

Understanding /= operator with a code -
``````int i=4;     //initializing i to 4
i/=2;
``````
• Statement i/=2 is equal to i=i/2, hence 4 will be divided by the value of i, which gives us 2.
• Finally, the result of division i.e. 2 is assigned back to i, updating its value from 4 to 2.

• ## Example with /= operator

``````/* Example with /= operator */

#include<stdio.h>
int main()
{
char c='j';
printf("Original char : %c \n", c);
c/=1; 		/* c = c/1; */
printf("char after /=1 operation :  %c \n", c);

short s=10;
printf("Original short : %d \n", s);
s/=5-2; 	/* s = s/(5-2); */
printf("short after /=5-2 operation : %d \n", s);

int i=10;
printf("Original int : %d \n", i);
i/=10; 		/* i = i/10; */
printf("int after /=10 operation : %d \n", i);

long l=999;
printf("Original int : %ld \n", l);
l/=2; 		/* l = l/2; */
printf("long after /=2 operation : %ld \n", l);

float f=80.5f;
printf("Original float : %f \n", f);
f/=20; 		/* f = f/20; */
printf("float after /=20 operation : %f \n", f);

double d=44.5;
printf("Original double : %lf \n", d);
d/=22; 		/* d = d/22; */
printf("double after /=30 operation :  %lf ", d);

return 0;
}``````

## Output-

``````Original char : j
char after /=1 operation :  j
Original short : 10
short after /=5-2 operation : 3
Original int : 10
int after /=10 operation : 1
Original int : 999
long after /=2 operation : 499
Original float : 80.500000
float after /=20 operation : 4.025000
Original double : 44.500000
double after /=30 operation :  2.022727``````   