C Addition Assignment Operator

In C, the Addition Assignment += operator is a compound assignment operator that adds the right operand to the left operand and assigns the result to the left operand.

Addition Assignment is a shorthand for writing variable = variable + value. The += operator is commonly used in loops, arithmetic operations, and counter updates.


Syntax of the Addition Assignment Operator

The syntax to use the Addition Assignment operator is:

</>
Copy
variable += value;

Explanation:

  1. variable: The left operand that stores the result.
  2. +=: The addition assignment operator.
  3. value: The right operand to be added to variable.

Examples of the Addition Assignment Operator

1. Using Addition Assignment on an Integer

In this example, we will use the += operator to add a value to an integer variable.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num = 10;

    num += 5; // Equivalent to num = num + 5

    printf("Updated value of num: %d\n", num);

    return 0;
}

Explanation:

  1. We declare an integer variable num and initialize it to 10.
  2. We use the addition assignment operator += to add 5 to num.
  3. The updated value of num becomes 10 + 5 = 15.
  4. The final value of num is printed using printf().

Output:

Updated value of num: 15

2. Using Addition Assignment in a Loop

In this example, we will use the += operator in a loop to calculate the sum of numbers from 1 to 5.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int sum = 0;

    for (int i = 1; i <= 5; i++) {
        sum += i; // Adds i to sum in each iteration
    }

    printf("Sum of numbers from 1 to 5: %d\n", sum);

    return 0;
}

Explanation:

  1. We declare an integer variable sum and initialize it to 0.
  2. We use a for loop to iterate from 1 to 5.
  3. Inside the loop, sum += i adds the current value of i to sum.
  4. The loop continues until i reaches 5, and the total sum is stored in sum.
  5. The final value of sum is printed using printf().

Output:

Sum of numbers from 1 to 5: 15

3. Using Addition Assignment with Floating-Point Numbers

In this example, we will use the += operator with floating-point numbers.

main.c

</>
Copy
#include <stdio.h>

int main() {
    float price = 100.50;

    price += 9.75; // Adding a floating-point value

    printf("Updated price: %.2f\n", price);

    return 0;
}

Explanation:

  1. We declare a floating-point variable price and initialize it to 100.50.
  2. We use the addition assignment operator += to add 9.75 to price.
  3. The updated value of price becomes 100.50 + 9.75 = 110.25.
  4. The final value of price is printed using printf() with two decimal places.

Output:

Updated price: 110.25

Conclusion

In this tutorial, we covered the Addition Assignment += operator in C:

  1. The += operator is used to add a value to a variable and assign the result to the same variable.
  2. It is a shorthand for variable = variable + value.
  3. It can be used with different data types, such as integers and floating-point numbers.
  4. It is commonly used in loops, counters, and arithmetic operations.