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.59 2005/09/05 09:49:33 tjr Exp $
38 .\" $DragonFly: src/lib/libc/stdio/printf.3,v 1.6 2006/08/26 10:27:55 swildner Exp $
54 .Nd formatted output conversion
60 .Fn printf "const char *format" ...
62 .Fn fprintf "FILE *stream" "const char *format" ...
64 .Fn sprintf "char *str" "const char *format" ...
66 .Fn snprintf "char *str" "size_t size" "const char *format" ...
68 .Fn asprintf "char **ret" "const char *format" ...
71 .Fn vprintf "const char *format" "va_list ap"
73 .Fn vfprintf "FILE *stream" "const char *format" "va_list ap"
75 .Fn vsprintf "char *str" "const char *format" "va_list ap"
77 .Fn vsnprintf "char *str" "size_t size" "const char *format" "va_list ap"
79 .Fn vasprintf "char **ret" "const char *format" "va_list ap"
83 family of functions produces output according to a
93 the standard output stream;
97 write output to the given output
104 write to the character string
110 dynamically allocate a new string with
113 These functions write the output under the control of a
115 string that specifies how subsequent arguments
116 (or arguments accessed via the variable-length argument facilities of
118 are converted for output.
120 These functions return the number of characters printed
121 (not including the trailing
123 used to end output to strings) or a negative value if an output error occurs,
128 which return the number of characters that would have been printed if the
131 (again, not including the final
141 to be a pointer to a buffer sufficiently large to hold the formatted string.
142 This pointer should be passed to
144 to release the allocated storage when it is no longer needed.
145 If sufficient space cannot be allocated,
149 will return \-1 and set
162 of the characters printed into the output string
165 character then gets the terminating
167 if the return value is greater than or equal to the
169 argument, the string was too short
170 and some of the printed characters were discarded.
171 The output is always null-terminated.
178 effectively assume an infinite
181 The format string is composed of zero or more directives:
186 which are copied unchanged to the output stream;
187 and conversion specifications, each of which results
188 in fetching zero or more subsequent arguments.
189 Each conversion specification is introduced by
193 The arguments must correspond properly (after type promotion)
194 with the conversion specifier.
197 the following appear in sequence:
200 An optional field, consisting of a decimal digit string followed by a
202 specifying the next argument to access.
203 If this field is not provided, the argument following the last
204 argument accessed will be used.
205 Arguments are numbered starting at
207 If unaccessed arguments in the format string are interspersed with ones that
208 are accessed the results will be indeterminate.
210 Zero or more of the following flags:
211 .Bl -tag -width ".So \ Sc (space)"
213 The value should be converted to an
216 .Cm c , d , i , n , p , s ,
219 conversions, this option has no effect.
222 conversions, the precision of the number is increased to force the first
223 character of the output string to a zero (except if a zero value is printed
224 with an explicit precision of zero).
229 conversions, a non-zero result has the string
235 conversions) prepended to it.
237 .Cm e , E , f , F , g ,
240 conversions, the result will always contain a decimal point, even if no
241 digits follow it (normally, a decimal point appears in the results of
242 those conversions only if a digit follows).
247 conversions, trailing zeros are not removed from the result as they
249 .It So Cm 0 Sc (zero)
251 For all conversions except
253 the converted value is padded on the left with zeros rather than blanks.
254 If a precision is given with a numeric conversion
255 .Cm ( d , i , o , u , i , x ,
262 A negative field width flag;
263 the converted value is to be left adjusted on the field boundary.
266 conversions, the converted value is padded on the right with blanks,
267 rather than on the left with blanks or zeros.
273 .It So "\ " Sc (space)
274 A blank should be left before a positive number
275 produced by a signed conversion
276 .Cm ( d , e , E , f , F , g , G ,
280 A sign must always be placed before a
281 number produced by a signed conversion.
284 overrides a space if both are used.
287 An optional decimal digit string specifying a minimum field width.
288 If the converted value has fewer characters than the field width, it will
289 be padded with spaces on the left (or right, if the left-adjustment
290 flag has been given) to fill out
293 An optional precision, in the form of a period
296 optional digit string.
297 If the digit string is omitted, the precision is taken as zero.
298 This gives the minimum number of digits to appear for
299 .Cm d , i , o , u , x ,
302 conversions, the number of digits to appear after the decimal-point for
306 conversions, the maximum number of significant digits for
310 conversions, or the maximum number of characters to be printed from a
315 An optional length modifier, that specifies the size of the argument.
316 The following length modifiers are valid for the
317 .Cm d , i , n , o , u , x ,
321 .Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *"
322 .It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n
323 .It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *"
324 .It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *"
325 .It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *"
326 .It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *"
327 .It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *"
328 .It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *"
329 .It Cm z Ta (see note) Ta Vt size_t Ta (see note)
330 .It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *"
336 modifier, when applied to a
340 conversion, indicates that the argument is of an unsigned type
341 equivalent in size to a
345 modifier, when applied to a
349 conversion, indicates that the argument is of a signed type equivalent in
352 Similarly, when applied to an
354 conversion, it indicates that the argument is a pointer to a signed type
355 equivalent in size to a
358 The following length modifier is valid for the
359 .Cm e , E , f , F , g ,
363 .Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G"
364 .It Sy Modifier Ta Cm e , E , f , F , g , G
365 .It Cm l No (ell) Ta Vt double
366 (ignored, same behavior as without it)
367 .It Cm L Ta Vt "long double"
370 The following length modifier is valid for the
375 .Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *"
376 .It Sy Modifier Ta Cm c Ta Cm s
377 .It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *"
380 A character that specifies the type of conversion to be applied.
383 A field width or precision, or both, may be indicated by
386 or an asterisk followed by one or more decimal digits and a
392 argument supplies the field width or precision.
393 A negative field width is treated as a left adjustment flag followed by a
394 positive field width; a negative precision is treated as though it were
396 If a single format directive mixes positional
398 and non-positional arguments, the results are undefined.
400 The conversion specifiers and their meanings are:
401 .Bl -tag -width ".Cm diouxX"
405 (or appropriate variant) argument is converted to signed decimal
413 or unsigned hexadecimal
422 conversions; the letters
427 The precision, if any, gives the minimum number of digits that must
428 appear; if the converted value requires fewer digits, it is padded on
433 argument is converted to signed decimal, unsigned octal, or unsigned
434 decimal, as if the format had been
439 These conversion characters are deprecated, and will eventually disappear.
443 argument is rounded and converted in the style
445 .Oo \- Oc Ar d Li \&. Ar ddd Li e \\*[Pm] Ar dd
447 where there is one digit before the
448 decimal-point character
449 and the number of digits after it is equal to the precision;
450 if the precision is missing,
451 it is taken as 6; if the precision is
452 zero, no decimal-point character appears.
455 conversion uses the letter
459 to introduce the exponent.
460 The exponent always contains at least two digits; if the value is zero,
464 .Cm e , E , f , F , g ,
467 conversions, positive and negative infinity are represented as
471 respectively when using the lowercase conversion character, and
475 respectively when using the uppercase conversion character.
476 Similarly, NaN is represented as
478 when using the lowercase conversion, and
480 when using the uppercase conversion.
484 argument is rounded and converted to decimal notation in the style
486 .Oo \- Oc Ar ddd Li \&. Ar ddd ,
488 where the number of digits after the decimal-point character
489 is equal to the precision specification.
490 If the precision is missing, it is taken as 6; if the precision is
491 explicitly zero, no decimal-point character appears.
492 If a decimal point appears, at least one digit appears before it.
496 argument is converted in style
507 The precision specifies the number of significant digits.
508 If the precision is missing, 6 digits are given; if the precision is zero,
512 is used if the exponent from its conversion is less than \-4 or greater than
513 or equal to the precision.
514 Trailing zeros are removed from the fractional part of the result; a
515 decimal point appears only if it is followed by at least one digit.
525 argument is converted to an
526 .Vt "unsigned char" ,
527 and the resulting character is written.
531 (ell) modifier is used, the
533 argument shall be converted to a
535 and the (potentially multi-byte) sequence representing the
536 single wide character is written, including any shift sequences.
537 If a shift sequence is used, the shift state is also restored
538 to the original state after the character.
548 argument is expected to be a pointer to an array of character type (pointer
550 Characters from the array are written up to (but not including)
554 if a precision is specified, no more than the number specified are
556 If a precision is given, no null character
557 need be present; if the precision is not specified, or is greater than
558 the size of the array, the array must contain a terminating
564 (ell) modifier is used, the
566 argument is expected to be a pointer to an array of wide characters
567 (pointer to a wide string).
568 For each wide character in the string, the (potentially multi-byte)
569 sequence representing the
570 wide character is written, including any shift sequences.
571 If any shift sequence is used, the shift state is also restored
572 to the original state after the string.
573 Wide characters from the array are written up to (but not including)
577 if a precision is specified, no more than the number of bytes specified are
578 written (including shift sequences).
579 Partial characters are never written.
580 If a precision is given, no null character
581 need be present; if the precision is not specified, or is greater than
582 the number of bytes required to render the multibyte representation of
583 the string, the array must contain a terminating wide
589 pointer argument is printed in hexadecimal (as if by
594 The number of characters written so far is stored into the
595 integer indicated by the
597 (or variant) pointer argument.
598 No argument is converted.
603 No argument is converted.
604 The complete conversion specification
610 character is defined in the program's locale (category
613 In no case does a non-existent or small field width cause truncation of
614 a numeric field; if the result of a conversion is wider than the field
616 field is expanded to contain the conversion result.
618 To print a date and time in the form
619 .Dq Li "Sunday, July 3, 10:02" ,
624 are pointers to strings:
625 .Bd -literal -offset indent
627 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
628 weekday, month, day, hour, min);
632 to five decimal places:
633 .Bd -literal -offset indent
636 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
639 To allocate a 128 byte string and print into it:
640 .Bd -literal -offset indent
644 char *newfmt(const char *fmt, ...)
648 if ((p = malloc(128)) == NULL)
651 (void) vsnprintf(p, 128, fmt, ap);
656 .Sh SECURITY CONSIDERATIONS
661 functions are easily misused in a manner which enables malicious users
662 to arbitrarily change a running program's functionality through
663 a buffer overflow attack.
668 assume an infinitely long string,
669 callers must be careful not to overflow the actual space;
670 this is often hard to assure.
671 For safety, programmers should use the
677 foo(const char *arbitrary_string, const char *and_another)
683 * This first sprintf is bad behavior. Do not use sprintf!
685 sprintf(onstack, "%s, %s", arbitrary_string, and_another);
688 * The following two lines demonstrate better use of
691 snprintf(onstack, sizeof(onstack), "%s, %s", arbitrary_string,
701 family of functions are also easily misused in a manner
702 allowing malicious users to arbitrarily change a running program's
703 functionality by either causing the program
704 to print potentially sensitive data
705 .Dq "left on the stack" ,
706 or causing it to generate a memory fault or bus error
707 by dereferencing an invalid pointer.
710 can be used to write arbitrary data to potentially carefully-selected
712 Programmers are therefore strongly advised to never pass untrusted strings
715 argument, as an attacker can put format specifiers in the string
716 to mangle your stack,
717 leading to a possible security hole.
718 This holds true even if the string was built using a function like
720 as the resulting string may still contain user-supplied conversion specifiers
721 for later interpolation by
724 Always use the proper secure idiom:
726 .Dl "snprintf(buffer, sizeof(buffer), \*q%s\*q, string);"
728 In addition to the errors documented for the
732 family of functions may fail if:
735 An invalid wide character code was encountered.
737 Insufficient storage space is available.
746 Subject to the caveats noted in the
761 With the same reservation, the
772 first appeared in the
775 These were implemented by
776 .An Peter Wemm Aq peter@FreeBSD.org
779 but were later replaced with a different implementation
781 .An Todd C. Miller Aq Todd.Miller@courtesan.com
785 The conversion formats
790 are provided only for backward compatibility.
791 The effect of padding the
793 format with zeros (either by the
795 flag or by specifying a precision), and the benign effect (i.e., none)
802 conversions, as well as other
803 nonsensical combinations such as
805 are not standard; such combinations
810 family of functions do not correctly handle multibyte characters in the