1*0a6a1f1dSLionel Sambuc.\" $NetBSD: printf.3,v 1.64 2014/09/29 14:58:33 christos Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1990, 1991, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by 72fe8fb19SBen Gras.\" Chris Torek and the American National Standards Committee X3, 82fe8fb19SBen Gras.\" on Information Processing Systems. 92fe8fb19SBen Gras.\" 102fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 112fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 122fe8fb19SBen Gras.\" are met: 132fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 142fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 152fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 162fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 172fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 182fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 192fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 202fe8fb19SBen Gras.\" without specific prior written permission. 212fe8fb19SBen Gras.\" 222fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 232fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 242fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 252fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 262fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 272fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 282fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 292fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 302fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 312fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 322fe8fb19SBen Gras.\" SUCH DAMAGE. 332fe8fb19SBen Gras.\" 342fe8fb19SBen Gras.\" @(#)printf.3 8.1 (Berkeley) 6/4/93 352fe8fb19SBen Gras.\" 36*0a6a1f1dSLionel Sambuc.Dd September 29, 2014 372fe8fb19SBen Gras.Dt PRINTF 3 382fe8fb19SBen Gras.Os 392fe8fb19SBen Gras.Sh NAME 402fe8fb19SBen Gras.Nm printf , 412fe8fb19SBen Gras.Nm fprintf , 422fe8fb19SBen Gras.Nm dprintf 432fe8fb19SBen Gras.Nm sprintf , 442fe8fb19SBen Gras.Nm snprintf , 4584d9c625SLionel Sambuc.Nm snprintf_ss , 462fe8fb19SBen Gras.Nm asprintf , 472fe8fb19SBen Gras.Nm vprintf , 482fe8fb19SBen Gras.Nm vfprintf , 492fe8fb19SBen Gras.Nm vsprintf , 502fe8fb19SBen Gras.Nm vdprintf , 512fe8fb19SBen Gras.Nm vsnprintf , 522fe8fb19SBen Gras.Nm vsnprintf_ss , 532fe8fb19SBen Gras.Nm vasprintf 542fe8fb19SBen Gras.Nd formatted output conversion 552fe8fb19SBen Gras.Sh LIBRARY 562fe8fb19SBen Gras.Lb libc 572fe8fb19SBen Gras.Sh SYNOPSIS 582fe8fb19SBen Gras.In stdio.h 592fe8fb19SBen Gras.Ft int 602fe8fb19SBen Gras.Fn printf "const char * restrict format" ... 612fe8fb19SBen Gras.Ft int 622fe8fb19SBen Gras.Fn fprintf "FILE * restrict stream" "const char * restrict format" ... 632fe8fb19SBen Gras.Ft int 642fe8fb19SBen Gras.Fn dprintf "int fd" "const char * restrict format" ... 652fe8fb19SBen Gras.Ft int 662fe8fb19SBen Gras.Fn sprintf "char * restrict str" "const char * restrict format" ... 672fe8fb19SBen Gras.Ft int 682fe8fb19SBen Gras.Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ... 692fe8fb19SBen Gras.Ft int 7084d9c625SLionel Sambuc.Fn snprintf_ss "char * restrict str" "size_t size" "const char * restrict format" ... 7184d9c625SLionel Sambuc.Ft int 722fe8fb19SBen Gras.Fn asprintf "char ** restrict ret" "const char * restrict format" ... 732fe8fb19SBen Gras.In stdarg.h 742fe8fb19SBen Gras.Ft int 752fe8fb19SBen Gras.Fn vprintf "const char * restrict format" "va_list ap" 762fe8fb19SBen Gras.Ft int 772fe8fb19SBen Gras.Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap" 782fe8fb19SBen Gras.Ft int 792fe8fb19SBen Gras.Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap" 802fe8fb19SBen Gras.Ft int 812fe8fb19SBen Gras.Fn vdprintf "int fd" "const char * restrict format" "va_list ap" 822fe8fb19SBen Gras.Ft int 832fe8fb19SBen Gras.Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap" 842fe8fb19SBen Gras.Ft int 852fe8fb19SBen Gras.Fn vsnprintf_ss "char * restrict str" "size_t size" "const char * restrict format" "va_list ap" 862fe8fb19SBen Gras.Ft int 872fe8fb19SBen Gras.Fn vasprintf "char ** restrict ret" "const char * restrict format" "va_list ap" 882fe8fb19SBen Gras.Sh DESCRIPTION 892fe8fb19SBen GrasThe 902fe8fb19SBen Gras.Fn printf 912fe8fb19SBen Grasfamily of functions produces output according to a 922fe8fb19SBen Gras.Fa format 932fe8fb19SBen Grasas described below. 942fe8fb19SBen GrasThe 952fe8fb19SBen Gras.Fn printf 962fe8fb19SBen Grasand 972fe8fb19SBen Gras.Fn vprintf 982fe8fb19SBen Grasfunctions 992fe8fb19SBen Graswrite output to 1002fe8fb19SBen Gras.Em stdout , 1012fe8fb19SBen Grasthe standard output stream; 1022fe8fb19SBen Gras.Fn fprintf 1032fe8fb19SBen Grasand 1042fe8fb19SBen Gras.Fn vfprintf 1052fe8fb19SBen Graswrite output to the given output 1062fe8fb19SBen Gras.Fa stream ; 1072fe8fb19SBen Gras.Fn dprintf 1082fe8fb19SBen Grasand 1092fe8fb19SBen Gras.Fn vdprintf 11084d9c625SLionel Sambucwrite output to the given file descriptor 1112fe8fb19SBen Gras.Fa fd ; 1122fe8fb19SBen Gras.Fn sprintf , 1132fe8fb19SBen Gras.Fn snprintf , 11484d9c625SLionel Sambuc.Fn snprintf_ss , 1152fe8fb19SBen Gras.Fn vsprintf , 1162fe8fb19SBen Gras.Fn vsnprintf , 1172fe8fb19SBen Grasand 1182fe8fb19SBen Gras.Fn vsnprintf_ss 1192fe8fb19SBen Graswrite to the character string 1202fe8fb19SBen Gras.Fa str ; 1212fe8fb19SBen Grasand 1222fe8fb19SBen Gras.Fn asprintf 1232fe8fb19SBen Grasand 1242fe8fb19SBen Gras.Fn vasprintf 1252fe8fb19SBen Graswrite to a dynamically allocated string that is stored in 1262fe8fb19SBen Gras.Fa ret . 1272fe8fb19SBen Gras.Pp 1282fe8fb19SBen GrasThese functions write the output under the control of a 1292fe8fb19SBen Gras.Fa format 1302fe8fb19SBen Grasstring that specifies how subsequent arguments 1312fe8fb19SBen Gras(or arguments accessed via the variable-length argument facilities of 1322fe8fb19SBen Gras.Xr stdarg 3 ) 1332fe8fb19SBen Grasare converted for output. 1342fe8fb19SBen Gras.Pp 13584d9c625SLionel Sambuc.Fn snprintf_ss 13684d9c625SLionel Sambucand 1372fe8fb19SBen Gras.Fn vsnprintf_ss 13884d9c625SLionel Sambucare signal-safe standalone versions that do not handle 13984d9c625SLionel Sambucfloating point formats, positional arguments, and wide characters. 1402fe8fb19SBen Gras.Pp 1412fe8fb19SBen Gras.Fn asprintf 1422fe8fb19SBen Grasand 1432fe8fb19SBen Gras.Fn vasprintf 1442fe8fb19SBen Grasreturn a pointer to a buffer sufficiently large to hold the 1452fe8fb19SBen Grasstring in the 1462fe8fb19SBen Gras.Fa ret 1472fe8fb19SBen Grasargument. 1482fe8fb19SBen GrasThis pointer should be passed to 1492fe8fb19SBen Gras.Xr free 3 1502fe8fb19SBen Grasto release the allocated storage when it is no longer needed. 1512fe8fb19SBen GrasIf sufficient space cannot be allocated, these functions 1522fe8fb19SBen Graswill return \-1 and set 1532fe8fb19SBen Gras.Fa ret 1542fe8fb19SBen Grasto be a 1552fe8fb19SBen Gras.Dv NULL 1562fe8fb19SBen Graspointer. 1572fe8fb19SBen GrasPlease note that these functions are not standardized, and not all 1582fe8fb19SBen Grasimplementations can be assumed to set the 1592fe8fb19SBen Gras.Fa ret 1602fe8fb19SBen Grasargument to 1612fe8fb19SBen Gras.Dv NULL 1622fe8fb19SBen Grason error. 1632fe8fb19SBen GrasIt is more portable to check for a return value of \-1 instead. 1642fe8fb19SBen Gras.Pp 1652fe8fb19SBen Gras.Fn snprintf , 1662fe8fb19SBen Gras.Fn vsnprintf , 1672fe8fb19SBen Grasand 1682fe8fb19SBen Gras.Fn vsnprintf_ss 1692fe8fb19SBen Graswill write at most 1702fe8fb19SBen Gras.Fa size Ns \-1 1712fe8fb19SBen Grasof the characters printed into the output string 1722fe8fb19SBen Gras(the 1732fe8fb19SBen Gras.Fa size Ns 'th 1742fe8fb19SBen Grascharacter then gets the terminating 1752fe8fb19SBen Gras.Ql \e0 ) ; 1762fe8fb19SBen Grasif the return value is greater than or equal to the 1772fe8fb19SBen Gras.Fa size 1782fe8fb19SBen Grasargument, the string was too short 1792fe8fb19SBen Grasand some of the printed characters were discarded. 1802fe8fb19SBen GrasIf 1812fe8fb19SBen Gras.Fa size 1822fe8fb19SBen Grasis zero, nothing is written and 1832fe8fb19SBen Gras.Fa str 1842fe8fb19SBen Grasmay be a 1852fe8fb19SBen Gras.Dv NULL 1862fe8fb19SBen Graspointer. 1872fe8fb19SBen Gras.Pp 1882fe8fb19SBen Gras.Fn sprintf 1892fe8fb19SBen Grasand 1902fe8fb19SBen Gras.Fn vsprintf 1912fe8fb19SBen Graseffectively assume an infinite 1922fe8fb19SBen Gras.Fa size . 1932fe8fb19SBen Gras.Pp 1942fe8fb19SBen GrasThe format string is composed of zero or more directives: 1952fe8fb19SBen Grasordinary 1962fe8fb19SBen Gras.\" multibyte 1972fe8fb19SBen Grascharacters (not 1982fe8fb19SBen Gras.Cm % ) , 1992fe8fb19SBen Graswhich are copied unchanged to the output stream; 2002fe8fb19SBen Grasand conversion specifications, each of which results 2012fe8fb19SBen Grasin fetching zero or more subsequent arguments. 2022fe8fb19SBen GrasEach conversion specification is introduced by 2032fe8fb19SBen Grasthe character 2042fe8fb19SBen Gras.Cm % . 2052fe8fb19SBen GrasThe arguments must correspond properly (after type promotion) 2062fe8fb19SBen Graswith the conversion specifier. 2072fe8fb19SBen GrasAfter the 2082fe8fb19SBen Gras.Cm % , 2092fe8fb19SBen Grasthe following appear in sequence: 2102fe8fb19SBen Gras.Bl -bullet 2112fe8fb19SBen Gras.It 2122fe8fb19SBen GrasAn optional field, consisting of a decimal digit string followed by a 2132fe8fb19SBen Gras.Cm $ , 2142fe8fb19SBen Grasspecifying the next argument to access. 2152fe8fb19SBen GrasIf this field is not provided, the argument following the last 2162fe8fb19SBen Grasargument accessed will be used. 2172fe8fb19SBen GrasArguments are numbered starting at 2182fe8fb19SBen Gras.Cm 1 . 2192fe8fb19SBen GrasIf unaccessed arguments in the format string are interspersed with ones that 2202fe8fb19SBen Grasare accessed the results will be indeterminate. 2212fe8fb19SBen Gras.It 2222fe8fb19SBen GrasZero or more of the following flags: 2232fe8fb19SBen Gras.Bl -tag -width ".So \ Sc (space)" 2242fe8fb19SBen Gras.It Sq Cm # 2252fe8fb19SBen GrasThe value should be converted to an 2262fe8fb19SBen Gras.Dq alternate form . 2272fe8fb19SBen GrasFor 2282fe8fb19SBen Gras.Cm c , 2292fe8fb19SBen Gras.Cm d , 2302fe8fb19SBen Gras.Cm i , 2312fe8fb19SBen Gras.Cm n , 2322fe8fb19SBen Gras.Cm p , 2332fe8fb19SBen Gras.Cm s , 2342fe8fb19SBen Grasand 2352fe8fb19SBen Gras.Cm u 2362fe8fb19SBen Grasconversions, this option has no effect. 2372fe8fb19SBen GrasFor 2382fe8fb19SBen Gras.Cm o 2392fe8fb19SBen Grasconversions, the precision of the number is increased to force the first 2402fe8fb19SBen Grascharacter of the output string to a zero (except if a zero value is printed 2412fe8fb19SBen Graswith an explicit precision of zero). 2422fe8fb19SBen GrasFor 2432fe8fb19SBen Gras.Cm x 2442fe8fb19SBen Grasand 2452fe8fb19SBen Gras.Cm X 2462fe8fb19SBen Grasconversions, a non-zero result has the string 2472fe8fb19SBen Gras.Ql 0x 2482fe8fb19SBen Gras(or 2492fe8fb19SBen Gras.Ql 0X 2502fe8fb19SBen Grasfor 2512fe8fb19SBen Gras.Cm X 2522fe8fb19SBen Grasconversions) prepended to it. 2532fe8fb19SBen GrasFor 2542fe8fb19SBen Gras.Cm a , 2552fe8fb19SBen Gras.Cm A , 2562fe8fb19SBen Gras.Cm e , 2572fe8fb19SBen Gras.Cm E , 2582fe8fb19SBen Gras.Cm f , 2592fe8fb19SBen Gras.Cm F , 2602fe8fb19SBen Gras.Cm g , 2612fe8fb19SBen Grasand 2622fe8fb19SBen Gras.Cm G 2632fe8fb19SBen Grasconversions, the result will always contain a decimal point, even if no 2642fe8fb19SBen Grasdigits follow it (normally, a decimal point appears in the results of 2652fe8fb19SBen Grasthose conversions only if a digit follows). 2662fe8fb19SBen GrasFor 2672fe8fb19SBen Gras.Cm g 2682fe8fb19SBen Grasand 2692fe8fb19SBen Gras.Cm G 2702fe8fb19SBen Grasconversions, trailing zeros are not removed from the result as they 2712fe8fb19SBen Graswould otherwise be. 2722fe8fb19SBen Gras.It So Cm 0 Sc (zero) 2732fe8fb19SBen GrasZero padding. 2742fe8fb19SBen GrasFor all conversions except 2752fe8fb19SBen Gras.Cm n , 2762fe8fb19SBen Grasthe converted value is padded on the left with zeros rather than blanks. 2772fe8fb19SBen GrasIf a precision is given with a numeric conversion 2782fe8fb19SBen Gras.Pf ( Cm d , 2792fe8fb19SBen Gras.Cm i , 2802fe8fb19SBen Gras.Cm o , 2812fe8fb19SBen Gras.Cm u , 2822fe8fb19SBen Gras.Cm i , 2832fe8fb19SBen Gras.Cm x , 2842fe8fb19SBen Grasand 2852fe8fb19SBen Gras.Cm X ) , 2862fe8fb19SBen Grasthe 2872fe8fb19SBen Gras.Cm 0 2882fe8fb19SBen Grasflag is ignored. 2892fe8fb19SBen Gras.It Sq Cm \- 2902fe8fb19SBen GrasA negative field width flag; 2912fe8fb19SBen Grasthe converted value is to be left adjusted on the field boundary. 2922fe8fb19SBen GrasExcept for 2932fe8fb19SBen Gras.Cm n 2942fe8fb19SBen Grasconversions, the converted value is padded on the right with blanks, 2952fe8fb19SBen Grasrather than on the left with blanks or zeros. 2962fe8fb19SBen GrasA 2972fe8fb19SBen Gras.Sq Cm \- 2982fe8fb19SBen Grasoverrides a 2992fe8fb19SBen Gras.Sq Cm \&0 3002fe8fb19SBen Grasif both are given. 3012fe8fb19SBen Gras.It So "\ " Sc (space) 3022fe8fb19SBen GrasA blank should be left before a positive number 3032fe8fb19SBen Grasproduced by a signed conversion 3042fe8fb19SBen Gras.Pf ( Cm a , 3052fe8fb19SBen Gras.Cm A 3062fe8fb19SBen Gras.Cm d , 3072fe8fb19SBen Gras.Cm e , 3082fe8fb19SBen Gras.Cm E , 3092fe8fb19SBen Gras.Cm f , 3102fe8fb19SBen Gras.Cm F , 3112fe8fb19SBen Gras.Cm g , 3122fe8fb19SBen Gras.Cm G , 3132fe8fb19SBen Grasor 3142fe8fb19SBen Gras.Cm i ) . 3152fe8fb19SBen Gras.It Sq Cm + 3162fe8fb19SBen GrasA sign must always be placed before a 3172fe8fb19SBen Grasnumber produced by a signed conversion. 3182fe8fb19SBen GrasA 3192fe8fb19SBen Gras.Sq Cm + 3202fe8fb19SBen Grasoverrides a space if both are used. 3212fe8fb19SBen Gras.It Sq Cm ' 3222fe8fb19SBen GrasDecimal conversions 3232fe8fb19SBen Gras.Cm ( d , u , 3242fe8fb19SBen Grasor 3252fe8fb19SBen Gras.Cm i ) 3262fe8fb19SBen Grasor the integral portion of a floating point conversion 3272fe8fb19SBen Gras.Cm ( f 3282fe8fb19SBen Grasor 3292fe8fb19SBen Gras.Cm F ) 3302fe8fb19SBen Grasshould be grouped and separated by thousands using 3312fe8fb19SBen Grasthe non-monetary separator returned by 3322fe8fb19SBen Gras.Xr localeconv 3 . 3332fe8fb19SBen Gras.El 3342fe8fb19SBen Gras.It 3352fe8fb19SBen GrasAn optional decimal digit string specifying a minimum field width. 3362fe8fb19SBen GrasIf the converted value has fewer characters than the field width, it will 3372fe8fb19SBen Grasbe padded with spaces on the left (or right, if the left-adjustment 3382fe8fb19SBen Grasflag has been given) to fill out the field width. 3392fe8fb19SBen Gras.It 3402fe8fb19SBen GrasAn optional precision, in the form of a period 3412fe8fb19SBen Gras.Sq Cm \&. 3422fe8fb19SBen Grasfollowed by an optional digit string. 3432fe8fb19SBen GrasIf the digit string is omitted, the precision is taken as zero. 3442fe8fb19SBen GrasThis gives the minimum number of digits to appear for 3452fe8fb19SBen Gras.Cm d , 3462fe8fb19SBen Gras.Cm i , 3472fe8fb19SBen Gras.Cm o , 3482fe8fb19SBen Gras.Cm u , 3492fe8fb19SBen Gras.Cm x , 3502fe8fb19SBen Grasand 3512fe8fb19SBen Gras.Cm X 3522fe8fb19SBen Grasconversions, the number of digits to appear after the decimal-point for 3532fe8fb19SBen Gras.Cm a , 3542fe8fb19SBen Gras.Cm A , 3552fe8fb19SBen Gras.Cm e , 3562fe8fb19SBen Gras.Cm E , 3572fe8fb19SBen Gras.Cm f , 3582fe8fb19SBen Grasand 3592fe8fb19SBen Gras.Cm F 3602fe8fb19SBen Grasconversions, the maximum number of significant digits for 3612fe8fb19SBen Gras.Cm g 3622fe8fb19SBen Grasand 3632fe8fb19SBen Gras.Cm G 3642fe8fb19SBen Grasconversions, or the maximum number of characters to be printed from a 3652fe8fb19SBen Grasstring for 3662fe8fb19SBen Gras.Cm s 3672fe8fb19SBen Grasconversions. 3682fe8fb19SBen Gras.It 3692fe8fb19SBen GrasAn optional length modifier, that specifies the size of the argument. 3702fe8fb19SBen GrasThe following length modifiers are valid for the 3712fe8fb19SBen Gras.Cm d , i , n , o , u , x , 3722fe8fb19SBen Grasor 3732fe8fb19SBen Gras.Cm X 3742fe8fb19SBen Grasconversion: 3752fe8fb19SBen Gras.Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *" 3762fe8fb19SBen Gras.It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n 3772fe8fb19SBen Gras.It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *" 3782fe8fb19SBen Gras.It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *" 3792fe8fb19SBen Gras.It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *" 3802fe8fb19SBen Gras.It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *" 3812fe8fb19SBen Gras.It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *" 3822fe8fb19SBen Gras.It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *" 3832fe8fb19SBen Gras.It Cm z Ta (see note) Ta Vt size_t Ta (see note) 3842fe8fb19SBen Gras.It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *" 3852fe8fb19SBen Gras.El 3862fe8fb19SBen Gras.Pp 3872fe8fb19SBen GrasNote: 3882fe8fb19SBen Grasthe 3892fe8fb19SBen Gras.Cm t 3902fe8fb19SBen Grasmodifier, when applied to a 3912fe8fb19SBen Gras.Cm o , u , x , 3922fe8fb19SBen Grasor 3932fe8fb19SBen Gras.Cm X 3942fe8fb19SBen Grasconversion, indicates that the argument is of an unsigned type 3952fe8fb19SBen Grasequivalent in size to a 3962fe8fb19SBen Gras.Vt ptrdiff_t . 3972fe8fb19SBen GrasThe 3982fe8fb19SBen Gras.Cm z 3992fe8fb19SBen Grasmodifier, when applied to a 4002fe8fb19SBen Gras.Cm d 4012fe8fb19SBen Grasor 4022fe8fb19SBen Gras.Cm i 4032fe8fb19SBen Grasconversion, indicates that the argument is of a signed type equivalent in 4042fe8fb19SBen Grassize to a 4052fe8fb19SBen Gras.Vt size_t . 4062fe8fb19SBen GrasSimilarly, when applied to an 4072fe8fb19SBen Gras.Cm n 4082fe8fb19SBen Grasconversion, it indicates that the argument is a pointer to a signed type 4092fe8fb19SBen Grasequivalent in size to a 4102fe8fb19SBen Gras.Vt size_t . 4112fe8fb19SBen Gras.Pp 4122fe8fb19SBen GrasNote: 4132fe8fb19SBen Grasif the standard integer types described in 4142fe8fb19SBen Gras.Xr stdint 3 4152fe8fb19SBen Grasare used, it is recommended that the predefined format string specifier 4162fe8fb19SBen Grasmacros are used when possible. 4172fe8fb19SBen GrasThese are further described in 4182fe8fb19SBen Gras.Xr inttypes 3 . 4192fe8fb19SBen Gras.Pp 4202fe8fb19SBen GrasThe following length modifier is valid for the 4212fe8fb19SBen Gras.Cm a , 4222fe8fb19SBen Gras.Cm A , 4232fe8fb19SBen Gras.Cm e , 4242fe8fb19SBen Gras.Cm E , 4252fe8fb19SBen Gras.Cm f , 4262fe8fb19SBen Gras.Cm F , 4272fe8fb19SBen Gras.Cm g , 4282fe8fb19SBen Grasor 4292fe8fb19SBen Gras.Cm G 4302fe8fb19SBen Grasconversion: 4312fe8fb19SBen Gras.Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G" 4322fe8fb19SBen Gras.It Sy Modifier Ta Cm a , A , e , E , f , F , g , G 4332fe8fb19SBen Gras.It Cm l No (ell) Ta Vt double 4342fe8fb19SBen Gras(ignored, same behavior as without it) 4352fe8fb19SBen Gras.It Cm L Ta Vt "long double" 4362fe8fb19SBen Gras.El 4372fe8fb19SBen Gras.Pp 4382fe8fb19SBen GrasThe following length modifier is valid for the 4392fe8fb19SBen Gras.Cm c 4402fe8fb19SBen Grasor 4412fe8fb19SBen Gras.Cm s 4422fe8fb19SBen Grasconversion: 4432fe8fb19SBen Gras.Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *" 4442fe8fb19SBen Gras.It Sy Modifier Ta Cm c Ta Cm s 4452fe8fb19SBen Gras.It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *" 4462fe8fb19SBen Gras.El 4472fe8fb19SBen Gras.It 4482fe8fb19SBen GrasA character that specifies the type of conversion to be applied. 4492fe8fb19SBen Gras.El 4502fe8fb19SBen Gras.Pp 4512fe8fb19SBen GrasA field width or precision, or both, may be indicated by 4522fe8fb19SBen Grasan asterisk 4532fe8fb19SBen Gras.Ql * 4542fe8fb19SBen Grasor an asterisk followed by one or more decimal digits and a 4552fe8fb19SBen Gras.Ql $ 4562fe8fb19SBen Grasinstead of a 4572fe8fb19SBen Grasdigit string. 4582fe8fb19SBen GrasIn this case, an 4592fe8fb19SBen Gras.Vt int 4602fe8fb19SBen Grasargument supplies the field width or precision. 4612fe8fb19SBen GrasA negative field width is treated as a left adjustment flag followed by a 4622fe8fb19SBen Graspositive field width; a negative precision is treated as though it were 4632fe8fb19SBen Grasmissing. 4642fe8fb19SBen GrasIf a single format directive mixes positional 4652fe8fb19SBen Gras.Pq Li nn$ 4662fe8fb19SBen Grasand non-positional arguments, the results are undefined. 4672fe8fb19SBen Gras.Pp 4682fe8fb19SBen GrasThe conversion specifiers and their meanings are: 4692fe8fb19SBen Gras.Bl -tag -width ".Cm diouxX" 4702fe8fb19SBen Gras.It Cm diouxX 4712fe8fb19SBen GrasThe 4722fe8fb19SBen Gras.Vt int 4732fe8fb19SBen Gras(or appropriate variant) argument is converted to signed decimal 4742fe8fb19SBen Gras.Pf ( Cm d 4752fe8fb19SBen Grasand 4762fe8fb19SBen Gras.Cm i ) , 4772fe8fb19SBen Grasunsigned octal 4782fe8fb19SBen Gras.Pq Cm o , 4792fe8fb19SBen Grasunsigned decimal 4802fe8fb19SBen Gras.Pq Cm u , 4812fe8fb19SBen Grasor unsigned hexadecimal 4822fe8fb19SBen Gras.Pf ( Cm x 4832fe8fb19SBen Grasand 4842fe8fb19SBen Gras.Cm X ) 4852fe8fb19SBen Grasnotation. 4862fe8fb19SBen GrasThe letters 4872fe8fb19SBen Gras.Dq Li abcdef 4882fe8fb19SBen Grasare used for 4892fe8fb19SBen Gras.Cm x 4902fe8fb19SBen Grasconversions; the letters 4912fe8fb19SBen Gras.Dq Li ABCDEF 4922fe8fb19SBen Grasare used for 4932fe8fb19SBen Gras.Cm X 4942fe8fb19SBen Grasconversions. 4952fe8fb19SBen GrasThe precision, if any, gives the minimum number of digits that must 4962fe8fb19SBen Grasappear; if the converted value requires fewer digits, it is padded on 4972fe8fb19SBen Grasthe left with zeros. 4982fe8fb19SBen Gras.It Cm DOU 4992fe8fb19SBen GrasThe 5002fe8fb19SBen Gras.Vt long int 5012fe8fb19SBen Grasargument is converted to signed decimal, unsigned octal, or unsigned 5022fe8fb19SBen Grasdecimal, as if the format had been 5032fe8fb19SBen Gras.Cm ld , 5042fe8fb19SBen Gras.Cm lo , 5052fe8fb19SBen Grasor 5062fe8fb19SBen Gras.Cm lu 5072fe8fb19SBen Grasrespectively. 5082fe8fb19SBen GrasThese conversion characters are deprecated, and will eventually disappear. 5092fe8fb19SBen Gras.It Cm eE 5102fe8fb19SBen GrasThe 5112fe8fb19SBen Gras.Vt double 5122fe8fb19SBen Grasargument is rounded and converted in the style 5132fe8fb19SBen Gras.Sm off 5142fe8fb19SBen Gras.Oo \- Oc Ar d Li \&. Ar ddd Li e \*[Pm] Ar dd 5152fe8fb19SBen Gras.Sm on 5162fe8fb19SBen Graswhere there is one digit before the 5172fe8fb19SBen Grasdecimal-point character 5182fe8fb19SBen Grasand the number of digits after it is equal to the precision; 5192fe8fb19SBen Grasif the precision is missing, 5202fe8fb19SBen Grasit is taken as 6; if the precision is 5212fe8fb19SBen Graszero, no decimal-point character appears. 5222fe8fb19SBen GrasAn 5232fe8fb19SBen Gras.Cm E 5242fe8fb19SBen Grasconversion uses the letter 5252fe8fb19SBen Gras.Ql E 5262fe8fb19SBen Gras(rather than 5272fe8fb19SBen Gras.Ql e ) 5282fe8fb19SBen Grasto introduce the exponent. 5292fe8fb19SBen GrasThe exponent always contains at least two digits; if the value is zero, 5302fe8fb19SBen Grasthe exponent is 00. 5312fe8fb19SBen Gras.Pp 5322fe8fb19SBen GrasFor 5332fe8fb19SBen Gras.Cm a , 5342fe8fb19SBen Gras.Cm A , 5352fe8fb19SBen Gras.Cm e , 5362fe8fb19SBen Gras.Cm E , 5372fe8fb19SBen Gras.Cm f , 5382fe8fb19SBen Gras.Cm F , 5392fe8fb19SBen Gras.Cm g , 5402fe8fb19SBen Grasand 5412fe8fb19SBen Gras.Cm G 5422fe8fb19SBen Grasconversions, positive and negative infinity are represented as 5432fe8fb19SBen Gras.Li inf 5442fe8fb19SBen Grasand 5452fe8fb19SBen Gras.Li -inf 5462fe8fb19SBen Grasrespectively when using the lowercase conversion character, and 5472fe8fb19SBen Gras.Li INF 5482fe8fb19SBen Grasand 5492fe8fb19SBen Gras.Li -INF 5502fe8fb19SBen Grasrespectively when using the uppercase conversion character. 5512fe8fb19SBen GrasSimilarly, NaN is represented as 5522fe8fb19SBen Gras.Li nan 5532fe8fb19SBen Graswhen using the lowercase conversion, and 5542fe8fb19SBen Gras.Li NAN 5552fe8fb19SBen Graswhen using the uppercase conversion. 5562fe8fb19SBen Gras.It Cm fF 5572fe8fb19SBen GrasThe 5582fe8fb19SBen Gras.Vt double 5592fe8fb19SBen Grasargument is rounded and converted to decimal notation in the style 5602fe8fb19SBen Gras.Sm off 5612fe8fb19SBen Gras.Oo \- Oc Ar ddd Li \&. Ar ddd , 5622fe8fb19SBen Gras.Sm on 5632fe8fb19SBen Graswhere the number of digits after the decimal-point character 5642fe8fb19SBen Grasis equal to the precision specification. 5652fe8fb19SBen GrasIf the precision is missing, it is taken as 6; if the precision is 5662fe8fb19SBen Grasexplicitly zero, no decimal-point character appears. 5672fe8fb19SBen GrasIf a decimal point appears, at least one digit appears before it. 5682fe8fb19SBen Gras.It Cm gG 5692fe8fb19SBen GrasThe 5702fe8fb19SBen Gras.Vt double 5712fe8fb19SBen Grasargument is converted in style 5722fe8fb19SBen Gras.Cm f 5732fe8fb19SBen Grasor 5742fe8fb19SBen Gras.Cm e 5752fe8fb19SBen Gras(or in style 5762fe8fb19SBen Gras.Cm F 5772fe8fb19SBen Grasor 5782fe8fb19SBen Gras.Cm E 5792fe8fb19SBen Grasfor 5802fe8fb19SBen Gras.Cm G 5812fe8fb19SBen Grasconversions). 5822fe8fb19SBen GrasThe precision specifies the number of significant digits. 5832fe8fb19SBen GrasIf the precision is missing, 6 digits are given; if the precision is zero, 5842fe8fb19SBen Grasit is treated as 1. 5852fe8fb19SBen GrasStyle 5862fe8fb19SBen Gras.Cm e 5872fe8fb19SBen Grasis used if the exponent from its conversion is less than \-4 or greater than 5882fe8fb19SBen Grasor equal to the precision. 5892fe8fb19SBen GrasTrailing zeros are removed from the fractional part of the result; a 5902fe8fb19SBen Grasdecimal point appears only if it is followed by at least one digit. 5912fe8fb19SBen Gras.It Cm aA 5922fe8fb19SBen GrasThe 5932fe8fb19SBen Gras.Vt double 5942fe8fb19SBen Grasargument is rounded and converted to hexadecimal notation in the style 5952fe8fb19SBen Gras.Sm off 5962fe8fb19SBen Gras.Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \*[Pm] Oc Ar d , 5972fe8fb19SBen Gras.Sm on 5982fe8fb19SBen Graswhere the number of digits after the hexadecimal-point character 5992fe8fb19SBen Grasis equal to the precision specification. 6002fe8fb19SBen GrasIf the precision is missing, it is taken as enough to represent 6012fe8fb19SBen Grasthe floating-point number exactly, and no rounding occurs. 6022fe8fb19SBen GrasIf the precision is zero, no hexadecimal-point character appears. 6032fe8fb19SBen GrasThe 6042fe8fb19SBen Gras.Cm p 6052fe8fb19SBen Grasis a literal character 6062fe8fb19SBen Gras.Ql p , 6072fe8fb19SBen Grasand the exponent consists of a positive or negative sign 6082fe8fb19SBen Grasfollowed by a decimal number representing an exponent of 2. 6092fe8fb19SBen GrasThe 6102fe8fb19SBen Gras.Cm A 6112fe8fb19SBen Grasconversion uses the prefix 6122fe8fb19SBen Gras.Dq Li 0X 6132fe8fb19SBen Gras(rather than 6142fe8fb19SBen Gras.Dq Li 0x ) , 6152fe8fb19SBen Grasthe letters 6162fe8fb19SBen Gras.Dq Li ABCDEF 6172fe8fb19SBen Gras(rather than 6182fe8fb19SBen Gras.Dq Li abcdef ) 6192fe8fb19SBen Grasto represent the hex digits, and the letter 6202fe8fb19SBen Gras.Ql P 6212fe8fb19SBen Gras(rather than 6222fe8fb19SBen Gras.Ql p ) 6232fe8fb19SBen Grasto separate the mantissa and exponent. 6242fe8fb19SBen Gras.Pp 6252fe8fb19SBen GrasNote that there may be multiple valid ways to represent floating-point 6262fe8fb19SBen Grasnumbers in this hexadecimal format. 6272fe8fb19SBen GrasFor example, 6282fe8fb19SBen Gras.Li 0x3.24p+0 , 0x6.48p-1 6292fe8fb19SBen Grasand 6302fe8fb19SBen Gras.Li 0xc.9p-2 6312fe8fb19SBen Grasare all equivalent. 6322fe8fb19SBen GrasThe format chosen depends on the internal representation of the 6332fe8fb19SBen Grasnumber, but the implementation guarantees that the length of the 6342fe8fb19SBen Grasmantissa will be minimized. 6352fe8fb19SBen GrasZeroes are always represented with a mantissa of 0 (preceded by a 6362fe8fb19SBen Gras.Ql - 6372fe8fb19SBen Grasif appropriate) and an exponent of 6382fe8fb19SBen Gras.Li +0 . 6392fe8fb19SBen Gras.It Cm C 6402fe8fb19SBen GrasTreated as 6412fe8fb19SBen Gras.Cm c 6422fe8fb19SBen Graswith the 6432fe8fb19SBen Gras.Cm l 6442fe8fb19SBen Gras(ell) modifier. 6452fe8fb19SBen Gras.It Cm c 6462fe8fb19SBen GrasThe 6472fe8fb19SBen Gras.Vt int 6482fe8fb19SBen Grasargument is converted to an 6492fe8fb19SBen Gras.Vt "unsigned char" , 6502fe8fb19SBen Grasand the resulting character is written. 6512fe8fb19SBen Gras.Pp 6522fe8fb19SBen GrasIf the 6532fe8fb19SBen Gras.Cm l 6542fe8fb19SBen Gras(ell) modifier is used, the 6552fe8fb19SBen Gras.Vt wint_t 6562fe8fb19SBen Grasargument shall be converted to a 6572fe8fb19SBen Gras.Vt wchar_t , 6582fe8fb19SBen Grasand the (potentially multi-byte) sequence representing the 6592fe8fb19SBen Grassingle wide character is written, including any shift sequences. 6602fe8fb19SBen GrasIf a shift sequence is used, the shift state is also restored 6612fe8fb19SBen Grasto the original state after the character. 6622fe8fb19SBen Gras.It Cm S 6632fe8fb19SBen GrasTreated as 6642fe8fb19SBen Gras.Cm s 6652fe8fb19SBen Graswith the 6662fe8fb19SBen Gras.Cm l 6672fe8fb19SBen Gras(ell) modifier. 6682fe8fb19SBen Gras.It Cm s 6692fe8fb19SBen GrasThe 6702fe8fb19SBen Gras.Vt "char *" 6712fe8fb19SBen Grasargument is expected to be a pointer to an array of character type (pointer 6722fe8fb19SBen Grasto a string). 6732fe8fb19SBen GrasCharacters from the array are written up to (but not including) 6742fe8fb19SBen Grasa terminating 6752fe8fb19SBen Gras.Dv NUL 6762fe8fb19SBen Grascharacter; 6772fe8fb19SBen Grasif a precision is specified, no more than the number specified are 6782fe8fb19SBen Graswritten. 6792fe8fb19SBen GrasIf a precision is given, no null character 6802fe8fb19SBen Grasneed be present; if the precision is not specified, or is greater than 6812fe8fb19SBen Grasthe size of the array, the array must contain a terminating 6822fe8fb19SBen Gras.Dv NUL 6832fe8fb19SBen Grascharacter. 6842fe8fb19SBen Gras.Pp 6852fe8fb19SBen GrasIf the 6862fe8fb19SBen Gras.Cm l 6872fe8fb19SBen Gras(ell) modifier is used, the 6882fe8fb19SBen Gras.Vt "wchar_t *" 6892fe8fb19SBen Grasargument is expected to be a pointer to an array of wide characters 6902fe8fb19SBen Gras(pointer to a wide string). 6912fe8fb19SBen GrasFor each wide character in the string, the (potentially multi-byte) 6922fe8fb19SBen Grassequence representing the 6932fe8fb19SBen Graswide character is written, including any shift sequences. 6942fe8fb19SBen GrasIf any shift sequence is used, the shift state is also restored 6952fe8fb19SBen Grasto the original state after the string. 6962fe8fb19SBen GrasWide characters from the array are written up to (but not including) 6972fe8fb19SBen Grasa terminating wide 6982fe8fb19SBen Gras.Dv NUL 6992fe8fb19SBen Grascharacter; 7002fe8fb19SBen Grasif a precision is specified, no more than the number of bytes specified are 7012fe8fb19SBen Graswritten (including shift sequences). 7022fe8fb19SBen GrasPartial characters are never written. 7032fe8fb19SBen GrasIf a precision is given, no null character 7042fe8fb19SBen Grasneed be present; if the precision is not specified, or is greater than 7052fe8fb19SBen Grasthe number of bytes required to render the multibyte representation of 7062fe8fb19SBen Grasthe string, the array must contain a terminating wide 7072fe8fb19SBen Gras.Dv NUL 7082fe8fb19SBen Grascharacter. 7092fe8fb19SBen Gras.It Cm p 7102fe8fb19SBen GrasThe 7112fe8fb19SBen Gras.Vt "void *" 7122fe8fb19SBen Graspointer argument is printed in hexadecimal (as if by 7132fe8fb19SBen Gras.Ql %#x 7142fe8fb19SBen Grasor 7152fe8fb19SBen Gras.Ql %#lx ) . 7162fe8fb19SBen Gras.It Cm n 7172fe8fb19SBen GrasThe number of characters written so far is stored into the 7182fe8fb19SBen Grasinteger indicated by the 7192fe8fb19SBen Gras.Vt "int *" 7202fe8fb19SBen Gras(or variant) pointer argument. 7212fe8fb19SBen GrasNo argument is converted. 7222fe8fb19SBen Gras.It Cm % 7232fe8fb19SBen GrasA 7242fe8fb19SBen Gras.Ql % 7252fe8fb19SBen Grasis written. 7262fe8fb19SBen GrasNo argument is converted. 7272fe8fb19SBen GrasThe complete conversion specification is 7282fe8fb19SBen Gras.Ql %% . 7292fe8fb19SBen Gras.El 7302fe8fb19SBen Gras.Pp 7312fe8fb19SBen GrasThe decimal point 7322fe8fb19SBen Grascharacter is defined in the program's locale (category 7332fe8fb19SBen Gras.Dv LC_NUMERIC ) . 7342fe8fb19SBen Gras.Pp 7352fe8fb19SBen GrasIn no case does a non-existent or small field width cause truncation of 7362fe8fb19SBen Grasa numeric field; if the result of a conversion is wider than the field 7372fe8fb19SBen Graswidth, the 7382fe8fb19SBen Grasfield is expanded to contain the conversion result. 7392fe8fb19SBen Gras.Sh RETURN VALUES 7402fe8fb19SBen GrasThese functions return 7412fe8fb19SBen Grasthe number of characters printed, or that would be printed if there 7422fe8fb19SBen Graswas adequate space in case of 7432fe8fb19SBen Gras.Fn snprintf , 7442fe8fb19SBen Gras.Fn vsnprintf , 7452fe8fb19SBen Grasand 7462fe8fb19SBen Gras.Fn vsnprintf_ss 7472fe8fb19SBen Gras(not including the trailing 7482fe8fb19SBen Gras.Ql \e0 7492fe8fb19SBen Grasused to end output to strings). 7502fe8fb19SBen GrasIf an output error was encountered, these functions shall return a 7512fe8fb19SBen Grasnegative value. 7522fe8fb19SBen Gras.Sh EXAMPLES 7532fe8fb19SBen GrasTo print a date and time in the form 7542fe8fb19SBen Gras.Dq Li "Sunday, July 3, 10:02" , 7552fe8fb19SBen Graswhere 7562fe8fb19SBen Gras.Fa weekday 7572fe8fb19SBen Grasand 7582fe8fb19SBen Gras.Fa month 7592fe8fb19SBen Grasare pointers to strings: 7602fe8fb19SBen Gras.Bd -literal -offset indent 7612fe8fb19SBen Gras#include \*[Lt]stdio.h\*[Gt] 7622fe8fb19SBen Grasfprintf(stdout, "%s, %s %d, %.2d:%.2d\en", 7632fe8fb19SBen Gras weekday, month, day, hour, min); 7642fe8fb19SBen Gras.Ed 7652fe8fb19SBen Gras.Pp 7662fe8fb19SBen GrasTo print \*(Pi 7672fe8fb19SBen Grasto five decimal places: 7682fe8fb19SBen Gras.Bd -literal -offset indent 7692fe8fb19SBen Gras#include \*[Lt]math.h\*[Gt] 7702fe8fb19SBen Gras#include \*[Lt]stdio.h\*[Gt] 7712fe8fb19SBen Grasfprintf(stdout, "pi = %.5f\en", 4 * atan(1.0)); 7722fe8fb19SBen Gras.Ed 7732fe8fb19SBen Gras.Pp 7742fe8fb19SBen GrasTo allocate a 128 byte string and print into it: 7752fe8fb19SBen Gras.Bd -literal -offset indent 7762fe8fb19SBen Gras#include \*[Lt]stdio.h\*[Gt] 7772fe8fb19SBen Gras#include \*[Lt]stdlib.h\*[Gt] 7782fe8fb19SBen Gras#include \*[Lt]stdarg.h\*[Gt] 7792fe8fb19SBen Graschar *newfmt(const char *fmt, ...) 7802fe8fb19SBen Gras{ 7812fe8fb19SBen Gras char *p; 7822fe8fb19SBen Gras va_list ap; 7832fe8fb19SBen Gras if ((p = malloc(128)) == NULL) 7842fe8fb19SBen Gras return (NULL); 7852fe8fb19SBen Gras va_start(ap, fmt); 7862fe8fb19SBen Gras (void) vsnprintf(p, 128, fmt, ap); 7872fe8fb19SBen Gras va_end(ap); 7882fe8fb19SBen Gras return (p); 7892fe8fb19SBen Gras} 7902fe8fb19SBen Gras.Ed 7912fe8fb19SBen Gras.Sh ERRORS 7922fe8fb19SBen GrasIn addition to the errors documented for the 7932fe8fb19SBen Gras.Xr write 2 7942fe8fb19SBen Grassystem call, the 7952fe8fb19SBen Gras.Fn printf 7962fe8fb19SBen Grasfamily of functions may fail if: 7972fe8fb19SBen Gras.Bl -tag -width Er 7982fe8fb19SBen Gras.It Bq Er EILSEQ 7992fe8fb19SBen GrasAn invalid wide-character code was encountered. 8002fe8fb19SBen Gras.It Bq Er ENOMEM 8012fe8fb19SBen GrasInsufficient storage space is available. 802*0a6a1f1dSLionel Sambuc.It Bq Er EOVERFLOW 803*0a6a1f1dSLionel SambucThe 804*0a6a1f1dSLionel Sambuc.Fa size 805*0a6a1f1dSLionel Sambucargument exceeds 806*0a6a1f1dSLionel Sambuc.Dv INT_MAX , 807*0a6a1f1dSLionel Sambucor the return value would be too large to be represented by an 808*0a6a1f1dSLionel Sambuc.Vt int . 8092fe8fb19SBen Gras.El 8102fe8fb19SBen Gras.Sh SEE ALSO 8112fe8fb19SBen Gras.Xr printf 1 , 8122fe8fb19SBen Gras.Xr fmtcheck 3 , 8132fe8fb19SBen Gras.Xr scanf 3 , 8142fe8fb19SBen Gras.Xr setlocale 3 , 8152fe8fb19SBen Gras.Xr wprintf 3 , 8162fe8fb19SBen Gras.Xr printf 9 8172fe8fb19SBen Gras.Sh STANDARDS 8182fe8fb19SBen GrasSubject to the caveats noted in the 8192fe8fb19SBen Gras.Sx BUGS 8202fe8fb19SBen Grassection below, the 8212fe8fb19SBen Gras.Fn fprintf , 8222fe8fb19SBen Gras.Fn printf , 8232fe8fb19SBen Gras.Fn sprintf , 8242fe8fb19SBen Gras.Fn vprintf , 8252fe8fb19SBen Gras.Fn vfprintf , 8262fe8fb19SBen Grasand 8272fe8fb19SBen Gras.Fn vsprintf 8282fe8fb19SBen Grasfunctions 8292fe8fb19SBen Grasconform to 8302fe8fb19SBen Gras.St -ansiC 8312fe8fb19SBen Grasand 8322fe8fb19SBen Gras.St -isoC-99 . 8332fe8fb19SBen GrasWith the same reservation, the 8342fe8fb19SBen Gras.Fn snprintf 8352fe8fb19SBen Grasand 8362fe8fb19SBen Gras.Fn vsnprintf 8372fe8fb19SBen Grasfunctions conform to 8382fe8fb19SBen Gras.St -isoC-99 . 8392fe8fb19SBen Gras.Sh HISTORY 8402fe8fb19SBen GrasThe functions 8412fe8fb19SBen Gras.Fn snprintf 8422fe8fb19SBen Grasand 8432fe8fb19SBen Gras.Fn vsnprintf 8442fe8fb19SBen Grasfirst appeared in 8452fe8fb19SBen Gras.Bx 4.4 . 8462fe8fb19SBen GrasThe functions 8472fe8fb19SBen Gras.Fn asprintf 8482fe8fb19SBen Grasand 8492fe8fb19SBen Gras.Fn vasprintf 8502fe8fb19SBen Grasare modeled on the ones that first appeared in the GNU C library. 8512fe8fb19SBen GrasThe function 8522fe8fb19SBen Gras.Fn vsnprintf_ss 8532fe8fb19SBen Grasis non-standard and appeared in 8542fe8fb19SBen Gras.Nx 4.0 . 8552fe8fb19SBen GrasThe functions 8562fe8fb19SBen Gras.Fn dprintf 8572fe8fb19SBen Grasand 8582fe8fb19SBen Gras.Fn vdprintf 8592fe8fb19SBen Grasare parts of 8602fe8fb19SBen Gras.St -p1003.1-2008 8612fe8fb19SBen Grasand appeared in 8622fe8fb19SBen Gras.Nx 6.0 . 8632fe8fb19SBen Gras.Sh CAVEATS 8642fe8fb19SBen GrasBecause 8652fe8fb19SBen Gras.Fn sprintf 8662fe8fb19SBen Grasand 8672fe8fb19SBen Gras.Fn vsprintf 8682fe8fb19SBen Grasassume an infinitely long string, callers must be careful not to 8692fe8fb19SBen Grasoverflow the actual space; this is often impossible to assure. 8702fe8fb19SBen GrasFor safety, programmers should use the 8712fe8fb19SBen Gras.Fn snprintf 8722fe8fb19SBen Grasand 8732fe8fb19SBen Gras.Fn asprintf 8742fe8fb19SBen Grasfamily of interfaces instead. 8752fe8fb19SBen GrasUnfortunately, the 8762fe8fb19SBen Gras.Fn snprintf 8772fe8fb19SBen Grasinterfaces are not available on older 8782fe8fb19SBen Grassystems and the 8792fe8fb19SBen Gras.Fn asprintf 8802fe8fb19SBen Grasinterfaces are not yet portable. 8812fe8fb19SBen Gras.Pp 8822fe8fb19SBen GrasIt is important never to pass a string with user-supplied data as a 8832fe8fb19SBen Grasformat without using 8842fe8fb19SBen Gras.Ql %s . 8852fe8fb19SBen GrasAn attacker can put format specifiers in the string to mangle your stack, 8862fe8fb19SBen Grasleading to a possible security hole. 8872fe8fb19SBen GrasThis holds true even if you have built the string 8882fe8fb19SBen Gras.Dq by hand 8892fe8fb19SBen Grasusing a function like 8902fe8fb19SBen Gras.Fn snprintf , 8912fe8fb19SBen Grasas the resulting string may still contain user-supplied conversion specifiers 8922fe8fb19SBen Grasfor later interpolation by 8932fe8fb19SBen Gras.Fn printf . 8942fe8fb19SBen Gras.Pp 8952fe8fb19SBen GrasBe sure to use the proper secure idiom: 8962fe8fb19SBen Gras.Bd -literal -offset indent 8972fe8fb19SBen Grassnprintf(buffer, sizeof(buffer), "%s", string); 8982fe8fb19SBen Gras.Ed 8992fe8fb19SBen Gras.Pp 90084d9c625SLionel SambucThere is no way for 90184d9c625SLionel Sambuc.Fn printf 90284d9c625SLionel Sambucto know the size of each argument passed. 9032fe8fb19SBen GrasIf you use positional arguments you must ensure that all parameters, up to the 9042fe8fb19SBen Graslast positionally specified parameter, are used in the format string. 9052fe8fb19SBen GrasThis allows for the format string to be parsed for this information. 9062fe8fb19SBen GrasFailure to do this will mean your code is non-portable and liable to fail. 9072fe8fb19SBen Gras.Pp 9082fe8fb19SBen GrasIn this implementation, passing a 9092fe8fb19SBen Gras.Dv NULL 9102fe8fb19SBen Gras.Vt char * 9112fe8fb19SBen Grasargument to the 9122fe8fb19SBen Gras.Cm %s 9132fe8fb19SBen Grasformat specifier will output 9142fe8fb19SBen Gras.Em "(null)" 9152fe8fb19SBen Grasinstead of crashing. 9162fe8fb19SBen GrasPrograms that depend on this behavior are non-portable and may crash 9172fe8fb19SBen Grason other systems or in the future. 9182fe8fb19SBen Gras.Sh BUGS 9192fe8fb19SBen GrasThe conversion formats 9202fe8fb19SBen Gras.Cm \&%D , 9212fe8fb19SBen Gras.Cm \&%O , 9222fe8fb19SBen Grasand 9232fe8fb19SBen Gras.Cm \&%U 9242fe8fb19SBen Grasare not standard and are provided only for backward compatibility. 9252fe8fb19SBen GrasThe effect of padding the 9262fe8fb19SBen Gras.Cm %p 9272fe8fb19SBen Grasformat with zeros (either by the 9282fe8fb19SBen Gras.Sq Cm 0 9292fe8fb19SBen Grasflag or by specifying a precision), and the benign effect (i.e. none) 9302fe8fb19SBen Grasof the 9312fe8fb19SBen Gras.Sq Cm # 9322fe8fb19SBen Grasflag on 9332fe8fb19SBen Gras.Cm %n 9342fe8fb19SBen Grasand 9352fe8fb19SBen Gras.Cm %p 9362fe8fb19SBen Grasconversions, as well as other nonsensical combinations such as 9372fe8fb19SBen Gras.Cm %Ld , 9382fe8fb19SBen Grasare not standard; such combinations should be avoided. 9392fe8fb19SBen Gras.Pp 9402fe8fb19SBen GrasThe 9412fe8fb19SBen Gras.Nm 9422fe8fb19SBen Grasfamily of functions do not correctly handle multibyte characters in the 9432fe8fb19SBen Gras.Fa format 9442fe8fb19SBen Grasargument. 9452fe8fb19SBen Gras.Sh SECURITY CONSIDERATIONS 9462fe8fb19SBen GrasThe 9472fe8fb19SBen Gras.Fn sprintf 9482fe8fb19SBen Grasand 9492fe8fb19SBen Gras.Fn vsprintf 9502fe8fb19SBen Grasfunctions are easily misused in a manner which enables malicious users 9512fe8fb19SBen Grasto arbitrarily change a running program's functionality through 9522fe8fb19SBen Grasa buffer overflow attack. 9532fe8fb19SBen GrasBecause 9542fe8fb19SBen Gras.Fn sprintf 9552fe8fb19SBen Grasand 9562fe8fb19SBen Gras.Fn vsprintf 9572fe8fb19SBen Grasassume an infinitely long string, 9582fe8fb19SBen Grascallers must be careful not to overflow the actual space; 9592fe8fb19SBen Grasthis is often hard to assure. 9602fe8fb19SBen GrasFor safety, programmers should use the 9612fe8fb19SBen Gras.Fn snprintf 9622fe8fb19SBen Grasinterface instead. 9632fe8fb19SBen GrasFor example: 9642fe8fb19SBen Gras.Bd -literal 9652fe8fb19SBen Grasvoid 9662fe8fb19SBen Grasfoo(const char *arbitrary_string, const char *and_another) 9672fe8fb19SBen Gras{ 9682fe8fb19SBen Gras char onstack[8]; 9692fe8fb19SBen Gras 9702fe8fb19SBen Gras#ifdef BAD 9712fe8fb19SBen Gras /* 9722fe8fb19SBen Gras * This first sprintf is bad behavior. Do not use sprintf! 9732fe8fb19SBen Gras */ 9742fe8fb19SBen Gras sprintf(onstack, "%s, %s", arbitrary_string, and_another); 9752fe8fb19SBen Gras#else 9762fe8fb19SBen Gras /* 9772fe8fb19SBen Gras * The following two lines demonstrate better use of 9782fe8fb19SBen Gras * snprintf(). 9792fe8fb19SBen Gras */ 9802fe8fb19SBen Gras snprintf(onstack, sizeof(onstack), "%s, %s", arbitrary_string, 9812fe8fb19SBen Gras and_another); 9822fe8fb19SBen Gras#endif 9832fe8fb19SBen Gras} 9842fe8fb19SBen Gras.Ed 9852fe8fb19SBen Gras.Pp 9862fe8fb19SBen GrasThe 9872fe8fb19SBen Gras.Fn printf 9882fe8fb19SBen Grasand 9892fe8fb19SBen Gras.Fn sprintf 9902fe8fb19SBen Grasfamily of functions are also easily misused in a manner 9912fe8fb19SBen Grasallowing malicious users to arbitrarily change a running program's 9922fe8fb19SBen Grasfunctionality by either causing the program 9932fe8fb19SBen Grasto print potentially sensitive data 9942fe8fb19SBen Gras.Dq "left on the stack" , 9952fe8fb19SBen Grasor causing it to generate a memory fault or bus error 9962fe8fb19SBen Grasby dereferencing an invalid pointer. 9972fe8fb19SBen Gras.Pp 9982fe8fb19SBen Gras.Cm %n 9992fe8fb19SBen Grascan be used to write arbitrary data to potentially carefully-selected 10002fe8fb19SBen Grasaddresses. 10012fe8fb19SBen GrasProgrammers are therefore strongly advised to never pass untrusted strings 10022fe8fb19SBen Grasas the 10032fe8fb19SBen Gras.Fa format 10042fe8fb19SBen Grasargument, as an attacker can put format specifiers in the string 10052fe8fb19SBen Grasto mangle your stack, 10062fe8fb19SBen Grasleading to a possible security hole. 10072fe8fb19SBen GrasThis holds true even if the string was built using a function like 10082fe8fb19SBen Gras.Fn snprintf , 10092fe8fb19SBen Grasas the resulting string may still contain user-supplied conversion specifiers 10102fe8fb19SBen Grasfor later interpolation by 10112fe8fb19SBen Gras.Fn printf . 10122fe8fb19SBen Gras.Pp 10132fe8fb19SBen GrasAlways use the proper secure idiom: 10142fe8fb19SBen Gras.Pp 10152fe8fb19SBen Gras.Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);" 1016