Compound addition +=
What you'll learn: How to use the += operator to update a variable's value by adding to it in a single, efficient step.
The Shortcut for Growth
In programming, we rarely just set a number and leave it alone. We constantly update values: adding points to a score, increasing a counter, or moving a character across a screen.
Standard addition looks like this: balance = balance + 50;. While this works, it’s a bit repetitive. You are essentially telling the computer, "Look at the current balance, add 50 to it, and then overwrite the old balance with this new total." The compound addition operator (+=) turns this into a sleek shortcut.
How it works
The += operator combines addition and assignment into one symbol. Instead of writing the variable name twice, you write it once, follow it with +=, and then provide the amount you want to add.
Think of it like a piggy bank. You don't take all the money out, add a dollar to the pile, and buy a brand-new piggy bank to put it in. You simply drop the dollar through the slot. The += operator is that slot—it modifies what is already inside.
Code Example
See how much cleaner the code becomes when we use the compound version to track a player's score:
#include <stdio.h>
int main() {
int score = 100;
// The long way
score = score + 50;
// score is now 150
// The compound way (cleaner!)
score += 50;
// score is now 200
printf("Final score: %d", score);
return 0;
}
Why use it?
Professional C programmers use += for two reasons: readability and intent. When you see score += 10;, your brain instantly recognizes that the score is growing. It eliminates the clutter of repeating the variable name, which reduces the chance of typos—especially when your variable names are long, like player_health_points.
It is important to remember that the math happens first, and the result is stored back into the variable on the left.
Key takeaway
Use variable += value; as a shorthand to add a number to an existing variable and save the result instantly.