Name

printf

Synopsis

Writes formatted output to the standard output stream

#include <stdio.h>
intprintf( const char * restrict format, ... );

The printf() function converts various kinds of data into string representations for output, and substitutes them for placeholders in the string referenced by the mandatory pointer argument, format. The resulting output string is then written to the standard output stream. The return value of printf() is the number of characters printed, or EOF to indicate that an error occurred.

The placeholders in the string argument are called conversion specifications, because they also specify how each replacement data item is to be converted, according to a protocol described shortly.

The optional arguments, represented by the ellipsis in the function prototype, are the data items to be converted for insertion in the output string. The arguments are in the same order as the conversion specifications in the format string.

Conversion specification syntax

For a general overview of data output with printf(), see "Formatted Output" in Chapter 13. This section describes the syntax of conversion specifications in the printf() format string in detail. The conversion specifications have the following syntax:

%[flags][field width][.precision][length modifier]specifier

The flags consist of one or more of the characters +, ' ' (space), -, 0, or #. Their meanings are as follows:

+

Add a plus sign before positive numbers.

' '

Add a space before positive numbers (not applicable ...

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.