Format specifiers in C are the % codes inside the format string of printf and scanf that say what type each argument is and how to show or read it: %d for int, %f for double, %c for a character, %s for a string. Every specifier must match the type of its argument, because a mismatch is undefined behavior.
#include <stdio.h>
int main(void) {
int count = 42;
double price = 3.14159;
char grade = 'A';
const char *name = "Ada";
printf("%s got %c, count=%d, price=%.2f\n", name, grade, count, price);
return 0;
}
Example output: Ada got A, count=42, price=3.14
Common format specifiers in C
These are the conversions you will use daily with printf. The table follows the cppreference reference for the C standard library.
| Specifier | Argument type | Prints |
|---|---|---|
%d, %i | int | Signed decimal integer |
%u | unsigned int | Unsigned decimal integer |
%o | unsigned int | Unsigned octal |
%x, %X | unsigned int | Unsigned hexadecimal, lowercase or uppercase letters |
%f, %F | double | Decimal notation, [-]ddd.ddd |
%e, %E | double | Exponent notation, [-]d.ddde±dd |
%g, %G | double | Decimal or exponent notation, chosen by the value and precision |
%a, %A | double | Hexadecimal floating-point |
%c | int | One character |
%s | char * | A null-terminated string |
%p | void * | A pointer address (format is implementation-defined) |
%n | int * | Prints nothing; stores the count of characters written so far |
%% | none | A literal percent sign |
C23 adds %b for unsigned binary output. Older compilers and libraries may not support it, so check your toolchain before relying on it.
Why %f works for both float and double
When you pass a float to a variadic function such as printf, C promotes it to double first. That is why %f takes a double and also works for a float variable. The same promotion explains why %c expects an int: cppreference notes that passing a char is safe because of the integer promotion that takes place when a variadic function is called.
Input is different. scanf stores through a pointer, so it needs the exact type: %f for a float * and %lf for a double *. The scanf in C guide covers return values, buffer overflow with %s and the leftover newline problem. For how a double stores digits and how many it can print accurately, see Double in C: Data Type, Precision and Examples.
Length modifiers: long, short, size_t and more
A length modifier goes between % and the conversion letter and changes the argument type. These are the ones from the C standard, as listed by cppreference and the Linux man page:
| Modifier | Applies to | Argument type | Example |
|---|---|---|---|
hh | integers | signed char / unsigned char | %hhu |
h | integers | short / unsigned short | %hd |
l | integers | long / unsigned long | %ld, %lu |
ll | integers | long long / unsigned long long | %lld, %llu |
j | integers | intmax_t / uintmax_t | %jd |
z | integers | size_t (or its signed counterpart) | %zu, %zd |
t | integers | ptrdiff_t | %td |
L | floating-point | long double | %Lf |
The most common mistake here is printing a size_t, such as the result of sizeof or strlen, with %d or %lu. Use %zu. On a 64-bit Windows build, long is 32 bits but size_t is 64 bits, so %lu can be wrong even when it works on Linux. %zu is correct on every platform whose library implements C99.
size_t length = sizeof(long long);
printf("long long is %zu bytes\n", length);
long long big = 9000000000LL;
printf("big = %lld\n", big);
The l modifier does nothing for floating-point conversions in printf, so %lf is accepted and prints a double exactly like %f. With %c and %s, l switches to wide characters (wint_t and wchar_t *).
Field width, precision and flags
The full shape of a conversion is: %, optional flags, optional width, optional .precision, optional length modifier, conversion letter.
Width
The width is the minimum number of characters. Shorter output is padded with spaces on the left. A width never truncates: the Linux man page states that a field width never causes truncation. Put * in place of the number and printf takes the width from an extra int argument placed before the value. A negative value there acts like the - flag.
Precision
The precision starts with a dot, and what it means depends on the conversion:
- Integers: the minimum number of digits, padded with leading zeros. The default is 1, and a precision of 0 with the value 0 prints no digits at all.
%fand%e: the digits after the decimal point. The default is 6.%g: the maximum number of significant digits.%s: the maximum number of characters written. With a precision, the array does not need a terminating null byte.
Flags
| Flag | Effect |
|---|---|
- | Left-justify inside the width (right-justified by default) |
+ | Always print a sign on signed and floating-point values |
| space | Put a space before non-negative signed values; ignored if + is present |
0 | Pad with zeros instead of spaces; ignored with -, and for integers when a precision is given |
# | Alternate form: 0x prefix for nonzero %x, always a decimal point for floats |
Here is each one in use. The cppreference page lists these same results in its example:
printf("[%*.*f]\n", 10, 2, 3.14159); // [ 3.14]
printf("[%-8s][%.3s]\n", "abc", "abcdef"); // [abc ][abc]
printf("%#x %+d %05d\n", 255, 42, 42); // 0xff +42 00042
Format specifiers for scanf
scanf reuses most of the same letters, but the arguments are pointers and the rules differ:
| Goal | printf | scanf |
|---|---|---|
| int | %d | %d with &value |
| float | %f | %f with &value |
| double | %f (or %lf) | %lf with &value |
| char | %c | %c with &value |
| string | %s | %9s with the array name, width one less than the array size |
The two pitfalls that matter most are the missing l for double and the unbounded %s. Both are explained with fixes in the scanf guide linked above. If you are unsure why scanf needs &, read Pointers in C Explained with Examples.
Mistakes that cause wrong output or crashes
- Type mismatch.
printf("%d", 3.5)is undefined behavior. cppreference says that when an argument after default promotions is not the type the specification expects, the behavior is undefined. Modern compilers can warn: with GCC or Clang, compile with-Wall -Wformat. - Too few arguments.
printf("%d %d", x)reads a value that was never passed. Extra arguments, by contrast, are evaluated and ignored. - Passing user input as the format string.
printf(input)lets the user supply specifiers. The Linux man page warns in its BUGS section that an untrusted string containing%ncan write to memory. Writeprintf("%s", input)orfputs(input, stdout). - Printing a pointer with
%dor%x. Use%pand cast tovoid *:printf("%p", (void *)ptr). The output format is implementation-defined. - Forgetting
%%. A lone%followed by a space or a letter starts a conversion. Write%%to print the percent sign.
What printf returns
On success, printf returns the number of characters transmitted to the output. On an output or encoding error it returns a negative value. snprintf returns the number of characters that would have been written had the buffer been large enough, not counting the terminating null, so comparing that number with the buffer size tells you whether the output was truncated.
char buffer[8];
int needed = snprintf(buffer, sizeof buffer, "%d-%d", 12345, 6789);
if (needed >= (int)sizeof buffer) {
puts("Output was truncated.");
}
Here the text 12345-6789 is 10 characters, so needed is 10 and the 8-byte buffer holds only the first 7 characters plus the null.
Frequently asked questions
- What is a format specifier in C?
- A format specifier is a placeholder that starts with % inside the format string of printf, scanf and related functions. It names the type of the matching argument, for example %d for int or %s for a string, and can add a width, precision, flags and a length modifier.
- What is the format specifier for size_t in C?
- Use %zu. The z length modifier makes an integer conversion take a size_t, and u prints it as an unsigned decimal. For the signed counterpart, use %zd.
- How do I print a % sign with printf?
- Write %% in the format string. cppreference lists it as a conversion that writes a single percent character and takes no argument.
- What happens if the specifier does not match the argument type?
- The behavior is undefined. cppreference states that if an argument, after default argument promotions, is not the type the conversion expects, the behavior is undefined. The program may print garbage, crash, or appear to work on one machine and fail on another.
- Is %i the same as %d?
- In printf, yes: both print a signed decimal integer. In scanf they differ: %d reads decimal only, while %i also accepts octal (leading 0) and hexadecimal (leading 0x).