O'Reilly logo

C in a Nutshell by Tony Crawford, Peter Prinz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required