C Reference ยท stdio.h
C printf() function
Reference for C printf(): print formatted text to standard output, with conversions, flags, width and precision, the return value and common mistakes.
printf() writes formatted text to standard output. You give it a format string that mixes ordinary characters with conversion specifications such as %d or %s, followed by the values to insert. Each specification consumes the next argument and converts it to text.
It is a variadic function, so the compiler cannot always check that your specifiers match your arguments at the language level. A mismatch is undefined behavior. GCC and Clang check literal format strings and warn (-Wformat, part of -Wall), so always read those warnings.
#include <stdio.h> int printf(const char *restrict format, ...);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int printf(const char *restrict format, ...);
A conversion specification has the form %[flags][width][.precision][length]conversion.
| Name | Description |
|---|---|
format |
The format string. Ordinary characters are copied; % introduces a conversion; %% prints one percent sign. |
... |
One argument per conversion (plus one for each * used as width or precision, which come first). |
Common pieces (the full tables are on printf format specifiers):
| Part | Meaning |
|---|---|
| flags | - left-align, + always show sign, space, 0 pad with zeros, # alternate form |
| width | Minimum field width, a number or * (taken from an int argument) |
.precision |
Digits after the decimal point for floats, maximum characters for %s, minimum digits for integers |
| length | hh h l ll j z t L to say how wide the argument is (%zu for size_t, %lld for long long) |
| conversion | d i u o x X f e g a c s p % and so on |
Returns: the number of characters written, or a negative value if an output or encoding error occurred.
Errors / undefined behavior: too few arguments, a specifier that does not match the argument type (such as %d with a double), %s with a null pointer or a non-terminated array, or a bad conversion are all undefined behavior.
Usage notes
- Match type and specifier exactly.
%dis forint,%ldforlong,%lldforlong long,%zuforsize_t,%ffordouble(afloatis promoted todoubleautomatically).%uor%xexpectunsigned int. For fixed-width types use the macros from<inttypes.h>(PRId64). - Never pass user input as the format.
printf(user_text)lets the user supply%sor%nand read or write memory. Writeprintf("%s", user_text)or useputs(). - Output is buffered.
stdoutis line-buffered on a terminal and fully buffered when redirected, so text without a newline may appear late, and unflushed output is lost if the program crashes. Callfflush(stdout)when ordering withstderrmatters. - Characters are promoted.
charandshortarguments becomeint, so%cand%dwork for them.%hhdand%hdconvert back to the narrow type. - Floating point.
%fprints six decimals by default and rounds.%gchooses between fixed and scientific and strips trailing zeros. Infinity and NaN print asinfandnan(uppercase with%F,%G,%E). %pprints a pointer and requires avoid *argument (cast with(void *)p). The text is implementation-defined and changes between runs, so avoid it in reproducible output.%nwrites the count of characters so far into anint *. It is rarely needed and a security risk; some libraries disable it.- Locale. The decimal point and thousands grouping (
%'d) depend on the current locale; programs start in the"C"locale. - Use
snprintf()to format into a buffer, andfprintf()to choose the stream.printf(...)is exactlyfprintf(stdout, ...). - Complexity: linear in the output length. Many small
printfcalls are slower than one larger call orputs.