1 .\" Copyright (c) 1990, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
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.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)printf.3 8.1 (Berkeley) 6/4/93
37 .\" $FreeBSD: src/lib/libc/stdio/printf.3,v 1.58 2004/10/16 16:00:01 stefanf Exp $
53 .Nd formatted output conversion
61 .Fa "const char *format" ...
65 .Fa "FILE *restrict stream"
66 .Fa "const char *restrict format" ...
70 .Fa "const char *restrict format" ...
74 .Fa "char *restrict s"
76 .Fa "const char *restrict format" ...
80 .Fa "char *restrict s"
81 .Fa "const char *restrict format" ...
88 .Fa "const char *format"
93 .Fa "FILE *restrict stream"
94 .Fa "const char *restrict format"
99 .Fa "const char *restrict format"
104 .Fa "char *restrict s"
106 .Fa "const char *restrict format"
111 .Fa "char *restrict s"
112 .Fa "const char *restrict format"
118 family of functions produces output according to a
128 the standard output stream;
132 write output to the given output
139 write to the character string
145 dynamically allocate a new string with
148 Extended locale versions of these functions are documented in
152 for more information.
154 These functions write the output under the control of a
156 string that specifies how subsequent arguments
157 (or arguments accessed via the variable-length argument facilities of
159 are converted for output.
161 These functions return the number of characters printed
162 (not including the trailing
164 used to end output to strings) or a negative value if an output error occurs,
169 which return the number of characters that would have been printed if the
172 (again, not including the final
182 to be a pointer to a buffer sufficiently large to hold the formatted string.
183 This pointer should be passed to
185 to release the allocated storage when it is no longer needed.
186 If sufficient space cannot be allocated,
190 will return \-1 and set
203 of the characters printed into the output string
206 character then gets the terminating
208 if the return value is greater than or equal to the
210 argument, the string was too short
211 and some of the printed characters were discarded.
212 The output is always null-terminated.
219 effectively assume an infinite
222 The format string is composed of zero or more directives:
227 which are copied unchanged to the output stream;
228 and conversion specifications, each of which results
229 in fetching zero or more subsequent arguments.
230 Each conversion specification is introduced by
234 The arguments must correspond properly (after type promotion)
235 with the conversion specifier.
238 the following appear in sequence:
241 An optional field, consisting of a decimal digit string followed by a
243 specifying the next argument to access.
244 If this field is not provided, the argument following the last
245 argument accessed will be used.
246 Arguments are numbered starting at
248 If unaccessed arguments in the format string are interspersed with ones that
249 are accessed the results will be indeterminate.
251 Zero or more of the following flags:
252 .Bl -tag -width ".So \ Sc (space)"
254 The value should be converted to an
257 .Cm c , d , i , n , p , s ,
260 conversions, this option has no effect.
263 conversions, the precision of the number is increased to force the first
264 character of the output string to a zero (except if a zero value is printed
265 with an explicit precision of zero).
270 conversions, a non-zero result has the string
276 conversions) prepended to it.
278 .Cm a , A , e , E , f , F , g ,
281 conversions, the result will always contain a decimal point, even if no
282 digits follow it (normally, a decimal point appears in the results of
283 those conversions only if a digit follows).
288 conversions, trailing zeros are not removed from the result as they
290 .It So Cm 0 Sc (zero)
292 For all conversions except
294 the converted value is padded on the left with zeros rather than blanks.
295 If a precision is given with a numeric conversion
296 .Cm ( d , i , o , u , i , x ,
303 A negative field width flag;
304 the converted value is to be left adjusted on the field boundary.
307 conversions, the converted value is padded on the right with blanks,
308 rather than on the left with blanks or zeros.
314 .It So "\ " Sc (space)
315 A blank should be left before a positive number
316 produced by a signed conversion
317 .Cm ( a , A , d , e , E , f , F , g , G ,
321 A sign must always be placed before a
322 number produced by a signed conversion.
325 overrides a space if both are used.
331 or the integral portion of a floating point conversion
335 should be grouped and separated by thousands using
336 the non-monetary separator returned by
340 An optional separator character (
341 .Cm \ , | \; | \ : | _
342 ) used for separating multiple values when printing an AltiVec or SSE vector,
343 or other multi-value unit.
345 NOTE: This is an extension to the
348 Behaviour of these values for
350 is only defined for operating systems conforming to the
351 AltiVec Technology Programming Interface Manual.
352 (At time of writing this includes only Mac OS X 10.2 and later.)
354 An optional decimal digit string specifying a minimum field width.
355 If the converted value has fewer characters than the field width, it will
356 be padded with spaces on the left (or right, if the left-adjustment
357 flag has been given) to fill out
360 An optional precision, in the form of a period
363 optional digit string.
364 If the digit string is omitted, the precision is taken as zero.
365 This gives the minimum number of digits to appear for
366 .Cm d , i , o , u , x ,
369 conversions, the number of digits to appear after the decimal-point for
370 .Cm a , A , e , E , f ,
373 conversions, the maximum number of significant digits for
377 conversions, or the maximum number of characters to be printed from a
382 An optional length modifier, that specifies the size of the argument.
383 The following length modifiers are valid for the
384 .Cm d , i , n , o , u , x ,
388 .Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *"
389 .It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n
390 .It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *"
391 .It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *"
392 .It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *"
393 .It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *"
394 .It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *"
395 .It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *"
396 .It Cm z Ta (see note) Ta Vt size_t Ta (see note)
397 .It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *"
403 modifier, when applied to a
407 conversion, indicates that the argument is of an unsigned type
408 equivalent in size to a
412 modifier, when applied to a
416 conversion, indicates that the argument is of a signed type equivalent in
419 Similarly, when applied to an
421 conversion, it indicates that the argument is a pointer to a signed type
422 equivalent in size to a
425 The following length modifier is valid for the
426 .Cm a , A , e , E , f , F , g ,
430 .Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G"
431 .It Sy Modifier Ta Cm a , A , e , E , f , F , g , G
432 .It Cm l No (ell) Ta Vt double
433 (ignored, same behavior as without it)
434 .It Cm L Ta Vt "long double"
437 The following length modifier is valid for the
442 .Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *"
443 .It Sy Modifier Ta Cm c Ta Cm s
444 .It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *"
447 The AltiVec Technology Programming Interface Manual also defines five additional length modifiers
448 which can be used (in place of the conventional length modifiers) for the printing of AltiVec or SSE vectors:
451 Treat the argument as a vector value, unit length will be determined by the conversion
452 specifier (default = 16 8-bit units for all integer conversions,
453 4 32-bit units for floating point conversions).
455 Treat the argument as a vector of 8 16-bit units.
457 Treat the argument as a vector of 4 32-bit units.
460 NOTE: The vector length specifiers are extensions to the
463 Behaviour of these values for
465 is only defined for operating systems conforming to the
466 AltiVec Technology Programming Interface Manual.
467 (At time of writing this includes only Mac OS X 10.2 and later.)
469 As a further extension, for SSE2 64-bit units:
472 Treat the argument as a vector of 2 64-bit units.
475 A character that specifies the type of conversion to be applied.
478 A field width or precision, or both, may be indicated by
481 or an asterisk followed by one or more decimal digits and a
487 argument supplies the field width or precision.
488 A negative field width is treated as a left adjustment flag followed by a
489 positive field width; a negative precision is treated as though it were
491 If a single format directive mixes positional
493 and non-positional arguments, the results are undefined.
495 The conversion specifiers and their meanings are:
496 .Bl -tag -width ".Cm diouxX"
500 (or appropriate variant) argument is converted to signed decimal
508 or unsigned hexadecimal
517 conversions; the letters
522 The precision, if any, gives the minimum number of digits that must
523 appear; if the converted value requires fewer digits, it is padded on
528 argument is converted to signed decimal, unsigned octal, or unsigned
529 decimal, as if the format had been
534 These conversion characters are deprecated, and will eventually disappear.
538 argument is rounded and converted in the style
540 .Oo \- Oc Ar d Li \&. Ar ddd Li e \\*[Pm] Ar dd
542 where there is one digit before the
543 decimal-point character
544 and the number of digits after it is equal to the precision;
545 if the precision is missing,
546 it is taken as 6; if the precision is
547 zero, no decimal-point character appears.
550 conversion uses the letter
554 to introduce the exponent.
555 The exponent always contains at least two digits; if the value is zero,
559 .Cm a , A , e , E , f , F , g ,
562 conversions, positive and negative infinity are represented as
566 respectively when using the lowercase conversion character, and
570 respectively when using the uppercase conversion character.
571 Similarly, NaN is represented as
573 when using the lowercase conversion, and
575 when using the uppercase conversion.
579 argument is rounded and converted to decimal notation in the style
581 .Oo \- Oc Ar ddd Li \&. Ar ddd ,
583 where the number of digits after the decimal-point character
584 is equal to the precision specification.
585 If the precision is missing, it is taken as 6; if the precision is
586 explicitly zero, no decimal-point character appears.
587 If a decimal point appears, at least one digit appears before it.
591 argument is converted in style
602 The precision specifies the number of significant digits.
603 If the precision is missing, 6 digits are given; if the precision is zero,
607 is used if the exponent from its conversion is less than \-4 or greater than
608 or equal to the precision.
609 Trailing zeros are removed from the fractional part of the result; a
610 decimal point appears only if it is followed by at least one digit.
614 argument is rounded and converted to hexadecimal notation in the style
616 .Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \\*[Pm] Oc Ar d ,
618 where the number of digits after the hexadecimal-point character
619 is equal to the precision specification.
620 If the precision is missing, it is taken as enough to represent
621 the floating-point number exactly, and no rounding occurs.
622 If the precision is zero, no hexadecimal-point character appears.
625 is a literal character
627 and the exponent consists of a positive or negative sign
628 followed by a decimal number representing an exponent of 2.
631 conversion uses the prefix
639 to represent the hex digits, and the letter
643 to separate the mantissa and exponent.
645 Note that there may be multiple valid ways to represent floating-point
646 numbers in this hexadecimal format.
648 .Li 0x3.24p+0 , 0x6.48p-1
652 The format chosen depends on the internal representation of the
653 number, but the implementation guarantees that the length of the
654 mantissa will be minimized.
655 Zeroes are always represented with a mantissa of 0 (preceded by a
657 if appropriate) and an exponent of
668 argument is converted to an
669 .Vt "unsigned char" ,
670 and the resulting character is written.
674 (ell) modifier is used, the
676 argument shall be converted to a
678 and the (potentially multi-byte) sequence representing the
679 single wide character is written, including any shift sequences.
680 If a shift sequence is used, the shift state is also restored
681 to the original state after the character.
691 argument is expected to be a pointer to an array of character type (pointer
693 Characters from the array are written up to (but not including)
697 if a precision is specified, no more than the number specified are
699 If a precision is given, no null character
700 need be present; if the precision is not specified, or is greater than
701 the size of the array, the array must contain a terminating
707 (ell) modifier is used, the
709 argument is expected to be a pointer to an array of wide characters
710 (pointer to a wide string).
711 For each wide character in the string, the (potentially multi-byte)
712 sequence representing the
713 wide character is written, including any shift sequences.
714 If any shift sequence is used, the shift state is also restored
715 to the original state after the string.
716 Wide characters from the array are written up to (but not including)
720 if a precision is specified, no more than the number of bytes specified are
721 written (including shift sequences).
722 Partial characters are never written.
723 If a precision is given, no null character
724 need be present; if the precision is not specified, or is greater than
725 the number of bytes required to render the multibyte representation of
726 the string, the array must contain a terminating wide
732 pointer argument is printed in hexadecimal (as if by
737 The number of characters written so far is stored into the
738 integer indicated by the
740 (or variant) pointer argument.
741 No argument is converted.
746 No argument is converted.
747 The complete conversion specification
753 character is defined in the program's locale (category
756 In no case does a non-existent or small field width cause truncation of
757 a numeric field; if the result of a conversion is wider than the field
759 field is expanded to contain the conversion result.
761 To print a date and time in the form
762 .Dq Li "Sunday, July 3, 10:02" ,
767 are pointers to strings:
768 .Bd -literal -offset indent
770 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
771 weekday, month, day, hour, min);
775 to five decimal places:
776 .Bd -literal -offset indent
779 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
782 To allocate a 128 byte string and print into it:
783 .Bd -literal -offset indent
787 char *newfmt(const char *fmt, ...)
791 if ((p = malloc(128)) == NULL)
794 (void) vsnprintf(p, 128, fmt, ap);
799 .Sh SECURITY CONSIDERATIONS
804 functions are easily misused in a manner which enables malicious users
805 to arbitrarily change a running program's functionality through
806 a buffer overflow attack.
811 assume an infinitely long string,
812 callers must be careful not to overflow the actual space;
813 this is often hard to assure.
814 For safety, programmers should use the
820 foo(const char *arbitrary_string, const char *and_another)
826 * This first sprintf is bad behavior. Do not use sprintf!
828 sprintf(onstack, "%s, %s", arbitrary_string, and_another);
831 * The following two lines demonstrate better use of
834 snprintf(onstack, sizeof(onstack), "%s, %s", arbitrary_string,
844 family of functions are also easily misused in a manner
845 allowing malicious users to arbitrarily change a running program's
846 functionality by either causing the program
847 to print potentially sensitive data
848 .Dq "left on the stack" ,
849 or causing it to generate a memory fault or bus error
850 by dereferencing an invalid pointer.
853 can be used to write arbitrary data to potentially carefully-selected
855 Programmers are therefore strongly advised to never pass untrusted strings
858 argument, as an attacker can put format specifiers in the string
859 to mangle your stack,
860 leading to a possible security hole.
861 This holds true even if the string was built using a function like
863 as the resulting string may still contain user-supplied conversion specifiers
864 for later interpolation by
867 Always use the proper secure idiom:
869 .Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);"
871 In addition to the errors documented for the
875 family of functions may fail if:
878 An invalid wide character code was encountered.
880 Insufficient storage space is available.
891 Subject to the caveats noted in the
906 With the same reservation, the
917 first appeared in the
920 These were implemented by
921 .An Peter Wemm Aq peter@FreeBSD.org
924 but were later replaced with a different implementation
926 .An Todd C. Miller Aq Todd.Miller@courtesan.com
930 The conversion formats
935 are provided only for backward compatibility.
936 The effect of padding the
938 format with zeros (either by the
940 flag or by specifying a precision), and the benign effect (i.e., none)
947 conversions, as well as other
948 nonsensical combinations such as
950 are not standard; such combinations
955 family of functions do not correctly handle multibyte characters in the