TExcelTAD
Home › C › C Format Specifiers

C Format Specifiers

printf does not know what type your values are — you tell it with format specifiers. A format specifier begins with % and sits inside the format string as a placeholder. Each placeholder is replaced, in order, by the next argument after the string.

c · try it yourself
#include <stdio.h>

int main(void) {
    int items = 3;
    double cost = 4.5;
    char unit = '
  

;

    printf("%d items cost %c%f\n", items, unit, cost);
    return 0;
}
3 items cost $4.500000

The common specifiers

Specifier Used for Example output
%d or %i int (signed whole number) -42
%u unsigned int 42
%ld, %lld long, long long 9000000000
%f float or double in decimal notation 3.140000
%e double in scientific notation 3.140000e+00
%g double, shortest of %f / %e 3.14
%c char (one character) A
%s string (text) hello
%x / %X unsigned int in hexadecimal ff / FF
%o unsigned int in octal 377
%zu size_t (the type of sizeof) 8
%p pointer (memory address) varies
%% a literal percent sign %

Warning: The specifier must match the argument's type. Printing a double with %d, or an int with %f, is undefined behaviour and usually prints nonsense. Compile with -Wall and GCC will warn you about mismatches.

Controlling decimals

By default %f prints six digits after the decimal point. Write .N between % and f to choose how many:

c · try it yourself
#include <stdio.h>

int main(void) {
    double pi = 3.14159265358979;
    printf("%f\n", pi);
    printf("%.2f\n", pi);
    printf("%.0f\n", pi);
    printf("%.4f\n", pi);
    printf("%g\n", pi);
    printf("%e\n", 123456.789);
    return 0;
}
3.141593
3.14
3
3.1416
3.14159
1.234568e+05

Values are rounded, not cut off: %.4f of 3.14159… prints 3.1416.

Width and alignment

A number right after % sets the minimum width. The value is padded with spaces on the left (right-aligned). A - flag pads on the right instead (left-aligned), and a 0 flag pads numbers with zeros. This is perfect for tables:

c · try it yourself
#include <stdio.h>

int main(void) {
    printf("[%5d]\n", 42);
    printf("[%-5d]\n", 42);
    printf("[%05d]\n", 42);
    printf("[%8.2f]\n", 3.14159);
    printf("\n");
    printf("%-8s %6s\n", "Item", "Price");
    printf("%-8s %6.2f\n", "Coffee", 2.5);
    printf("%-8s %6.2f\n", "Bagel", 1.75);
    printf("%-8s %6.2f\n", "Juice", 3.0);
    return 0;
}
[   42]
[42   ]
[00042]
[    3.14]

Item      Price
Coffee     2.50
Bagel      1.75
Juice      3.00

Other bases, characters and percent signs

c · try it yourself
#include <stdio.h>

int main(void) {
    int n = 255;
    printf("decimal %d, hex %x, HEX %X, octal %o\n", n, n, n, n);
    printf("with prefix: %#x %#o\n", n, n);
    printf("char code 72 is '%c'\n", 72);
    printf("Battery at %d%%\n", 80);
    printf("Name: %s\n", "Grace");
    printf("Plus sign: %+d %+d\n", 5, -5);
    return 0;
}
decimal 255, hex ff, HEX FF, octal 377
with prefix: 0xff 0377
char code 72 is 'H'
Battery at 80%
Name: Grace
Plus sign: +5 -5

Anatomy of a format specifier

The full shape is %[flags][width][.precision][length]type:

Part Examples Effect
flags -, 0, +, , # left-align, zero-pad, always show sign, space for sign, base prefix
width 5, 10 minimum number of characters
precision .2, .0 decimals for %f; max characters for %s
length l, ll, z, h size of the integer argument (long, long long, size_t, short)
type d, f, s, c, x … how to interpret and print the value

Tip: %.3s prints at most three characters of a string: printf("%.3s", "abcdef") shows abc.

Test yourself

Which specifier prints a double with exactly two decimals?

  1. %2f
  2. %.2f
  3. %d.2
  4. %f2

How do you print a literal % sign with printf?

  1. \%
  2. %%
  3. %p
  4. '%'

What does printf("[%-4d]", 7); print?

  1. [ 7]
  2. [7 ]
  3. [0007]
  4. [7]