2.12.4.1 ..printf Functions

Declarations:

int fprintf(FILE *stream, const char *format, ...);
int printf(const char *
format, ...);
int sprintf(char *
str, const char *format, ...);
int vfprintf(FILE *
stream, const char *format, va_list arg);
int vprintf(const char *
format, va_list arg);
int vsprintf(char *
str, const char *format, va_list arg);

The ..printf functions provide a means to output formatted information to a stream.

fprintfsends formatted output to a stream
printfsends formatted output to stdout
sprintfsends formatted output to a string
vfprintfsends formatted output to a stream using an argument list
vprintfsends formatted output to stdout using an argument list
vsprintfsends formatted output to a string using an argument list

These functions take the format string specified by the format argument and apply each following argument to the format specifiers in the string in a left to right fashion. Each character in the format string is copied to the stream except for conversion characters which specify a format specifier.

The string commands (sprintf and vsprintf) append a null character to the end of the string. This null character is not counted in the character count.

The argument list commands (vfprintfvprintf, and vsprintf) use an argument list which is prepared by va_start. These commands do not call va_end (the caller must call it).

A conversion specifier begins with the % character. After the % character come the following in this order:

[flags]Control the conversion (optional).
[width]Defines the number of characters to print (optional).
[.precision]Defines the amount of precision to print for a number type (optional).
[modifier]Overrides the size (type) of the argument (optional).
[type]The type of conversion to be applied (required).

Flags:

-Value is left justified (default is right justified). Overrides the 0 flag.
+Forces the sign (+ or -) to always be shown. Default is to just show the – sign. Overrides the space flag.
spaceCauses a positive value to display a space for the sign. Negative values still show the – sign.
#Alternate form:

Conversion CharacterResult
oPrecision is increased to make the first digit a zero.
X or xNonzero value will have 0x or 0X prefixed to it.
E, e, f, g, or GResult will always have a decimal point.
G or gTrailing zeros will not be removed.
0For d, i, o, u, x, X, e, E, f, g, and G leading zeros are used to pad the field width instead of spaces. This is useful only with a width specifier. Precision overrides this flag.

Width:
The width of the field is specified here with a decimal value. If the value is not large enough to fill the width, then the rest of the field is padded with spaces (unless the 0 flag is specified). If the value overflows the width of the field, then the field is expanded to fit the value. If a * is used in place of the width specifer, then the next argument (which must be an int type) specifies the width of the field. Note: when using the * with the width and/or precision specifier, the width argument comes first, then the precision argument, then the value to be converted.

Precision:
The precision begins with a dot (.) to distinguish itself from the width specifier. The precision can be given as a decimal value or as an asterisk (*). If a * is used, then the next argument (which is an int type) specifies the precision. Note: when using the * with the width and/or precision specifier, the width argument comes first, then the precision argument, then the value to be converted. Precision does not affect the c type.

[.precision]Result
(none)Default precision values:
1 for diouxX types. The minimum number of digits to appear.
6 for feE types. Specifies the number of digits after the decimal point.
For g or G types all significant digits are shown.
For s type all characters in string are print up to but not including the null character.
. or .0For diouxX types the default precis ion value is used unless the value is zero in which case no characters are printed.
For feE types no decimal point character or digits are printed.
For g or G types the precision is assumed to be 1.
.nFor diouxX types then at least n digits are printed (padding with zeros if necessary).
For feE types specifies the number of digits after the decimal point.
For g or G types specifies the number of significant digits to print.
For s type specifies the maximum number of characters to print.

Modifier:
A modifier changes the way a conversion specifier type is interpreted.

[modifier][type]Effect
hdiouxXValue is first converted to a short int or unsigned short i nt.
hnSpecifies that the pointer points to a short int.
ldiouxXValue is first converted to a long int or unsigned long int .
lnSpecifies that the pointer points to a long int.
LeEfgGValue is first converted to a long double.

Conversion specifier type:
The conversion specifier specifies what type the argument is to be treated as.

[type]Output
diType signed int.
oType unsigned int printed in octal.
uType unsigned int printed in decimal.
xType unsigned int printed in hexadecimal as dddd using a, b, c, d, e, f.
XType unsigned int printed in hexadecimal as dddd using A, B, C, D, E, F.
fType double printed as [-]ddd.ddd.
eEType double printed as [-]d.ddde�dd where there is one digit printed before the decimal (zero only if the value is zero). The exponent contains at least two digits. If type is E then the exponent is printed with a capital E.
gGType double printed as type e or E if the exponent is less than -4 or greater than or equal to the precision. Otherwise printed as type f. Trailing zeros are removed. Decimal point character appears only if there is a nonzero decimal digit.
cType char. Single character is printed.
sType pointer to array. String is printed according to precision (no precision prints entire string).
pPrints the value of a pointer (the memory location it holds).
nThe argument must be a pointer to an int. Stores the number of characters printed thus far in the int. No characters are printed.
%% sign is printed.

The number of characters printed are returned. If an error occurred, -1 is returned.