xref: /freebsd/lib/libc/stdio/printf.3 (revision abcdc1b9)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)printf.3	8.1 (Berkeley) 6/4/93
33.\"
34.Dd September 5, 2023
35.Dt PRINTF 3
36.Os
37.Sh NAME
38.Nm printf ,
39.Nm fprintf ,
40.Nm sprintf ,
41.Nm snprintf ,
42.Nm asprintf ,
43.Nm dprintf ,
44.Nm vprintf ,
45.Nm vfprintf ,
46.Nm vsprintf ,
47.Nm vsnprintf ,
48.Nm vasprintf ,
49.Nm vdprintf
50.Nd formatted output conversion
51.Sh LIBRARY
52.Lb libc
53.Sh SYNOPSIS
54.In stdio.h
55.Ft int
56.Fn printf "const char * restrict format" ...
57.Ft int
58.Fn fprintf "FILE * restrict stream" "const char * restrict format" ...
59.Ft int
60.Fn sprintf "char * restrict str" "const char * restrict format" ...
61.Ft int
62.Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ...
63.Ft int
64.Fn asprintf "char **ret" "const char *format" ...
65.Ft int
66.Fn dprintf "int fd" "const char * restrict format" ...
67.In stdarg.h
68.Ft int
69.Fn vprintf "const char * restrict format" "va_list ap"
70.Ft int
71.Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap"
72.Ft int
73.Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap"
74.Ft int
75.Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap"
76.Ft int
77.Fn vasprintf "char **ret" "const char *format" "va_list ap"
78.Ft int
79.Fn vdprintf "int fd" "const char * restrict format" "va_list ap"
80.Sh DESCRIPTION
81The
82.Fn printf
83family of functions produces output according to a
84.Fa format
85as described below.
86The
87.Fn printf
88and
89.Fn vprintf
90functions
91write output to
92.Dv stdout ,
93the standard output stream;
94.Fn fprintf
95and
96.Fn vfprintf
97write output to the given output
98.Fa stream ;
99.Fn dprintf
100and
101.Fn vdprintf
102write output to the given file descriptor;
103.Fn sprintf ,
104.Fn snprintf ,
105.Fn vsprintf ,
106and
107.Fn vsnprintf
108write to the character string
109.Fa str ;
110and
111.Fn asprintf
112and
113.Fn vasprintf
114dynamically allocate a new string with
115.Xr malloc 3 .
116.Pp
117These functions write the output under the control of a
118.Fa format
119string that specifies how subsequent arguments
120(or arguments accessed via the variable-length argument facilities of
121.Xr stdarg 3 )
122are converted for output.
123.Pp
124The
125.Fn asprintf
126and
127.Fn vasprintf
128functions
129set
130.Fa *ret
131to be a pointer to a buffer sufficiently large to hold the formatted string.
132This pointer should be passed to
133.Xr free 3
134to release the allocated storage when it is no longer needed.
135If sufficient space cannot be allocated,
136.Fn asprintf
137and
138.Fn vasprintf
139will return \-1 and set
140.Fa ret
141to be a
142.Dv NULL
143pointer.
144.Pp
145The
146.Fn snprintf
147and
148.Fn vsnprintf
149functions
150will write at most
151.Fa size Ns \-1
152of the characters printed into the output string
153(the
154.Fa size Ns 'th
155character then gets the terminating
156.Ql \e0 ) ;
157if the return value is greater than or equal to the
158.Fa size
159argument, the string was too short
160and some of the printed characters were discarded.
161The output is always null-terminated, unless
162.Fa size
163is 0.
164.Pp
165The
166.Fn sprintf
167and
168.Fn vsprintf
169functions
170effectively assume a
171.Fa size
172of
173.Dv INT_MAX + 1.
174.Pp
175The format string is composed of zero or more directives:
176ordinary
177.\" multibyte
178characters (not
179.Cm % ) ,
180which are copied unchanged to the output stream;
181and conversion specifications, each of which results
182in fetching zero or more subsequent arguments.
183Each conversion specification is introduced by
184the
185.Cm %
186character.
187The arguments must correspond properly (after type promotion)
188with the conversion specifier.
189After the
190.Cm % ,
191the following appear in sequence:
192.Bl -bullet
193.It
194An optional field, consisting of a decimal digit string followed by a
195.Cm $ ,
196specifying the next argument to access.
197If this field is not provided, the argument following the last
198argument accessed will be used.
199Arguments are numbered starting at
200.Cm 1 .
201If unaccessed arguments in the format string are interspersed with ones that
202are accessed the results will be indeterminate.
203.It
204Zero or more of the following flags:
205.Bl -tag -width ".So \  Sc (space)"
206.It Sq Cm #
207The value should be converted to an
208.Dq alternate form .
209For
210.Cm c , d , i , n , p , s ,
211and
212.Cm u
213conversions, this option has no effect.
214For
215.Cm b
216and
217.Cm B
218conversions, a non-zero result has the string
219.Ql 0b
220(or
221.Ql 0B
222for
223.Cm B
224conversions) prepended to it.
225For
226.Cm o
227conversions, the precision of the number is increased to force the first
228character of the output string to a zero.
229For
230.Cm x
231and
232.Cm X
233conversions, a non-zero result has the string
234.Ql 0x
235(or
236.Ql 0X
237for
238.Cm X
239conversions) prepended to it.
240For
241.Cm a , A , e , E , f , F , g ,
242and
243.Cm G
244conversions, the result will always contain a decimal point, even if no
245digits follow it (normally, a decimal point appears in the results of
246those conversions only if a digit follows).
247For
248.Cm g
249and
250.Cm G
251conversions, trailing zeros are not removed from the result as they
252would otherwise be.
253.It So Cm 0 Sc (zero)
254Zero padding.
255For all conversions except
256.Cm n ,
257the converted value is padded on the left with zeros rather than blanks.
258If a precision is given with a numeric conversion
259.Cm ( b , B , d , i , o , u , i , x ,
260and
261.Cm X ) ,
262the
263.Cm 0
264flag is ignored.
265.It Sq Cm \-
266A negative field width flag;
267the converted value is to be left adjusted on the field boundary.
268Except for
269.Cm n
270conversions, the converted value is padded on the right with blanks,
271rather than on the left with blanks or zeros.
272A
273.Cm \-
274overrides a
275.Cm 0
276if both are given.
277.It So "\ " Sc (space)
278A blank should be left before a positive number
279produced by a signed conversion
280.Cm ( a , A , d , e , E , f , F , g , G ,
281or
282.Cm i ) .
283.It Sq Cm +
284A sign must always be placed before a
285number produced by a signed conversion.
286A
287.Cm +
288overrides a space if both are used.
289.It So "'" Sc (apostrophe)
290Decimal conversions
291.Cm ( d , u ,
292or
293.Cm i )
294or the integral portion of a floating point conversion
295.Cm ( f
296or
297.Cm F )
298should be grouped and separated by thousands using
299the non-monetary separator returned by
300.Xr localeconv 3 .
301.El
302.It
303An optional decimal digit string specifying a minimum field width.
304If the converted value has fewer characters than the field width, it will
305be padded with spaces on the left (or right, if the left-adjustment
306flag has been given) to fill out
307the field width.
308.It
309An optional precision, in the form of a period
310.Cm \&.
311followed by an
312optional digit string.
313If the digit string is omitted, the precision is taken as zero.
314This gives the minimum number of digits to appear for
315.Cm b , B , d , i , o , u , x ,
316and
317.Cm X
318conversions, the number of digits to appear after the decimal-point for
319.Cm a , A , e , E , f ,
320and
321.Cm F
322conversions, the maximum number of significant digits for
323.Cm g
324and
325.Cm G
326conversions, or the maximum number of characters to be printed from a
327string for
328.Cm s
329conversions.
330.It
331An optional length modifier, that specifies the size of the argument.
332The following length modifiers are valid for the
333.Cm b , B , d , i , n , o , u , x ,
334or
335.Cm X
336conversion:
337.Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *"
338.It Sy Modifier Ta Cm d , i Ta Cm b , B , o , u , x , X Ta Cm n
339.It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *"
340.It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *"
341.It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *"
342.It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *"
343.It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *"
344.It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *"
345.It Cm w Ns Ar N Ta Vt intN_t Ta Vt uintN_t Ta Vt "intN_t *"
346.It Cm wf Ns Ar N Ta Vt int_fastN_t Ta Vt uint_fastN_t Ta Vt "int_fastN_t *"
347.It Cm z Ta (see note) Ta Vt size_t Ta (see note)
348.It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *"
349.El
350.Pp
351Note:
352the
353.Cm t
354modifier, when applied to a
355.Cm b , B , o , u , x ,
356or
357.Cm X
358conversion, indicates that the argument is of an unsigned type
359equivalent in size to a
360.Vt ptrdiff_t .
361The
362.Cm z
363modifier, when applied to a
364.Cm d
365or
366.Cm i
367conversion, indicates that the argument is of a signed type equivalent in
368size to a
369.Vt size_t .
370Similarly, when applied to an
371.Cm n
372conversion, it indicates that the argument is a pointer to a signed type
373equivalent in size to a
374.Vt size_t .
375.Pp
376The following length modifier is valid for the
377.Cm a , A , e , E , f , F , g ,
378or
379.Cm G
380conversion:
381.Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G"
382.It Sy Modifier Ta Cm a , A , e , E , f , F , g , G
383.It Cm l No (ell) Ta Vt double
384(ignored, same behavior as without it)
385.It Cm L Ta Vt "long double"
386.El
387.Pp
388The following length modifier is valid for the
389.Cm c
390or
391.Cm s
392conversion:
393.Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *"
394.It Sy Modifier Ta Cm c Ta Cm s
395.It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *"
396.El
397.It
398A character that specifies the type of conversion to be applied.
399.El
400.Pp
401A field width or precision, or both, may be indicated by
402an asterisk
403.Ql *
404or an asterisk followed by one or more decimal digits and a
405.Ql $
406instead of a
407digit string.
408In this case, an
409.Vt int
410argument supplies the field width or precision.
411A negative field width is treated as a left adjustment flag followed by a
412positive field width; a negative precision is treated as though it were
413missing.
414If a single format directive mixes positional
415.Pq Li nn$
416and non-positional arguments, the results are undefined.
417.Pp
418The conversion specifiers and their meanings are:
419.Bl -tag -width ".Cm bBdiouxX"
420.It Cm bBdiouxX
421The
422.Vt int
423(or appropriate variant) argument is converted to
424unsigned binary
425.Cm ( b
426and
427.Cm B ) ,
428signed decimal
429.Cm ( d
430and
431.Cm i ) ,
432unsigned octal
433.Pq Cm o ,
434unsigned decimal
435.Pq Cm u ,
436or unsigned hexadecimal
437.Cm ( x
438and
439.Cm X )
440notation.
441The letters
442.Dq Li abcdef
443are used for
444.Cm x
445conversions; the letters
446.Dq Li ABCDEF
447are used for
448.Cm X
449conversions.
450The precision, if any, gives the minimum number of digits that must
451appear; if the converted value requires fewer digits, it is padded on
452the left with zeros.
453.It Cm DOU
454The
455.Vt "long int"
456argument is converted to signed decimal, unsigned octal, or unsigned
457decimal, as if the format had been
458.Cm ld , lo ,
459or
460.Cm lu
461respectively.
462These conversion characters are deprecated, and will eventually disappear.
463.It Cm eE
464The
465.Vt double
466argument is rounded and converted in the style
467.Sm off
468.Oo \- Oc Ar d Li \&. Ar ddd Li e \(+- Ar dd
469.Sm on
470where there is one digit before the
471decimal-point character
472and the number of digits after it is equal to the precision;
473if the precision is missing,
474it is taken as 6; if the precision is
475zero, no decimal-point character appears.
476An
477.Cm E
478conversion uses the letter
479.Ql E
480(rather than
481.Ql e )
482to introduce the exponent.
483The exponent always contains at least two digits; if the value is zero,
484the exponent is 00.
485.Pp
486For
487.Cm a , A , e , E , f , F , g ,
488and
489.Cm G
490conversions, positive and negative infinity are represented as
491.Li inf
492and
493.Li -inf
494respectively when using the lowercase conversion character, and
495.Li INF
496and
497.Li -INF
498respectively when using the uppercase conversion character.
499Similarly, NaN is represented as
500.Li nan
501when using the lowercase conversion, and
502.Li NAN
503when using the uppercase conversion.
504.It Cm fF
505The
506.Vt double
507argument is rounded and converted to decimal notation in the style
508.Sm off
509.Oo \- Oc Ar ddd Li \&. Ar ddd ,
510.Sm on
511where the number of digits after the decimal-point character
512is equal to the precision specification.
513If the precision is missing, it is taken as 6; if the precision is
514explicitly zero, no decimal-point character appears.
515If a decimal point appears, at least one digit appears before it.
516.It Cm gG
517The
518.Vt double
519argument is converted in style
520.Cm f
521or
522.Cm e
523(or
524.Cm F
525or
526.Cm E
527for
528.Cm G
529conversions).
530The precision specifies the number of significant digits.
531If the precision is missing, 6 digits are given; if the precision is zero,
532it is treated as 1.
533Style
534.Cm e
535is used if the exponent from its conversion is less than \-4 or greater than
536or equal to the precision.
537Trailing zeros are removed from the fractional part of the result; a
538decimal point appears only if it is followed by at least one digit.
539.It Cm aA
540The
541.Vt double
542argument is rounded and converted to hexadecimal notation in the style
543.Sm off
544.Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \(+- Oc Ar d ,
545.Sm on
546where the number of digits after the hexadecimal-point character
547is equal to the precision specification.
548If the precision is missing, it is taken as enough to represent
549the floating-point number exactly, and no rounding occurs.
550If the precision is zero, no hexadecimal-point character appears.
551The
552.Cm p
553is a literal character
554.Ql p ,
555and the exponent consists of a positive or negative sign
556followed by a decimal number representing an exponent of 2.
557The
558.Cm A
559conversion uses the prefix
560.Dq Li 0X
561(rather than
562.Dq Li 0x ) ,
563the letters
564.Dq Li ABCDEF
565(rather than
566.Dq Li abcdef )
567to represent the hex digits, and the letter
568.Ql P
569(rather than
570.Ql p )
571to separate the mantissa and exponent.
572.Pp
573Note that there may be multiple valid ways to represent floating-point
574numbers in this hexadecimal format.
575For example,
576.Li 0x1.92p+1 , 0x3.24p+0 , 0x6.48p-1 ,
577and
578.Li 0xc.9p-2
579are all equivalent.
580.Fx 8.0
581and later always prints finite non-zero numbers using
582.Ql 1
583as the digit before the hexadecimal point.
584Zeroes are always represented with a mantissa of 0 (preceded by a
585.Ql -
586if appropriate) and an exponent of
587.Li +0 .
588.It Cm C
589Treated as
590.Cm c
591with the
592.Cm l
593(ell) modifier.
594.It Cm c
595The
596.Vt int
597argument is converted to an
598.Vt "unsigned char" ,
599and the resulting character is written.
600.Pp
601If the
602.Cm l
603(ell) modifier is used, the
604.Vt wint_t
605argument shall be converted to a
606.Vt wchar_t ,
607and the (potentially multi-byte) sequence representing the
608single wide character is written, including any shift sequences.
609If a shift sequence is used, the shift state is also restored
610to the original state after the character.
611.It Cm S
612Treated as
613.Cm s
614with the
615.Cm l
616(ell) modifier.
617.It Cm s
618The
619.Vt "char *"
620argument is expected to be a pointer to an array of character type (pointer
621to a string).
622Characters from the array are written up to (but not including)
623a terminating
624.Dv NUL
625character;
626if a precision is specified, no more than the number specified are
627written.
628If a precision is given, no null character
629need be present; if the precision is not specified, or is greater than
630the size of the array, the array must contain a terminating
631.Dv NUL
632character.
633.Pp
634If the
635.Cm l
636(ell) modifier is used, the
637.Vt "wchar_t *"
638argument is expected to be a pointer to an array of wide characters
639(pointer to a wide string).
640For each wide character in the string, the (potentially multi-byte)
641sequence representing the
642wide character is written, including any shift sequences.
643If any shift sequence is used, the shift state is also restored
644to the original state after the string.
645Wide characters from the array are written up to (but not including)
646a terminating wide
647.Dv NUL
648character;
649if a precision is specified, no more than the number of bytes specified are
650written (including shift sequences).
651Partial characters are never written.
652If a precision is given, no null character
653need be present; if the precision is not specified, or is greater than
654the number of bytes required to render the multibyte representation of
655the string, the array must contain a terminating wide
656.Dv NUL
657character.
658.It Cm p
659The
660.Vt "void *"
661pointer argument is printed in hexadecimal (as if by
662.Ql %#x
663or
664.Ql %#lx ) .
665.It Cm n
666The number of characters written so far is stored into the
667integer indicated by the
668.Vt "int *"
669(or variant) pointer argument.
670No argument is converted.
671.It Cm m
672Print the string representation of the error code stored in the
673.Dv errno
674variable at the beginning of the call, as returned by
675.Xr strerror 3 .
676No argument is taken.
677.It Cm %
678A
679.Ql %
680is written.
681No argument is converted.
682The complete conversion specification
683is
684.Ql %% .
685.El
686.Pp
687The decimal point
688character is defined in the program's locale (category
689.Dv LC_NUMERIC ) .
690.Pp
691In no case does a non-existent or small field width cause truncation of
692a numeric field; if the result of a conversion is wider than the field
693width, the
694field is expanded to contain the conversion result.
695.Sh RETURN VALUES
696These functions return the number of characters printed
697(not including the trailing
698.Ql \e0
699used to end output to strings),
700except for
701.Fn snprintf
702and
703.Fn vsnprintf ,
704which return the number of characters that would have been printed if the
705.Fa size
706were unlimited
707(again, not including the final
708.Ql \e0 ) .
709These functions return a negative value if an error occurs.
710.Sh EXAMPLES
711To print a date and time in the form
712.Dq Li "Sunday, July 3, 10:02" ,
713where
714.Fa weekday
715and
716.Fa month
717are pointers to strings:
718.Bd -literal -offset indent
719#include <stdio.h>
720fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
721	weekday, month, day, hour, min);
722.Ed
723.Pp
724To print \*(Pi
725to five decimal places:
726.Bd -literal -offset indent
727#include <math.h>
728#include <stdio.h>
729fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
730.Ed
731.Pp
732To allocate a 128 byte string and print into it:
733.Bd -literal -offset indent
734#include <stdio.h>
735#include <stdlib.h>
736#include <stdarg.h>
737char *newfmt(const char *fmt, ...)
738{
739	char *p;
740	va_list ap;
741	if ((p = malloc(128)) == NULL)
742		return (NULL);
743	va_start(ap, fmt);
744	(void) vsnprintf(p, 128, fmt, ap);
745	va_end(ap);
746	return (p);
747}
748.Ed
749.Sh COMPATIBILITY
750The conversion formats
751.Cm \&%D , \&%O ,
752and
753.Cm \&%U
754are not standard and
755are provided only for backward compatibility.
756The conversion format
757.Cm \&%m
758is also not standard and provides the popular extension from the
759.Tn GNU C
760library.
761.Pp
762The effect of padding the
763.Cm %p
764format with zeros (either by the
765.Cm 0
766flag or by specifying a precision), and the benign effect (i.e., none)
767of the
768.Cm #
769flag on
770.Cm %n
771and
772.Cm %p
773conversions, as well as other
774nonsensical combinations such as
775.Cm %Ld ,
776are not standard; such combinations
777should be avoided.
778.Sh ERRORS
779In addition to the errors documented for the
780.Xr write 2
781system call, the
782.Fn printf
783family of functions may fail if:
784.Bl -tag -width Er
785.It Bq Er EILSEQ
786An invalid wide character code was encountered.
787.It Bq Er ENOMEM
788Insufficient storage space is available.
789.It Bq Er EOVERFLOW
790The
791.Fa size
792argument exceeds
793.Dv INT_MAX + 1 ,
794or the return value would be too large to be represented by an
795.Vt int .
796.El
797.Sh SEE ALSO
798.Xr printf 1 ,
799.Xr errno 2 ,
800.Xr fmtcheck 3 ,
801.Xr scanf 3 ,
802.Xr setlocale 3 ,
803.Xr strerror 3 ,
804.Xr wprintf 3
805.Sh STANDARDS
806Subject to the caveats noted in the
807.Sx BUGS
808section below, the
809.Fn fprintf ,
810.Fn printf ,
811.Fn sprintf ,
812.Fn vprintf ,
813.Fn vfprintf ,
814and
815.Fn vsprintf
816functions
817conform to
818.St -ansiC
819and
820.St -isoC-99 .
821With the same reservation, the
822.Fn snprintf
823and
824.Fn vsnprintf
825functions conform to
826.St -isoC-99 ,
827while
828.Fn dprintf
829and
830.Fn vdprintf
831conform to
832.St -p1003.1-2008 .
833.Sh HISTORY
834The functions
835.Fn asprintf
836and
837.Fn vasprintf
838first appeared in the
839.Tn GNU C
840library.
841These were implemented by
842.An Peter Wemm Aq Mt peter@FreeBSD.org
843in
844.Fx 2.2 ,
845but were later replaced with a different implementation
846from
847.Ox 2.3
848by
849.An Todd C. Miller Aq Mt Todd.Miller@courtesan.com .
850The
851.Fn dprintf
852and
853.Fn vdprintf
854functions were added in
855.Fx 8.0 .
856The
857.Cm \&%m
858format extension first appeared in the
859.Tn GNU C
860library, and was implemented in
861.Fx 12.0 .
862.Sh BUGS
863The
864.Nm
865family of functions do not correctly handle multibyte characters in the
866.Fa format
867argument.
868.Sh SECURITY CONSIDERATIONS
869The
870.Fn sprintf
871and
872.Fn vsprintf
873functions are easily misused in a manner which enables malicious users
874to arbitrarily change a running program's functionality through
875a buffer overflow attack.
876Because
877.Fn sprintf
878and
879.Fn vsprintf
880assume an infinitely long string,
881callers must be careful not to overflow the actual space;
882this is often hard to assure.
883For safety, programmers should use the
884.Fn snprintf
885interface instead.
886For example:
887.Bd -literal
888void
889foo(const char *arbitrary_string, const char *and_another)
890{
891	char onstack[8];
892
893#ifdef BAD
894	/*
895	 * This first sprintf is bad behavior.  Do not use sprintf!
896	 */
897	sprintf(onstack, "%s, %s", arbitrary_string, and_another);
898#else
899	/*
900	 * The following two lines demonstrate better use of
901	 * snprintf().
902	 */
903	snprintf(onstack, sizeof(onstack), "%s, %s", arbitrary_string,
904	    and_another);
905#endif
906}
907.Ed
908.Pp
909The
910.Fn printf
911and
912.Fn sprintf
913family of functions are also easily misused in a manner
914allowing malicious users to arbitrarily change a running program's
915functionality by either causing the program
916to print potentially sensitive data
917.Dq "left on the stack" ,
918or causing it to generate a memory fault or bus error
919by dereferencing an invalid pointer.
920.Pp
921.Cm %n
922can be used to write arbitrary data to potentially carefully-selected
923addresses.
924Programmers are therefore strongly advised to never pass untrusted strings
925as the
926.Fa format
927argument, as an attacker can put format specifiers in the string
928to mangle your stack,
929leading to a possible security hole.
930This holds true even if the string was built using a function like
931.Fn snprintf ,
932as the resulting string may still contain user-supplied conversion specifiers
933for later interpolation by
934.Fn printf .
935.Pp
936Always use the proper secure idiom:
937.Pp
938.Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);"
939