printf, fprintf, sprintf - print formatted output
#include <stdio.h>
printf() places output on the standard output stream stdout .
fprintf() places output on strm.
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 character string that contains three types of objects defined below:
plain characters that are simply copied to the output stream;
escape sequences that represent non-graphic characters;
conversion specifications.
The following escape sequences produce the associated action on display devices capable of the action: \a Alert. Ring the bell.
All forms of the printf() functions allow for the insertion of a language-dependent decimal-point character. The decimal-point character is defined by the program's locale (category LC_NUMERIC). In the C locale, or in a locale where the decimal-point character is not defined, the decimal-point character defaults to a period (.).
Each conversion specification is introduced by the character %. After the character %, the following appear in sequence:
An optional field, consisting of a decimal digit string followed by a $, specifying the next args to be converted. If this field is not provided, the args following the last args converted will be used.
Zero or more flags, which modify the meaning of the conversion specification.
An optional string of decimal digits to specify 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. If the format is %s or %ws (wide-character string), then the field width should be interpreted as the minimum columns of screen display. E.g. %10s means if the converted value has a screen width of 7 columns, then 3 spaces would be padded on the right.
An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, or X conversions (the field is padded with leading zeros), the number of digits to appear after the decimal-point character for the e, E, and f conversions, the maximum number of significant digits for the g and G conversions, or the maximum number of characters to be printed from a string in s or ws conversions. 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.
An optional h specifies that a following d, i, o, u, x, or X conversion specifier applies to a short int or unsigned short int argument (the argument will be promoted according to the integral promotions and its value converted to short int or unsigned short int before printing); an optional h specifies that a following n conversion specifier applies to a pointer to a short int argument. An optional l (ell) specifies that a following d, i, o, u, x, or X conversion specifier applies to a long int or unsigned long int argument; an optional l (ell) specifies that a following n conversion specifier applies to a pointer to long int argument. An optional L specifies that a following e, E, f, g, or G conversion specifier applies to a long double argument. If an h, l, or L appears before any other conversion specifier, the behavior is undefined.
A conversion character (see below) that indicates the type of conversion to be applied.
A field width or precision may be indicated by an asterisk (*) instead of a digit string. In this case, an integer args supplies the field width or precision. The args 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 args (if any) to be converted. If the precision argument is negative, it will be changed to zero. A negative field width argument is taken as a - flag, followed by a positive field width.
In format strings containing the *digits$ form of a conversion specification, a field width or precision may also be indicated by the sequence *digits$, giving the position in the argument list of an integer args containing the field width or precision.
When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, be specified in the format string.
The flag characters and their meanings are:
If the character after the % or %digits sequence is not a valid conversion character, the results of the conversion are undefined.
If a floating-point value is the internal representation for infinity, the output is [(+-]Infinity, where Infinity is either Infinity or Inf , depending on the desired output string length. Printing of the sign follows the rules described above.
If a floating-point value is the internal representation for ``not-a-number,'' the output is [+-]NaN. Printing of the sign follows the rules described above.
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 the putc() routine had been called.
printf(), , fprintf(), , and sprintf() return the number of characters transmitted, or return a negative value if an error was encountered.
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 *p to 5 decimal places:
printf("pi eq %.5f", 4 * atan(1.0));
To print a list of names in columns which are 20 characters wide:
printf("%20s%20s%20s", lastname, firstname, middlename );
SEE ALSO
lseek(), write(), putc(), scanf(),