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. 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.
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
32 .\" @(#)printf.3 8.1 (Berkeley) 6/4/93
33 .\" $FreeBSD: head/lib/libc/stdio/printf.3 303524 2016-07-30 01:00:16Z bapt $
51 .Nd formatted output conversion
57 .Fn printf "const char * restrict format" ...
59 .Fn fprintf "FILE * restrict stream" "const char * restrict format" ...
61 .Fn sprintf "char * restrict str" "const char * restrict format" ...
63 .Fn snprintf "char * restrict str" "size_t size" "const char * restrict format" ...
65 .Fn asprintf "char **ret" "const char *format" ...
67 .Fn dprintf "int" "const char * restrict format" ...
70 .Fn vprintf "const char * restrict format" "va_list ap"
72 .Fn vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap"
74 .Fn vsprintf "char * restrict str" "const char * restrict format" "va_list ap"
76 .Fn vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap"
78 .Fn vasprintf "char **ret" "const char *format" "va_list ap"
80 .Fn vdprintf "int fd" "const char * restrict format" "va_list ap"
84 family of functions produces output according to a
94 the standard output stream;
98 write output to the given output
103 write output to the given file descriptor;
109 write to the character string
115 dynamically allocate a new string with
118 These functions write the output under the control of a
120 string that specifies how subsequent arguments
121 (or arguments accessed via the variable-length argument facilities of
123 are converted for output.
132 to be a pointer to a buffer sufficiently large to hold the formatted string.
133 This pointer should be passed to
135 to release the allocated storage when it is no longer needed.
136 If sufficient space cannot be allocated,
140 will return \-1 and set
153 of the characters printed into the output string
156 character then gets the terminating
158 if the return value is greater than or equal to the
160 argument, the string was too short
161 and some of the printed characters were discarded.
162 The output is always null-terminated, unless
177 The format string is composed of zero or more directives:
182 which are copied unchanged to the output stream;
183 and conversion specifications, each of which results
184 in fetching zero or more subsequent arguments.
185 Each conversion specification is introduced by
189 The arguments must correspond properly (after type promotion)
190 with the conversion specifier.
193 the following appear in sequence:
196 An optional field, consisting of a decimal digit string followed by a
198 specifying the next argument to access.
199 If this field is not provided, the argument following the last
200 argument accessed will be used.
201 Arguments are numbered starting at
203 If unaccessed arguments in the format string are interspersed with ones that
204 are accessed the results will be indeterminate.
206 Zero or more of the following flags:
207 .Bl -tag -width ".So \ Sc (space)"
209 The value should be converted to an
212 .Cm c , d , i , n , p , s ,
215 conversions, this option has no effect.
218 conversions, the precision of the number is increased to force the first
219 character of the output string to a zero.
224 conversions, a non-zero result has the string
230 conversions) prepended to it.
232 .Cm a , A , e , E , f , F , g ,
235 conversions, the result will always contain a decimal point, even if no
236 digits follow it (normally, a decimal point appears in the results of
237 those conversions only if a digit follows).
242 conversions, trailing zeros are not removed from the result as they
244 .It So Cm 0 Sc (zero)
246 For all conversions except
248 the converted value is padded on the left with zeros rather than blanks.
249 If a precision is given with a numeric conversion
250 .Cm ( d , i , o , u , i , x ,
257 A negative field width flag;
258 the converted value is to be left adjusted on the field boundary.
261 conversions, the converted value is padded on the right with blanks,
262 rather than on the left with blanks or zeros.
268 .It So "\ " Sc (space)
269 A blank should be left before a positive number
270 produced by a signed conversion
271 .Cm ( a , A , d , e , E , f , F , g , G ,
275 A sign must always be placed before a
276 number produced by a signed conversion.
279 overrides a space if both are used.
280 .It So "'" Sc (apostrophe)
285 or the integral portion of a floating point conversion
289 should be grouped and separated by thousands using
290 the non-monetary separator returned by
294 An optional decimal digit string specifying a minimum field width.
295 If the converted value has fewer characters than the field width, it will
296 be padded with spaces on the left (or right, if the left-adjustment
297 flag has been given) to fill out
300 An optional precision, in the form of a period
303 optional digit string.
304 If the digit string is omitted, the precision is taken as zero.
305 This gives the minimum number of digits to appear for
306 .Cm d , i , o , u , x ,
309 conversions, the number of digits to appear after the decimal-point for
310 .Cm a , A , e , E , f ,
313 conversions, the maximum number of significant digits for
317 conversions, or the maximum number of characters to be printed from a
322 An optional length modifier, that specifies the size of the argument.
323 The following length modifiers are valid for the
324 .Cm d , i , n , o , u , x ,
328 .Bl -column ".Cm q Em (non-standard)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *"
329 .It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n
330 .It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *"
331 .It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *"
332 .It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *"
333 .It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *"
334 .It Cm L Em (non-standard) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *"
335 .It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *"
336 .It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *"
337 .It Cm z Ta (see note) Ta Vt size_t Ta (see note)
338 .It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *"
344 modifier, when applied to a
348 conversion, indicates that the argument is of an unsigned type
349 equivalent in size to a
353 modifier, when applied to a
357 conversion, indicates that the argument is of a signed type equivalent in
360 Similarly, when applied to an
362 conversion, it indicates that the argument is a pointer to a signed type
363 equivalent in size to a
366 The following length modifier is valid for the
367 .Cm a , A , e , E , f , F , g ,
371 .Bl -column ".Cm ll Em (non-standard)" ".Cm a , A , e , E , f , F , g , G"
372 .It Sy Modifier Ta Cm a , A , e , E , f , F , g , G
373 .It Cm l No (ell) Ta Vt double
374 (ignored, same behavior as without it)
375 .It Cm L Ta Vt "long double"
376 .It Cm ll Em (non-standard) Ta Vt "long double"
379 The following length modifier is valid for the
384 .Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *"
385 .It Sy Modifier Ta Cm c Ta Cm s
386 .It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *"
389 A character that specifies the type of conversion to be applied.
392 A field width or precision, or both, may be indicated by
395 or an asterisk followed by one or more decimal digits and a
401 argument supplies the field width or precision.
402 A negative field width is treated as a left adjustment flag followed by a
403 positive field width; a negative precision is treated as though it were
405 If a single format directive mixes positional
407 and non-positional arguments, the results are undefined.
409 The conversion specifiers and their meanings are:
410 .Bl -tag -width ".Cm diouxX"
414 (or appropriate variant) argument is converted to signed decimal
422 or unsigned hexadecimal
431 conversions; the letters
436 The precision, if any, gives the minimum number of digits that must
437 appear; if the converted value requires fewer digits, it is padded on
442 argument is converted to signed decimal, unsigned octal, or unsigned
443 decimal, as if the format had been
448 These conversion characters are deprecated, and will eventually disappear.
452 argument is rounded and converted in the style
454 .Oo \- Oc Ar d Li \&. Ar ddd Li e \(+- Ar dd
456 where there is one digit before the
457 decimal-point character
458 and the number of digits after it is equal to the precision;
459 if the precision is missing,
460 it is taken as 6; if the precision is
461 zero, no decimal-point character appears.
464 conversion uses the letter
468 to introduce the exponent.
469 The exponent always contains at least two digits; if the value is zero,
473 .Cm a , A , e , E , f , F , g ,
476 conversions, positive and negative infinity are represented as
480 respectively when using the lowercase conversion character, and
484 respectively when using the uppercase conversion character.
485 Similarly, NaN is represented as
487 when using the lowercase conversion, and
489 when using the uppercase conversion.
493 argument is rounded and converted to decimal notation in the style
495 .Oo \- Oc Ar ddd Li \&. Ar ddd ,
497 where the number of digits after the decimal-point character
498 is equal to the precision specification.
499 If the precision is missing, it is taken as 6; if the precision is
500 explicitly zero, no decimal-point character appears.
501 If a decimal point appears, at least one digit appears before it.
505 argument is converted in style
516 The precision specifies the number of significant digits.
517 If the precision is missing, 6 digits are given; if the precision is zero,
521 is used if the exponent from its conversion is less than \-4 or greater than
522 or equal to the precision.
523 Trailing zeros are removed from the fractional part of the result; a
524 decimal point appears only if it is followed by at least one digit.
528 argument is rounded and converted to hexadecimal notation in the style
530 .Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \(+- Oc Ar d ,
532 where the number of digits after the hexadecimal-point character
533 is equal to the precision specification.
534 If the precision is missing, it is taken as enough to represent
535 the floating-point number exactly, and no rounding occurs.
536 If the precision is zero, no hexadecimal-point character appears.
539 is a literal character
541 and the exponent consists of a positive or negative sign
542 followed by a decimal number representing an exponent of 2.
545 conversion uses the prefix
553 to represent the hex digits, and the letter
557 to separate the mantissa and exponent.
559 Note that there may be multiple valid ways to represent floating-point
560 numbers in this hexadecimal format.
562 .Li 0x1.92p+1 , 0x3.24p+0 , 0x6.48p-1 ,
567 and later always prints finite non-zero numbers using
569 as the digit before the hexadecimal point.
570 Zeroes are always represented with a mantissa of 0 (preceded by a
572 if appropriate) and an exponent of
583 argument is converted to an
584 .Vt "unsigned char" ,
585 and the resulting character is written.
589 (ell) modifier is used, the
591 argument shall be converted to a
593 and the (potentially multi-byte) sequence representing the
594 single wide character is written, including any shift sequences.
595 If a shift sequence is used, the shift state is also restored
596 to the original state after the character.
606 argument is expected to be a pointer to an array of character type (pointer
608 Characters from the array are written up to (but not including)
612 if a precision is specified, no more than the number specified are
614 If a precision is given, no null character
615 need be present; if the precision is not specified, or is greater than
616 the size of the array, the array must contain a terminating
622 (ell) modifier is used, the
624 argument is expected to be a pointer to an array of wide characters
625 (pointer to a wide string).
626 For each wide character in the string, the (potentially multi-byte)
627 sequence representing the
628 wide character is written, including any shift sequences.
629 If any shift sequence is used, the shift state is also restored
630 to the original state after the string.
631 Wide characters from the array are written up to (but not including)
635 if a precision is specified, no more than the number of bytes specified are
636 written (including shift sequences).
637 Partial characters are never written.
638 If a precision is given, no null character
639 need be present; if the precision is not specified, or is greater than
640 the number of bytes required to render the multibyte representation of
641 the string, the array must contain a terminating wide
647 pointer argument is printed in hexadecimal (as if by
652 The number of characters written so far is stored into the
653 integer indicated by the
655 (or variant) pointer argument.
656 No argument is converted.
661 No argument is converted.
662 The complete conversion specification
668 character is defined in the program's locale (category
671 In no case does a non-existent or small field width cause truncation of
672 a numeric field; if the result of a conversion is wider than the field
674 field is expanded to contain the conversion result.
676 These functions return the number of characters printed
677 (not including the trailing
679 used to end output to strings),
684 which return the number of characters that would have been printed if the
687 (again, not including the final
689 These functions return a negative value if an error occurs.
691 To print a date and time in the form
692 .Dq Li "Sunday, July 3, 10:02" ,
697 are pointers to strings:
698 .Bd -literal -offset indent
700 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
701 weekday, month, day, hour, min);
705 to five decimal places:
706 .Bd -literal -offset indent
709 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
712 To allocate a 128 byte string and print into it:
713 .Bd -literal -offset indent
717 char *newfmt(const char *fmt, ...)
721 if ((p = malloc(128)) == NULL)
724 (void) vsnprintf(p, 128, fmt, ap);
730 The conversion formats
735 are provided only for backward compatibility.
736 The effect of padding the
738 format with zeros (either by the
740 flag or by specifying a precision), and the benign effect (i.e., none)
747 conversions, as well as other
748 nonsensical combinations such as
750 are not standard; such combinations
753 In addition to the errors documented for the
757 family of functions may fail if:
760 An invalid wide character code was encountered.
762 Insufficient storage space is available.
768 or the return value would be too large to be represented by an
779 Subject to the caveats noted in the
794 With the same reservation, the
809 treats the length modifiers
813 as synonyms, so that the non-standard
827 first appeared in the
830 These were implemented by
831 .An Peter Wemm Aq Mt peter@FreeBSD.org
834 but were later replaced with a different implementation
838 .An Todd C. Miller Aq Mt Todd.Miller@courtesan.com .
843 functions were added in
848 family of functions do not correctly handle multibyte characters in the
851 .Sh SECURITY CONSIDERATIONS
856 functions are easily misused in a manner which enables malicious users
857 to arbitrarily change a running program's functionality through
858 a buffer overflow attack.
863 assume an infinitely long string,
864 callers must be careful not to overflow the actual space;
865 this is often hard to assure.
866 For safety, programmers should use the
872 foo(const char *arbitrary_string, const char *and_another)
878 * This first sprintf is bad behavior. Do not use sprintf!
880 sprintf(onstack, "%s, %s", arbitrary_string, and_another);
883 * The following two lines demonstrate better use of
886 snprintf(onstack, sizeof(onstack), "%s, %s", arbitrary_string,
896 family of functions are also easily misused in a manner
897 allowing malicious users to arbitrarily change a running program's
898 functionality by either causing the program
899 to print potentially sensitive data
900 .Dq "left on the stack" ,
901 or causing it to generate a memory fault or bus error
902 by dereferencing an invalid pointer.
905 can be used to write arbitrary data to potentially carefully-selected
907 Programmers are therefore strongly advised to never pass untrusted strings
910 argument, as an attacker can put format specifiers in the string
911 to mangle your stack,
912 leading to a possible security hole.
913 This holds true even if the string was built using a function like
915 as the resulting string may still contain user-supplied conversion specifiers
916 for later interpolation by
919 Always use the proper secure idiom:
921 .Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);"