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