Home
Main website
Display Sidebar
Hide Ads
Recent Changes
View Source:
vprintf(3)
Edit
PageHistory
Diff
Info
LikePages
PRINTF !!!PRINTF NAME SYNOPSIS DESCRIPTION EXAMPLES SEE ALSO CONFORMING TO HISTORY BUGS ---- !!NAME printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - formatted output conversion !!SYNOPSIS __#include __ __int printf(const char *__''format''__, ...); int fprintf(FILE *__''stream''__, const char *__''format''__, ...); int sprintf(char *__''str''__, const char *__''format''__, ...); int snprintf(char *__''str''__, size_t__ ''size''__, const char *__''format''__, ...); int asprintf(char **__''strp''__, const char *__''format''__, ...); int dprintf(int__ ''d''__, const char *__''format''__, ...);__ __#include __ __int vprintf(const char *__''format''__, va_list__ ''ap''__); int vfprintf(FILE *__''stream''__, const char *__''format''__, va_list__ ''ap''__); int vsprintf(char *__''str''__, const char *__''format''__, va_list__ ''ap''__); int vsnprintf(char *__''str''__, size_t__ ''size''__, const char *__''format''__, va_list__ ''ap''__); int vasprintf(char **__''strp''__, const char *__''format''__, va_list__ ''ap''__); int vdprintf(int__ ''d''__, const char *__''format''__, va_list__ ''ap''__);__ !!DESCRIPTION The functions in the __printf__ family produce output according to a ''format'' as described below. The functions __printf__ and __vprintf__ write output to ''stdout'', the standard output stream; __fprintf__ and __vfprintf__ write output to the given output ''stream''; __sprintf__, __snprintf__, __vsprintf__ and __vsnprintf__ write to the character string ''str''. The functions __vprintf__, __vfprintf__, __vsprintf__, __vsnprintf__ are equivalent to the functions __printf__, __fprintf__, __sprintf__, __snprintf__, respectively, except that they are called with a va_list instead of a variable number of arguments. These functions do not call the ''va_end'' macro. Consequently, the value of ''ap'' is undefined after the call. The application should call ''va_end(ap)'' itself afterwards. These eight functions write the output under the control of a ''format'' string that specifies how subsequent arguments (or arguments accessed via the variable-length argument facilities of stdarg(3)) are converted for output. __Return value__ These functions return the number of characters printed (not including the trailing `0' used to end output to strings). __snprintf__ and __vsnprintf__ do not write more than ''size'' bytes (including the trailing '0'), and return -1 if the output was truncated due to this limit. (Thus until glibc 2.0.6. Since glibc 2.1 these functions follow the C99 standard and return the number of characters (excluding the trailing '0') which would have been written to the final string if enough space had been available.) __Format of the format string__ The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: ordinary characters (not __%__), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the character __%__, and ends with a ''conversion specifier''. In between there may be (in this order) zero or more ''flags'', an optional minimum ''field width'', an optional ''precision'' and an optional ''length modifier''. The arguments must correspond properly (after type promotion) with the conversion specifier. By default, the arguments are used in the order given, where each `*' and each conversion specifier asks for the next argument (and it is an error if insufficiently many arguments are given). One can also specify explicitly which argument is taken, at each place where an argument is required, by writing `%m$' instead of `%' and `*m$' instead of `*', where the decimal integer m denotes the position in the argument list of the desired argument, indexed starting from 1. Thus, printf( and printf( are equivalent. The second style allows repeated references to the same argument. The C99 standard does not include the style using `$', which comes from the Single Unix Specification. If the style using `$' is used, it must be used throughout for all conversions taking an argument and all width and precision arguments, but it may be mixed with `%%' formats which do not consume an argument. There may be no gaps in the numbers of arguments specified using `$'; for example, if arguments 1 and 3 are specified, argument 2 must also be specified somewhere in the format string. For some numeric conversions a radix character (`decimal point') or thousands' grouping character is used. The actual character used depends on the LC_NUMERIC part of the locale. The POSIX locale uses `.' as radix character, and does not have a grouping character. Thus, printf( results in `1234567.89' in the POSIX locale, in `1234567,89' in the nl_NL locale, and in `1.234.567,89' in the da_DK locale. __The flag characters__ The character % is followed by zero or more of the following flags: __#__ The value should be converted to an ``alternate form''. For __o__ conversions, the first character of the output string is made zero (by prefixing a 0 if it was not zero already). For __x__ and __X__ conversions, a non-zero result has the string `0x' (or `0X' for __X__ conversions) prepended to it. For __a__, __A__, __e__, __E__, __f__, __F__, __g__, and __G__ conversions, the result will always contain a decimal point, even if no digits follow it (normally, a decimal point appears in the results of those conversions only if a digit follows). For __g__ and __G__ conversions, trailing zeros are not removed from the result as they would otherwise be. For other conversions, the result is undefined. __0__ The value should be zero padded. For __d__, __i__, __o__, __u__, __x__, __X__, __a__, __A__, __e__, __E__, __f__, __F__, __g__, and __G__ conversions, the converted value is padded on the left with zeros rather than blanks. If the __0__ and __-__ flags both appear, the __0__ flag is ignored. If a precision is given with a numeric conversion (__d__, __i__, __o__, __u__, __x__, and __X__), the __0__ flag is ignored. For other conversions, the behavior is undefined. __-__ The converted value is to be left adjusted on the field boundary. (The default is right justification.) Except for __n__ conversions, the converted value is padded on the right with blanks, rather than on the left with blanks or zeros. A __-__ overrides a __0__ if both are given. __' '__ (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. __+__ A sign (+ or -) always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers. A __+__ overrides a space if both are used. The five flag characters above are defined in the C standard. The SUSv2 specifies one further flag character. __'__ For decimal conversion (__i__, __d__, __u__, __f__, __F__, __g__, __G__) the output is to be grouped with thousands' grouping characters if the locale information indicates any. Note that many versions of __gcc__ cannot parse this option and will issue a warning. SUSv2 does not include %'F. glibc 2.2 adds one further flag character. __I__ For decimal integer conversion (__i__, __d__, __u__) the output uses the locale's alternative output digits, if any (for example, Arabic digits). However, it does not include any locale definitions with such __outdigits__ defined. __The field width__ An optional decimal digit string (with nonzero first digit) 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 flag has been given). Instead of a decimal digit string one may write `*' or `*m$' (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type ''int''. A negative field width is taken as a `-' flag followed by a positive field width. 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 expanded to contain the conversion result. __The precision__ An optional precision, in the form of a period (`.') followed by an optional decimal digit string. Instead of a decimal digit string one may write `*' or `*m$' (for some decimal integer m) to specify that the precision is given in the next argument, or in the m-th argument, respectively, which must be of type ''int''. If the precision is given as just `.', or the precision is negative, the precision is taken to be zero. This gives the minimum number of digits to appear for __d__, __i__, __o__, __u__, __x__, and __X__ conversions, the number of digits to appear after the radix character for __a__, __A__, __e__, __E__, __f__, and __F__ conversions, the maximum number of significant digits for __g__ and __G__ conversions, or the maximum number of characters to be printed from a string for __s__ and __S__ conversions. __The length modifier__ Here, `integer conversion' stands for __d__, __i__, __o__, __u__, __x__, or __X__ conversion. __hh__ A following integer conversion corresponds to a ''signed char'' or ''unsigned char'' argument, or a following __n__ conversion corresponds to a pointer to a ''signed char'' argument. __h__ A following integer conversion corresponds to a ''short int'' or ''unsigned short int'' argument, or a following __n__ conversion corresponds to a pointer to a ''short int'' argument. __l__ (ell) A following integer conversion corresponds to a ''long int'' or ''unsigned long int'' argument, or a following __n__ conversion corresponds to a pointer to a ''long int'' argument, or a following __c__ conversion corresponds to a ''wint_t'' argument, or a following __s__ conversion corresponds to a pointer to ''wchar_t'' argument. __ll__ (ell-ell). A following integer conversion corresponds to a ''long long int'' or ''unsigned long long int'' argument, or a following __n__ conversion corresponds to a pointer to a ''long long int'' argument. __L__ A following __a__, __A__, __e__, __E__, __f__, __F__, __g__, or __G__ conversion corresponds to a ''long double'' argument. (C99 allows %LF, but SUSv2 does not.) __q__ (`quad'. BSD 4.4 and Linux libc5 only. Don't use.) This is a synonym for __ll__. __j__ A following integer conversion corresponds to an ''intmax_t'' or ''uintmax_t'' argument. __z__ A following integer conversion corresponds to a ''size_t'' or ''ssize_t'' argument. (Linux libc5 has __Z__ with this meaning. Don't use it.) __t__ A following integer conversion corresponds to a ''ptrdiff_t'' argument. The SUSv2 only knows about the length modifiers __h__ (in __hd__, __hi__, __ho__, __hx__, __hX__, __hn__) and __l__ (in __ld__, __li__, __lo__, __lx__, __lX__, __ln__, __lc__, __ls__) and __L__ (in __Le__, __LE__, __Lf__, __Lg__, __LG__). __The conversion specifier__ A character that specifies the type of conversion to be applied. The conversion specifiers and their meanings are: __d__,__i__ The ''int'' argument is converted to signed decimal notation. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. The default precision is 1. When 0 is printed with an explicit precision 0, the output is empty. __o__,__u__,__x__,__X__ The ''unsigned int'' argument is converted to unsigned octal (__o__), unsigned decimal (__u__), or unsigned hexadecimal (__x__ and __X__) notation. The letters __abcdef__ are used for __x__ conversions; the letters __ABCDEF__ are used for __X__ conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. The default precision is 1. When 0 is printed with an explicit precision 0, the output is empty. __e__,__E__ The ''double'' argument is rounded and converted in the style [[-]d__.__ddd__e__ __E__ conversion uses the letter __E__ (rather than __e__) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00. __f__,__F__ The ''double'' argument is rounded and converted to decimal notation in the style [[-]ddd__.__ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it. (The SUSv2 does not know about __F__ and says that character string representations for infinity and NaN may be made available. The C99 standard specifies `[[-]inf' or `[[-]infinity' for infinity, and a string starting with `nan' for NaN, in the case of __f__ conversion, and `[[-]INF' or `[[-]INFINITY' or `NAN*' in the case of __F__ conversion.) __g__,__G__ The ''double'' argument is converted in style __f__ or __e__ (or __F__ or __E__ for __G__ conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style __e__ is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit. __a__,__A__ (C99; not in SUSv2) For __a__ conversion, the ''double'' argument is converted to hexadecimal notation (using the letters abcdef) in the style [[-]__0x__h__.__hhhh__p____A__ conversion the prefix __0X__, the letters ABCDEF, and the exponent separator __P__ is used. There is one hexadecimal digit before the decimal point, and the number of digits after it is equal to the precision. The default precision suffices for an exact representation of the value if an exact representation in base 2 exists and otherwise is sufficiently large to distinguish values of type ''double''. The digit before the decimal point is unspecified for non-normalized numbers, and nonzero but otherwise unspecified for normalized numbers. __c__ If no __l__ modifier is present, the ''int'' argument is converted to an ''unsigned char'', and the resulting character is written. If an __l__ modifier is present, the ''wint_t'' (wide character) argument is converted to a multibyte sequence by a call to the __wcrtomb__ function, with a conversion state starting in the initial state, and the resulting multibyte string is written. __s__ If no __l__ modifier is present: The ''const char *'' argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (but not including) a terminating __NUL__ character; if a precision is specified, no more than the number specified are written. If a precision is given, no null character need be present; if the precision is not specified, or is greater than the size of the array, the array must contain a terminating __NUL__ character. If an __l__ modifier is present: The ''const wchar_t *'' argument is expected to be a pointer to an array of wide characters. Wide characters from the array are converted to multibyte characters (each by a call to the __wcrtomb__ function, with a conversion state starting in the initial state before the first wide character), up to and including a terminating null wide character. The resulting multibyte characters are written up to (but not including) the terminating null byte. If a precision is specified, no more bytes than the number specified are written, but no partial multibyte characters are written. Note that the precision determines the number of ''bytes'' written, not the number of ''wide characters'' or ''screen positions''. The array must contain a terminating null wide character, unless a precision is given and it is so small that the number of bytes written exceeds it before the end of the array is reached. __C__ (Not in C99, but in SUSv2.) Synonym for __lc__. Don't use. __S__ (Not in C99, but in SUSv2.) Synonym for __ls__. Don't use. __p__ The ''void *'' pointer argument is printed in hexadecimal (as if by __%#x__ or __%#lx__). __n__ The number of characters written so far is stored into the integer indicated by the ''int *'' (or variant) pointer argument. No argument is converted. __%__ A `%' is written. No argument is converted. The complete conversion specification is `%%'. !!EXAMPLES To print pi to five decimal places: #include To print a date and time in the form `Sunday, July 3, 10:02', where ''weekday'' and ''month'' are pointers to strings: #include Many countries use the day-month-year order. Hence, an internationalized version must be able to print the arguments in an order specified by the format: #include where ''format'' depends on locale, and may permute the arguments. With the value one might obtain `Sonntag, 3. Juli, 10:02'. To allocate a sufficiently large string and print into it (code correct for both glibc 2.0 and glibc 2.1): #include !!SEE ALSO printf(1), wcrtomb(3), wprintf(3), scanf(3), locale(5) !!CONFORMING TO The __fprintf__, __printf__, __sprintf__, __vprintf__, __vfprintf__, and __vsprintf__ functions conform to ANSI X3.159-1989 (``ANSI C'') and ISO/IEC 9899:1999 (``ISO C99''). The __snprintf__ and __vsnprintf__ functions conform to ISO/IEC 9899:1999. Concerning the return value of __snprintf__, the SUSv2 and the C99 standard contradict each other: when __snprintf__ is called with ''size''=0 then SUSv2 stipulates an unspecified return value less than 1, while C99 allows ''str'' to be NULL in this case, and gives the return value (as always) as the number of characters that would have been written in case the output string has been large enough. Linux libc5 knows about the five C standard flags and the ' flag, locale, %m$ and *m$. It knows about the length modifiers h,l,L,Z,q, but accepts L and q both for long doubles and for long long integers (this is a bug). It no longer recognizes FDOU, but adds a new conversion character __m__, which outputs ''strerror(errno)''. glibc 2.0 adds conversion characters C and S. glibc 2.1 adds length modifiers hh,j,t,z and conversion characters a,A. glibc 2.2 adds the conversion character F with C99 semantics, and the flag character I. !!HISTORY Unix V7 defines the three routines __printf__, __fprintf__, __sprintf__, and has the flag -, the width or precision *, the length modifier l, and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx. This is still true for BSD 2.9.1, but BSD 2.10 has the flags #, + and __vprintf__, __vfprintf__, __vsprintf__, and warns not to use D,O,U,X. BSD 4.3 Reno has the flag 0, the length modifiers h and L, and the conversions n, p, E, G, X (with current meaning) and deprecates D,O,U. BSD 4.4 introduces the functions __snprintf__ and __vsnprintf__, and the length modifier q. FreeBSD also has functions ''asprintf'' and ''vasprintf'', that allocate a buffer large enough for __sprintf__. !!BUGS Because __sprintf__ and __vsprintf__ assume an arbitrarily long string, callers must be careful not to overflow the actual space; this is often impossible to assure. Note that the length of the strings produced is locale-dependent and difficult to predict. Use __snprintf__ and __vsnprintf__ instead (or __asprintf__ and __vasprintf__). Code such as __printf(__''foo''__);__ often indicates a bug, since ''foo'' may contain a % character. If ''foo'' comes from untrusted user input, it may contain %n, causing the __printf__ call to write to memory and creating a security hole. ----
One page links to
vprintf(3)
:
Man3v
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.