fprintf, printf, sprintf, snprintf, vfprintf, vprintf, vsprintf, vsnprintf – print formatted output

#include <u.h>
#include <stdio.h>

int fprintf(FILE *f, char *format, ...)

int printf(char *format, ...)

int sprintf(char *s, char *format, ...)

int snprintf(char *s, int n, char *format, ...)

int vfprintf(FILE *f, char *format, va_list args)

int vprintf(char *format, va_list args)

int vsprintf(char *s, char *format, va_list args)

int vsnprintf(char *s, int n, char *format, va_list args)

Fprintf places output on the named output stream f (see fopen(2)). Printf places output on the standard output stream stdout. 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. Snprintf is like sprintf but writes at most n bytes (including the null character) into s. Vfprintf, vprintf, vsnprintf, and vsprintf are the same, except the args argument is the argument list of the calling function, and the effect is as if the calling function's argument list from that point on is passed to the printf routines.

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

These functions convert, format, and print their trailing arguments under control of a format string. The format 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 arguments. The results are undefined if there are arguments of the wrong type or too few arguments for the format. If the format is exhausted while arguments remain, the excess are ignored.

Each conversion specification is introduced by the character %. After the %, 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 with spaces on the left (or right, if the left adjustment, described later, has been given) to the field width.

An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and 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 conversions, or the maximum number of characters to be written from a string in s conversion. The precision takes the form of a period (.) followed by an optional decimal integer; if the integer is omitted, it is treated as zero.

An optional h specifying that a following d, i, o, u, x or X conversion specifier applies to a short int or unsigned short argument (the argument will have been promoted according to the integral promotions, and its value shall be converted to short or unsigned short before printing); an optional h specifying that a following n conversion specifier applies to a pointer to a short argument; an optional l (ell) specifying that a following d, i, o, u, x, or X conversion character applies to a long or unsigned long argument; an optional l specifying that a following n conversion specifier applies to a pointer to a long int argument; or an optional L specifying that a following e, E, f, g, or G conversion specifier applies to a long double argument. If an h, l, or 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 int arg supplies the field width or precision. The arguments specifying field width or precision, or both, shall appear (in that order) before the argument (if any) to be converted. A negative field width argument is taken as a – flag followed by a positive field width. A negative precision is taken as if it were missing.

The flag characters and their meanings are:
–       The result of the conversion is left–justified within the field.
+       The result of a signed conversion always begins with a sign (+ or –).
blank    If the first character of a signed conversion is not a sign, or a signed conversion results in no characters, a blank is prefixed to the result. This implies that if the blank and + flags both appear, the blank flag is ignored.
#       The result is to be converted to an ``alternate form.'' 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 non–zero result has 0x or 0X prefixed to it. For e, E, f, g, and G conversions, the result always contains 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 are not be removed from the result as they normally are. For other conversions, the behavior is undefined.
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 the field width; no space padding is performed. If the 0 and – flags both appear, the 0 flag will be ignored. For d, i, o, u, x, and X conversions, if a precision is specified, the 0 flag will
be ignored. For other conversions, the behavior is undefined.

The conversion characters and their meanings are:

The integer arg is converted to signed decimal (d or i), unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal notation (x or X); 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 is expanded with leading zeros. The default precision is 1. The result of converting a zero value with a precision of zero is no characters.
f       The double argument 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, it is taken as 6; if the precision is explicitly 0, no decimal point appears.
e,E     The double argument is converted in the style [–]d.ddddd, 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, it is taken as 6; if the precision is zero, no decimal point appears. The E format code produces a number
with E instead of e introducing the exponent. The exponent always contains at least two digits.
g,G     The double argument is printed in style f or e (or in style E in the case of a G conversion specifier), with the precision specifying the number of significant digits. If an explicit precision is zero, it is taken as 1. The style used depends on the value converted: style e is used only if the exponent resulting
from the conversion is less than –4 or greater than or equal to the precision. Trailing zeros are removed from the fractional portion of the result; a decimal point appears only if it is followed by a digit.
c       The int argument is converted to an unsigned char, and the resulting character is written.
s       The argument 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, it is taken to be infinite, so all characters up to the first null
character are printed. A zero value for the argument yields undefined results.
P       The void* argument is printed in an implementation–defined way (for Plan 9: the address as hexadecimal number).
n       The argument shall be a pointer to an integer into which is written the number of characters written to the output stream so far by this call to fprintf. No argument is converted.
%       Print a %; no argument is converted.

If a conversion specification is invalid, the behavior is undefined.

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 cast to be a pointer to void using %P conversion), the behavior is undefined.

In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.


fopen(2), fscanf(2), print(2)

There is no way to print a wide character (rune); use print(2) or bio(2).
Copyright © 2024 Alcatel-Lucent. All rights reserved.