Course contentsShow
← C
Lesson 51 of 1,2191. FoundationsFree lesson

Printing characters with `%c`

Printing characters with %c

What you'll learn: How to use the %c format specifier to display individual letters, symbols, and numbers as character data.

The character container

In C, a character (or char) is a single building block of text. While you have already used %d to print whole numbers, printing a character requires a different "placeholder" inside your printf function. We use %c for this purpose.

Think of %c as a specific slot in a sentence that is only the size of a single key on your keyboard. If you try to put a whole word into that slot, it won't fit; if you try to put a large number there, C will get confused and try to translate that number into a symbol.

Using single quotes

When we work with characters in code, we wrap them in single quotes (like 'A' or '!'). This tells the C compiler, "This is a single character, not a variable name or a piece of text."

When printf sees the %c inside your double-quoted string, it looks at the list of variables you provided after the comma and grabs the character you’ve stored there.

Code example

Here is how you display a single character stored in a variable:

#include <stdio.h>

int main() {
    char grade = 'A';
    char symbol = '$';

    printf("Your current grade is: %c\n", grade);
    printf("The price starts with a %c symbol.\n", symbol);

    return 0;
}

In this example, the %c acts as a scout. When the program runs, the scout finds the value of grade and swaps itself out for the letter A.

Why not just type the letter?

You might wonder why we don't just type A directly into the printf statement. Using %c allows your program to be dynamic. For instance, a player’s initial might change, or a menu selection might depend on what the user typed. By using a placeholder, your text remains a template that can show different characters depending on the situation.

Key takeaway

Use the %c specifier within printf to display a single char variable wrapped in single quotes.