Course contentsShow
← C
Lesson 193 of 1,2193. Operators and ExpressionsFree lesson

Compound subtraction `-=`

Compound subtraction -=

What you'll learn: How to shorten your code by using a single operator to subtract a value from a variable and update it at the same time.

The Shortcut for Updating

In programming, we frequently need to take an existing number and lower it. If you were writing a game, you might want to decrease a player's health when they take damage. In standard math notation, you might write health = health - 10. While this is perfectly valid C code, it feels a bit repetitive to type the word health twice just to change its value.

The compound subtraction operator, written as -=, is a handy shortcut that does exactly the same thing but with less typing. It tells the computer: "Take the current value of the variable on the left, subtract the amount on the right from it, and save the new result back into the variable."

A Concrete Analogy: The Cookie Jar

Think of a cookie jar. If the jar is your variable, its "value" is the number of cookies inside.

If you want to remove three cookies, you don't build a brand-new jar. You simply reach into the existing jar, take three out, and the jar now holds a new, smaller total. The -= operator is like that hand reaching into the jar. It targets the specific variable and "withdraws" a specific amount immediately.

Seeing it in Action

Here is how the syntax looks in a real C program. Notice how the operator combines the subtraction sign and the equals sign into one smooth motion.

#include <stdio.h>

int main() {
    int fuel_liters = 50;
    
    // Traditional way:
    fuel_liters = fuel_liters - 5; // Now 45
    
    // The compound way (faster to read and write):
    fuel_liters -= 10; // Now 35
    
    printf("Remaining fuel: %d liters\n", fuel_liters);
    
    return 0;
}

Why Use It?

Beyond saving a few keystrokes, compound operators make your intentions clearer to other programmers. When you use -=, you are explicitly saying "I am modifying this specific variable." In complex programs where variable names might be long (like player_experience_points), using -= prevents typos because you only have to write the name once. It keeps your logic compact and easy to scan at a glance.

Key takeaway

The -= operator subtracts the right-hand value from the left-hand variable and stores the result back in that variable in one step.