• Home
  • 3.7 Compound Assignment Operators

3.7 Compound Assignment Operators

View Categories

3.7 Compound Assignment Operators

Table of Contents

Compound assignment operators combine an arithmetic or bitwise operation with assignment. They modify the value of the left operand using the value on the right.

#include <stdio.h>

int main()
{
    int number = 10;

    number += 5;
    printf("After += : %d\n", number);

    number -= 3;
    printf("After -= : %d\n", number);

    number *= 2;
    printf("After *= : %d\n", number);

    number /= 4;
    printf("After /= : %d\n", number);

    number %= 3;
    printf("After %%= : %d\n", number);

    return 0;
}

Example Output #

After += : 15
After -= : 12
After *= : 24
After /= : 6
After %= : 0

Explanation #

For example:

number += 5;

is equivalent to:

number = number + 5;

The common compound assignment operators are:

Operator Equivalent operation
+= a = a + b
-= a = a - b
*= a = a * b
/= a = a / b
%= a = a % b
&= a = a & b
` =`
^= a = a ^ b
<<= a = a << b
>>= a = a >> b

The operator performs the specified operation and stores the resulting value back in the left operand.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *