Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

ecvt(3C)

putc(3S)

scanf(3S)

setlocale(3C)

PRINTF(3S)                           SysV                           PRINTF(3S)



NAME
     printf, fprintf, sprintf - print formatted output

SYNOPSIS
     #include <stdio.h>

     int printf(format [, arg ] ...  )
     const char *format;

     int fprintf(stream, format [, arg ] ...  )
     FILE *stream;
     const char *format;

     int sprintf(s, format [, arg ] ...  )
     char *s;
     const char *format;

DESCRIPTION
     printf places output on the standard output stream stdout.  fprintf
     places output on the named output stream.  sprintf places "output,"
     followed by the null character (\0), in consecutive bytes starting at *s;
     it is the user's responsibility to ensure that enough storage is
     available.  Each function returns the number of characters transmitted
     (not including the \0 in the case of sprintf), or a negative value if an
     output error was encountered.

     Each of these functions converts, formats, and prints its args under
     control of the format.  The format is a multibyte character string,
     beginning and ending in its initial shift state.  It contains two types
     of objects:  plain characters, which are simply copied to the output
     stream, and conversion specifications, each of which results in fetching
     of zero or more args.  The results are undefined if there are
     insufficient args for the format.  If the format is exhausted while args
     remain, the excess args are simply ignored.  Conversions can be applied
     to the nth argument in the argument list, rather than to the next unused
     argument. In this case, the conversion character % (see below) is
     replaced by the sequence %digit$ where digit is a decimal integer n in
     the range [1, {NL_ARGMAX}], giving the position of the argument in the
     argument list. This feature provides for the definition of format strings
     that select arguments in an order appropriate to specific languages.

     All forms of the printf functions allow for the insertion of a language-
     dependent radix character in the output string. The radix character is
     defined by langinfo data in the program's locale (category LC_NUMERIC).
     In the "C" locale, or in a locale where the radix character is not
     defined, the radix character defaults to ".".

     Each conversion specification is introduced by the character % or by the
     character sequence %digit$.

     These forms may not be mixed within a single format string. An argument
     may be referenced by a %digit$ specification more than once. After the %
     character or %digit$ sequence, the following appear in sequence:

          Zero or more flags, which modify the meaning of the conversion
          specification.

          An optional decimal digit string specifying a minimum field width.
          If the converted value has fewer characters than the field width, it
          will be padded on the left (or right, if the left-adjustment flag
          `-', described below, has been given) to the field width.  The
          padding is with blanks unless the field width digit string starts
          with a zero, in which case the padding is with zeros.

          A precision that gives the minimum number of digits to appear for
          the d, i, o, u, x, or X conversions, the number of digits to appear
          after the decimal point for the e, E, and f conversions, the maximum
          number of significant digits for the g and G conversion, or the
          maximum number of characters to be printed from a string in s
          conversion.  The precision takes the form of a period (.)  followed
          by a decimal digit string; a null digit string is treated as zero.
          Padding specified  by the precision overrides the padding specified
          by the field width.  If a precision appears with any other
          conversion specifier, the behavior is undefined.

          An optional h specifying that a following d, i, o, u, x, or X
          conversion specifier applies to a short integer or unsigned short
          integer arg, or specifying that a following n conversion specifier
          applies to a pointer to a short integer.  If an h appears with any
          other conversion specifier, the behavior is undefined.

          An optional l (ell) specifying that a following d, i, o, u, x, or X
          conversion character applies to a long integer or unsigned long
          integer arg.  If an l appears with any other conversion specifier,
          the behavior is undefined.

          An optional L specifying that a following e, E, f, g, or G
          conversion specifier applies to a long double arg.  If an L appears
          with any other conversion specifier, the behavior is undefined.

          A character that indicates the type of conversion to be applied.

     A field width or precision or both may be indicated by an asterisk (*)
     instead of a digit string.  In this case, an integer arg supplies the
     field width or precision.  The arg that is actually converted is not
     fetched until the conversion letter is seen, so the args specifying field
     width or precision must appear before the arg (if any) to be converted.
     A negative field width argument is taken as a `-' flag followed by a
     positive field width. If the precision argument is negative, it will be
     changed to zero.  In format strings containing the %digit$ form of a
     conversion specification, a field width or precision may be indicated by
     the sequence *digit$ where digit is a decimal integer in the range [1,
     {NL_ARGMAX}] giving the position of the argument list of an integer arg
     containing the field width or precision, for example:

          printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);

          The format can contain either numbered argument specifications
          (i.e., %digit$ and *digit$), or unnumbered argument specifications
          (i.e., % and *), but not both. The results of mixing numbered and
          unnumbered argument specifications in a format string are undefined.
          When numbered argument specifications are used, specifying the Nth
          argument requires that all the leading arguments, from the first to
          the (N-1)th are specified in the format string.

     The flag characters and their meanings are:

     -       The result of the conversion will be left-justified within the
             field.

     +       The result of a signed conversion will always begin with a sign
             (+ or -).

     blank   If the first character of a signed conversion is not a sign, a
             blank will be prefixed to the result.  This implies that if the
             blank and + flags both appear, the blank flag will be ignored.

     #       This flag specifies that the value is to be converted to an
             "alternate form."

             XPG3: For c, d, i, s, and u conversions, the flag has no effect.

             For o conversion, it increases the precision to force the first
             digit of the result to be a zero.  For x or X conversion, a
             nonzero result will have 0x or 0X prefixed to it.  For e, E, f,
             g, and G conversions, the result will always contain a decimal
             point, even if no digits follow the point (normally, a decimal
             point appears in the result of these conversions only if a digit
             follows it).  For g and G conversions, trailing zeros will not be
             removed from the result (which they normally are).

     0       For d, i, o, u, x, X, e, E, f, g and G conversions, leading zeros
             (following any indication of sign or base) are used to pad to the
             field width; no space padding is performed. If the 0 and - flags
             both appear, the 0 flag is ignored. For d,i,o,u,x, and X
             conversions, if a precision is specified, the 0 flag is ignored.
             For other conversions, the behavior is undefined.

     The conversion characters and their meanings are:

     d,i,o,u,x,X   The integer arg is converted to signed decimal (d or i),
                   unsigned octal, (o), decimal (u), or hexadecimal notation
                   (x or X), respectively; the letters abcdef are used for x
                   conversion and the letters ABCDEF for X conversion.  The
                   precision specifies the minimum number of digits to appear;
                   if the value being converted can be represented in fewer
                   digits, it will be expanded with leading zeros.  The
                   default precision is 1.  The result of converting a zero
                   value with a precision of zero is a null string.

     f             The float or double arg is converted to decimal notation in
                   the style "[-]ddd.ddd," where the number of digits after
                   the decimal point is equal to the precision specification.
                   If the precision is missing, six digits are output; if the
                   precision is explicitly 0 and the # flag is not specified,
                   no decimal point appears.  If a decimal point character
                   appears, at least one digit appears before it.  The value
                   is rounded to the appropriate number of digits.

     e,E           The float or double arg is converted in the style
                   "[-]d.ddde+dd," where there is one digit before the decimal
                   point and the number of digits after it is equal to the
                   precision; when the precision is missing, six digits are
                   produced; if the precision is zero and the # is not
                   specified, no decimal point appears.  The value is rounded
                   to the appropriate number of digits.  The E format code
                   will produce a number with E instead of e introducing the
                   exponent.  The exponent always contains at least two
                   digits.

     g,G           The float or double arg is printed in style f or e (or in
                   style E in the case of a G format code), with the precision
                   specifying the number of significant digits.  The style
                   used depends on the value converted:  style e will be used
                   only if the exponent resulting from the conversion is less
                   than -4 or greater than the precision.  Trailing zeros are
                   removed from the result; a decimal point appears only if it
                   is followed by a digit.

     c             The int arg is converted to an unsigned char and the
                   resulting character is printed.

     s             The arg is taken to be a string (character pointer) and
                   characters from the string are printed until a null
                   character (\0) is encountered or the number of characters
                   indicated by the precision specification is reached.  If
                   the precision is missing or is greater than the size of the
                   array, the array must contain a null character or the
                   behavior is undefined.

     p             The value of the pointer-to-void arg is written out as
                   0xXXXXXXXX, where XXXXXXXX is an eight-digit hexadecimal
                   number in which any alphabetic digits appear in lowercase.

     n             arg points to an integer into which is copied the number of
                   characters written to the output stream so far by this
                   call.  No argument is converted.

     %             Print a %; no argument is converted. The complete
                   conversion specification must be %%.

     If any argument is, or points to, a union or an aggregate (except for an
     array of character type using %s conversion, or a pointer using %p
     conversion), the behavior is undefined.  If, in the case of sprintf,
     copying takes place between objects that overlap, the behavior is
     undefined.

     In printing floating point types (float and double), if the exponent is
     0x7FF and the mantissa is not equal to zero, then the output is

          [-]NaN0xdddddddd

     where 0xdddddddd is the hexadecimal representation of the leftmost 32
     bits of the mantissa.  If the mantissa is zero, the output is

          [+]inf.  If the character after the % or %digit$ sequence is not a
     valid conversion character, the results of the conversion are undefined.

     In no case does a non-existent or small field width cause truncation of a
     field; if the result of a conversion is wider than the field width, the
     field is simply expanded to contain the conversion result.  Characters
     generated by printf and fprintf are printed as if putc(3S) had been
     called.  The st_ctime and st_mtime fields of the file will be marked for
     update between the successful execution of printf or fprintf and the next
     successful completion of a call to fflush(3S) or fclose(3S) on the same
     stream, or a call to exit(2) or abort(3C).

DIAGNOSTICS
     Each function returns the number of bytes transmitted (not including the
     (\0), in the case of sprintf, or a negative value if an output error was
     encountered.

ERRORS
     The printf or fprintf functions fail if either the stream is unbuffered,
     or the stream's buffer needed to be flushed and the function call caused
     an underlying write(2) or lseek(2) to be invoked.  In addition, if these
     functions fail, errno is set to one of the following values:

     [EAGAIN]       The O_NONBLOCK flag is set for the file descriptor
                    underlying stream and the process would be delayed in the
                    write operation.

     [EBADF]        The file descriptor underlying stream is not a valid file
                    descriptor open for writing.

     [EFBIG]        An attempt was made to write to a file that exceeds the
                    process' file size limit or the maximum file size.

     [EINTR]        The read operation was interrupted by a signal which was
                    caught, and no data was transferred.

     [EIO]          The implementation supports job control, the process is a
                    member of a background process group attempting to write
                    to its controlling terminal, TOSTOP is set, the process is
                    neither ignoring nor blocking SIGTTOU and the process
                    group of the process is orphaned.

     [ENOSPC]       There was no free space remaining on the device containing
                    the file.

     [EPIPE]        An attempt was made to write to a pipe or FIFO that is not
                    open for reading by any process.  A SIGPIPE signal will
                    also be sent to the process.


EXAMPLES
     To print a date and time in the form "Sunday, July 3, 10:02," where
     weekday and month are pointers to null terminated strings:

     printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min);

     To print pi to 5 decimal places:

     printf("pi = %.5f", 4 * atan(1.0));

SEE ALSO
     ecvt(3C), putc(3S), scanf(3S), setlocale(3C), <stdio.h>.

NOTE
     Parts of this discussion are adapted from ANS X3.159-1989.

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026