Course contentsShow
← C
Lesson 690 of 1,21910. Input and OutputFree lesson

Escaping the percent sign %%

Escaping the percent sign %%

What you'll learn: How to display a literal percent symbol in your output without confusing the C compiler.

The Percent Problem

In C, the % character is special. It acts like a "reserved seat" sign in a movie theater. When printf sees a percent sign, it stops printing plain text and waits for a command, like %d for an integer or %f for a float.

But what happens if you actually want to print a percent sign? For example, if you want to tell a user, "Battery level is 85%."

If you type printf("Battery: 85%");, the compiler gets confused. It sees the % and thinks, "Okay, a special command is starting!" When it doesn't find a valid type letter (like d or s) immediately after it, your program might crash, print gibberish, or throw a warning. You can't just type one % because the compiler is always looking for a format specifier to follow it.

The Double-Up Solution

To solve this, C uses a simple "escape" rule: to print one percent sign, you must type two of them in a row: %%.

Think of it like a secret password. The first % tells printf, "Get ready, something special is coming." The second % clarifies, "The special thing I want is actually just a regular percent sign."

It’s an analogy to how we use a backslash \ to escape quotes. By doubling the symbol, you are telling C to ignore its special function and treat it as a normal character.

Seeing it in Action

Notice in the code below how the first two percent signs are used to bring in numbers, while the double %% at the end is used to show the unit.

#include <stdio.h>

int main() {
    int score = 95;
    int total = 100;

    // The first %d pulls the variable 'score'
    // The %% prints a literal '%' sign on the screen
    printf("You scored %d out of %d!\n", score, total);
    printf("Your current accuracy is 95%%.\n");

    return 0;
}

When you run this code, the output will look perfectly normal: Your current accuracy is 95%. The compiler consumes both symbols but only displays one to the user.

Key takeaway

To display a literal % in a printf statement, use a double percent sign %%.