C Format Specifiers

Format specifiers are placeholders inside a format string. They tell functions such as printf and scanf what type of value to print or read.

Why Format Specifiers Matter

C needs format specifiers because the format string is just text. When you write %d, you are telling C to treat the matching value as an int. When you write %f, you are telling C to treat the matching value as a floating-point number.

The order matters. The first specifier matches the first extra value, the second specifier matches the second extra value, and so on.

Common Specifiers

Specifier Common use Example type
%d Signed whole number int
%u Unsigned whole number unsigned int
%c Single character char
%s String of characters char[]
%f Decimal number in printf double
%lf Decimal number in scanf double

Printing Different Types

Use a format specifier where the value should appear in the output. Then pass the matching value after the format string.

#include <stdio.h>

int main(void)
{
    char initial = 'A';
    int age = 31;
    double temperature = 23.5;

    printf("Initial: %c\n", initial);
    printf("Age: %d\n", age);
    printf("Temperature: %.1f\n", temperature);

    return 0;
}

Output:

Initial: A
Age: 31
Temperature: 23.5

In this program, %c prints one character, %d prints an integer, and %.1f prints a decimal number with one digit after the decimal point.

Width And Precision

Some specifiers can include extra formatting instructions. A number before the specifier sets a minimum width. A dot followed by a number sets precision for floating-point output.

#include <stdio.h>

int main(void)
{
    int count = 42;
    double price = 7.5;

    printf("Count: %5d\n", count);
    printf("Price: %.2f\n", price);
    printf("Column: |%8.2f|\n", price);

    return 0;
}

Output:

Count:    42
Price: 7.50
Column: |    7.50|

The %5d output uses at least five character positions for the integer. The %.2f output shows exactly two digits after the decimal point. The %8.2f output combines both ideas: at least eight positions wide, with two digits after the decimal point.

Specifiers With scanf

scanf also uses format specifiers, but it stores input in variables. For ordinary variables, pass the variable’s address with &.

#include <stdio.h>

int main(void)
{
    int quantity;
    double price;

    printf("Enter quantity and price: ");
    scanf("%d %lf", &quantity, &price);

    printf("Total: %.2f\n", quantity * price);
    return 0;
}

Output:

Enter quantity and price: Total: 29.97

This sample output assumes the user typed 3 9.99 and pressed Enter. Notice that scanf uses %lf to read a double, while printf commonly uses %f to print one.

Common Beginner Mistakes

  • Match each specifier to the correct value type.
  • Provide one value for each printf specifier.
  • Provide one variable address for each scanf specifier.
  • Use %d for int, not %f.
  • Use %lf with scanf when reading a double.

The key idea is that format specifiers describe the values moving into or out of your program, so they must match the data types you use.