Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <stdio.h>

int printf(const char *restrict format, ...);

Try it

C Common conversions

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.

C Return value, dynamic width and size_t

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

C
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. %d is for int, %ld for long, %lld for long long, %zu for size_t, %f for double (a float is promoted to double automatically). %u or %x expect unsigned 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 %s or %n and read or write memory. Write printf("%s", user_text) or use puts().
  • Output is buffered. stdout is 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. Call fflush(stdout) when ordering with stderr matters.
  • Characters are promoted. char and short arguments become int, so %c and %d work for them. %hhd and %hd convert back to the narrow type.
  • Floating point. %f prints six decimals by default and rounds. %g chooses between fixed and scientific and strips trailing zeros. Infinity and NaN print as inf and nan (uppercase with %F, %G, %E).
  • %p prints a pointer and requires a void * argument (cast with (void *)p). The text is implementation-defined and changes between runs, so avoid it in reproducible output.
  • %n writes the count of characters so far into an int *. 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, and fprintf() to choose the stream. printf(...) is exactly fprintf(stdout, ...).
  • Complexity: linear in the output length. Many small printf calls are slower than one larger call or puts.