]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/printf.3
cb59b00c5a9621a07e897da059b17d5abcd04789
[thirdparty/man-pages.git] / man3 / printf.3
1 .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
2 .\"
3 .\" This is free documentation; you can redistribute it and/or
4 .\" modify it under the terms of the GNU General Public License as
5 .\" published by the Free Software Foundation; either version 2 of
6 .\" the License, or (at your option) any later version.
7 .\"
8 .\" The GNU General Public License's references to "object code"
9 .\" and "executables" are to be interpreted as the output of any
10 .\" document formatting or typesetting system, including
11 .\" intermediate and printed output.
12 .\"
13 .\" This manual is distributed in the hope that it will be useful,
14 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
15 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 .\" GNU General Public License for more details.
17 .\"
18 .\" You should have received a copy of the GNU General Public
19 .\" License along with this manual; if not, write to the Free
20 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
21 .\" USA.
22 .\"
23 .\"
24 .\" Earlier versions of this page influenced the present text.
25 .\" It was derived from a Berkeley page with version
26 .\" @(#)printf.3 6.14 (Berkeley) 7/30/91
27 .\" converted for Linux by faith@cs.unc.edu, updated by
28 .\" Helmut.Geyer@iwr.uni-heidelberg.de, agulbra@troll.no and Bruno Haible.
29 .\"
30 .\" 1999-11-25 aeb - Rewritten, using SUSv2 and C99.
31 .\" 2000-07-26 jsm28@hermes.cam.ac.uk - three small fixes
32 .\" 2000-10-16 jsm28@hermes.cam.ac.uk - more fixes
33 .\"
34 .TH PRINTF 3 2007-07-26 "GNU" "Linux Programmer's Manual"
35 .SH NAME
36 printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf \- formatted output conversion
37 .SH SYNOPSIS
38 .B #include <stdio.h>
39 .sp
40 .BI "int printf(const char *" format ", ...);"
41 .br
42 .BI "int fprintf(FILE *" stream ", const char *" format ", ...);"
43 .br
44 .BI "int sprintf(char *" str ", const char *" format ", ...);"
45 .br
46 .BI "int snprintf(char *" str ", size_t " size ", const char *" format ", ...);"
47 .sp
48 .B #include <stdarg.h>
49 .sp
50 .BI "int vprintf(const char *" format ", va_list " ap );
51 .br
52 .BI "int vfprintf(FILE *" stream ", const char *" format ", va_list " ap );
53 .br
54 .BI "int vsprintf(char *" str ", const char *" format ", va_list " ap );
55 .br
56 .BI "int vsnprintf(char *" str ", size_t " size ", const char *" format ", va_list " ap );
57 .ad
58 .sp
59 .in -4n
60 Feature Test Macro Requirements for glibc (see
61 .BR feature_test_macros (7)):
62 .in
63 .sp
64 .ad l
65 .BR snprintf (),
66 .BR vsnprintf ():
67 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _ISOC99_SOURCE; or
68 .I "cc -std=c99"
69 .ad b
70 .SH DESCRIPTION
71 The functions in the
72 .BR printf ()
73 family produce output according to a
74 .I format
75 as described below.
76 The functions
77 .BR printf ()
78 and
79 .BR vprintf ()
80 write output to
81 .IR stdout ,
82 the standard output stream;
83 .BR fprintf ()
84 and
85 .BR vfprintf ()
86 write output to the given output
87 .IR stream ;
88 .BR sprintf (),
89 .BR snprintf (),
90 .BR vsprintf ()
91 and
92 .BR vsnprintf ()
93 write to the character string
94 .IR str .
95 .PP
96 The functions
97 .BR snprintf ()
98 and
99 .BR vsnprintf ()
100 write at most
101 .I size
102 bytes (including the trailing null byte ('\e0')) to
103 .IR str .
104 .PP
105 The functions
106 .BR vprintf (),
107 .BR vfprintf (),
108 .BR vsprintf (),
109 .BR vsnprintf ()
110 are equivalent to the functions
111 .BR printf (),
112 .BR fprintf (),
113 .BR sprintf (),
114 .BR snprintf (),
115 respectively, except that they are called with a va_list instead
116 of a variable number of arguments.
117 These functions do not call the
118 .I va_end
119 macro.
120 Consequently, the value of
121 .I ap
122 is undefined after the call.
123 The application should call
124 .I va_end(ap)
125 itself afterwards.
126 .PP
127 These eight functions write the output under the control of a
128 .I format
129 string that specifies how subsequent arguments (or arguments accessed via
130 the variable-length argument facilities of
131 .BR stdarg (3))
132 are converted for output.
133 .SS "Return value"
134 Upon successful return, these functions return the number of characters
135 printed (not including the trailing '\e0' used to end output to strings).
136
137 The functions
138 .BR snprintf ()
139 and
140 .BR vsnprintf ()
141 do not write more than
142 .I size
143 bytes (including the trailing '\e0').
144 If the output was truncated due to this limit then the return value
145 is the number of characters (not including the trailing '\e0')
146 which would have been written to the final string if enough space
147 had been available.
148 Thus, a return value of
149 .I size
150 or more means that the output was truncated.
151 (See also below under NOTES.)
152
153 If an output error is encountered, a negative value is returned.
154 .SS "Format of the format string"
155 The format string is a character string, beginning and ending
156 in its initial shift state, if any.
157 The format string is composed of zero or more directives: ordinary
158 characters (not
159 .BR % ),
160 which are copied unchanged to the output stream;
161 and conversion specifications, each of which results in fetching zero or
162 more subsequent arguments.
163 Each conversion specification is introduced by
164 the character
165 .BR % ,
166 and ends with a
167 .IR "conversion specifier" .
168 In between there may be (in this order) zero or more
169 .IR flags ,
170 an optional minimum
171 .IR "field width" ,
172 an optional
173 .I precision
174 and an optional
175 .IR "length modifier" .
176
177 The arguments must correspond properly (after type promotion) with the
178 conversion specifier.
179 By default, the arguments are used in the order
180 given, where each `*' and each conversion specifier asks for the next
181 argument (and it is an error if insufficiently many arguments are given).
182 One can also specify explicitly which argument is taken,
183 at each place where an argument is required, by writing `%m$' instead
184 of `%' and `*m$' instead of `*', where the decimal integer m denotes
185 the position in the argument list of the desired argument, indexed starting
186 from 1.
187 Thus,
188 .RS
189 .nf
190
191 printf("%*d", width, num);
192
193 .fi
194 .RE
195 and
196 .RS
197 .nf
198
199 printf("%2$*1$d", width, num);
200
201 .fi
202 .RE
203 are equivalent.
204 The second style allows repeated references to the
205 same argument.
206 The C99 standard does not include the style using `$',
207 which comes from the Single Unix Specification.
208 If the style using
209 `$' is used, it must be used throughout for all conversions taking an
210 argument and all width and precision arguments, but it may be mixed
211 with `%%' formats which do not consume an argument.
212 There may be no
213 gaps in the numbers of arguments specified using `$'; for example, if
214 arguments 1 and 3 are specified, argument 2 must also be specified
215 somewhere in the format string.
216
217 For some numeric conversions a radix character (`decimal point') or
218 thousands' grouping character is used.
219 The actual character used
220 depends on the
221 .B LC_NUMERIC
222 part of the locale.
223 The POSIX locale
224 uses `.' as radix character, and does not have a grouping character.
225 Thus,
226 .RS
227 .nf
228 printf("%'.2f", 1234567.89);
229 .fi
230 .RE
231 results in `1234567.89' in the POSIX locale, in `1234567,89' in the
232 nl_NL locale, and in `1.234.567,89' in the da_DK locale.
233 .SS "The flag characters"
234 The character % is followed by zero or more of the following flags:
235 .TP
236 .B #
237 The value should be converted to an ``alternate form''.
238 For
239 .BR o
240 conversions, the first character of the output string is made zero
241 (by prefixing a 0 if it was not zero already).
242 For
243 .B x
244 and
245 .B X
246 conversions, a non-zero result has the string `0x' (or `0X' for
247 .B X
248 conversions) prepended to it.
249 For
250 .BR a ,
251 .BR A ,
252 .BR e ,
253 .BR E ,
254 .BR f ,
255 .BR F ,
256 .BR g ,
257 and
258 .B G
259 conversions, the result will always contain a decimal point, even if no
260 digits follow it (normally, a decimal point appears in the results of those
261 conversions only if a digit follows).
262 For
263 .B g
264 and
265 .B G
266 conversions, trailing zeros are not removed from the result as they would
267 otherwise be.
268 For other conversions, the result is undefined.
269 .TP
270 .B \&0
271 The value should be zero padded.
272 For
273 .BR d ,
274 .BR i ,
275 .BR o ,
276 .BR u ,
277 .BR x ,
278 .BR X ,
279 .BR a ,
280 .BR A ,
281 .BR e ,
282 .BR E ,
283 .BR f ,
284 .BR F ,
285 .BR g ,
286 and
287 .B G
288 conversions, the converted value is padded on the left with zeros rather
289 than blanks.
290 If the
291 .B \&0
292 and
293 .B \-
294 flags both appear, the
295 .B \&0
296 flag is ignored.
297 If a precision is given with a numeric conversion
298 .BR "" ( d ,
299 .BR i ,
300 .BR o ,
301 .BR u ,
302 .BR x ,
303 and
304 .BR X ),
305 the
306 .B \&0
307 flag is ignored.
308 For other conversions, the behavior is undefined.
309 .TP
310 .B \-
311 The converted value is to be left adjusted on the field boundary.
312 (The default is right justification.)
313 Except for
314 .B n
315 conversions, the converted value is padded on the right with blanks, rather
316 than on the left with blanks or zeros.
317 A
318 .B \-
319 overrides a
320 .B \&0
321 if both are given.
322 .TP
323 .B ' '
324 (a space) A blank should be left before a positive number
325 (or empty string) produced by a signed conversion.
326 .TP
327 .B +
328 A sign (+ or \-) should always be placed before a number produced by a signed
329 conversion.
330 By default a sign is used only for negative numbers.
331 A
332 .B +
333 overrides a space if both are used.
334 .PP
335 The five flag characters above are defined in the C standard.
336 The SUSv2 specifies one further flag character.
337 .TP
338 .B '
339 For decimal conversion
340 .BR "" ( i ,
341 .BR d ,
342 .BR u ,
343 .BR f ,
344 .BR F ,
345 .BR g ,
346 .BR G )
347 the output is to be grouped with thousands' grouping characters
348 if the locale information indicates any.
349 Note that many versions of
350 .BR gcc (1)
351 cannot parse this option and will issue a warning.
352 SUSv2 does not
353 include %'F.
354 .PP
355 glibc 2.2 adds one further flag character.
356 .TP
357 .B I
358 For decimal integer conversion
359 .BR "" ( i ,
360 .BR d ,
361 .BR u )
362 the output uses the locale's alternative output digits, if any.
363 For example, since glibc 2.2.3 this will give Arabic-Indic digits
364 in the Persian (`fa_IR') locale.
365 .\" outdigits keyword in locale file
366 .SS "The field width"
367 An optional decimal digit string (with non-zero first digit) specifying
368 a minimum field width.
369 If the converted value has fewer characters
370 than the field width, it will be padded with spaces on the left
371 (or right, if the left-adjustment flag has been given).
372 Instead of a decimal digit string one may write `*' or `*m$'
373 (for some decimal integer m) to specify that the field width
374 is given in the next argument, or in the m-th argument, respectively,
375 which must be of type
376 .IR int .
377 A negative field width is taken as a `\-' flag followed by a
378 positive field width.
379 In no case does a non-existent or small field width cause truncation of a
380 field; if the result of a conversion is wider than the field width, the
381 field is expanded to contain the conversion result.
382 .SS "The precision"
383 An optional precision, in the form of a period (`\&.') followed by an
384 optional decimal digit string.
385 Instead of a decimal digit string one may write `*' or `*m$'
386 (for some decimal integer m) to specify that the precision
387 is given in the next argument, or in the m-th argument, respectively,
388 which must be of type
389 .IR int .
390 If the precision is given as just `.', or the precision is negative,
391 the precision is taken to be zero.
392 This gives the minimum number of digits to appear for
393 .BR d ,
394 .BR i ,
395 .BR o ,
396 .BR u ,
397 .BR x ,
398 and
399 .B X
400 conversions, the number of digits to appear after the radix character for
401 .BR a ,
402 .BR A ,
403 .BR e ,
404 .BR E ,
405 .BR f ,
406 and
407 .B F
408 conversions, the maximum number of significant digits for
409 .B g
410 and
411 .B G
412 conversions, or the maximum number of characters to be printed from a
413 string for
414 .B s
415 and
416 .B S
417 conversions.
418 .SS "The length modifier"
419 Here, `integer conversion' stands for
420 .BR d ,
421 .BR i ,
422 .BR o ,
423 .BR u ,
424 .BR x ,
425 or
426 .BR X
427 conversion.
428 .TP
429 .B hh
430 A following integer conversion corresponds to a
431 .I signed char
432 or
433 .I unsigned char
434 argument, or a following
435 .B n
436 conversion corresponds to a pointer to a
437 .I signed char
438 argument.
439 .TP
440 .B h
441 A following integer conversion corresponds to a
442 .I short int
443 or
444 .I unsigned short int
445 argument, or a following
446 .B n
447 conversion corresponds to a pointer to a
448 .I short int
449 argument.
450 .TP
451 .B l
452 (ell) A following integer conversion corresponds to a
453 .I long int
454 or
455 .I unsigned long int
456 argument, or a following
457 .B n
458 conversion corresponds to a pointer to a
459 .I long int
460 argument, or a following
461 .B c
462 conversion corresponds to a
463 .I wint_t
464 argument, or a following
465 .B s
466 conversion corresponds to a pointer to
467 .I wchar_t
468 argument.
469 .TP
470 .B ll
471 (ell-ell).
472 A following integer conversion corresponds to a
473 .I long long int
474 or
475 .I unsigned long long int
476 argument, or a following
477 .B n
478 conversion corresponds to a pointer to a
479 .I long long int
480 argument.
481 .TP
482 .BR L
483 A following
484 .BR a ,
485 .BR A ,
486 .BR e ,
487 .BR E ,
488 .BR f ,
489 .BR F ,
490 .BR g ,
491 or
492 .B G
493 conversion corresponds to a
494 .I long double
495 argument.
496 (C99 allows %LF, but SUSv2 does not.)
497 .TP
498 .B q
499 (`quad'. 4.4BSD and Linux libc5 only.
500 Don't use.)
501 This is a synonym for
502 .BR ll .
503 .TP
504 .B j
505 A following integer conversion corresponds to an
506 .I intmax_t
507 or
508 .I uintmax_t
509 argument.
510 .TP
511 .B z
512 A following integer conversion corresponds to a
513 .I size_t
514 or
515 .I ssize_t
516 argument.
517 (Linux libc5 has
518 .B Z
519 with this meaning.
520 Don't use it.)
521 .TP
522 .B t
523 A following integer conversion corresponds to a
524 .I ptrdiff_t
525 argument.
526 .PP
527 The SUSv2 only knows about the length modifiers
528 .B h
529 (in
530 .BR hd ,
531 .BR hi ,
532 .BR ho ,
533 .BR hx ,
534 .BR hX ,
535 .BR hn )
536 and
537 .B l
538 (in
539 .BR ld ,
540 .BR li ,
541 .BR lo ,
542 .BR lx ,
543 .BR lX ,
544 .BR ln ,
545 .BR lc ,
546 .BR ls )
547 and
548 .B L
549 (in
550 .BR Le ,
551 .BR LE ,
552 .BR Lf ,
553 .BR Lg ,
554 .BR LG ).
555 .SS "The conversion specifier"
556 A character that specifies the type of conversion to be applied.
557 The conversion specifiers and their meanings are:
558 .TP
559 .BR d , i
560 The
561 .I int
562 argument is converted to signed decimal notation.
563 The precision, if any, gives the minimum number of digits
564 that must appear; if the converted value requires fewer digits, it is
565 padded on the left with zeros.
566 The default precision is 1.
567 When 0 is printed with an explicit precision 0, the output is empty.
568 .TP
569 .BR o , u , x , X
570 The
571 .I "unsigned int"
572 argument is converted to unsigned octal
573 .BR "" ( o ),
574 unsigned decimal
575 .BR "" ( u ),
576 or unsigned hexadecimal
577 .BR "" ( x
578 and
579 .BR X )
580 notation.
581 The letters
582 .B abcdef
583 are used for
584 .B x
585 conversions; the letters
586 .B ABCDEF
587 are used for
588 .B X
589 conversions.
590 The precision, if any, gives the minimum number of digits
591 that must appear; if the converted value requires fewer digits, it is
592 padded on the left with zeros.
593 The default precision is 1.
594 When 0 is printed with an explicit precision 0, the output is empty.
595 .TP
596 .BR e , E
597 The
598 .I double
599 argument is rounded and converted in the style
600 .if \w'\*(Pm'=0 .ds Pm \(+-
601 .BR "" [\-]d \&. ddd e \\*(Pmdd
602 where there is one digit before the decimal-point character and the number
603 of digits after it is equal to the precision; if the precision is missing,
604 it is taken as 6; if the precision is zero, no decimal-point character
605 appears.
606 An
607 .B E
608 conversion uses the letter
609 .B E
610 (rather than
611 .BR e )
612 to introduce the exponent.
613 The exponent always contains at least two
614 digits; if the value is zero, the exponent is 00.
615 .TP
616 .BR f , F
617 The
618 .I double
619 argument is rounded and converted to decimal notation in the style
620 .BR "" [\-]ddd \&. ddd,
621 where the number of digits after the decimal-point character is equal to
622 the precision specification.
623 If the precision is missing, it is taken as
624 6; if the precision is explicitly zero, no decimal-point character appears.
625 If a decimal point appears, at least one digit appears before it.
626
627 (The SUSv2 does not know about
628 .B F
629 and says that character string representations for infinity and NaN
630 may be made available.
631 The C99 standard specifies `[\-]inf' or `[\-]infinity'
632 for infinity, and a string starting with `nan' for NaN, in the case of
633 .B f
634 conversion, and `[\-]INF' or `[\-]INFINITY' or `NAN*' in the case of
635 .B F
636 conversion.)
637 .TP
638 .BR g , G
639 The
640 .I double
641 argument is converted in style
642 .B f
643 or
644 .B e
645 (or
646 .B F
647 or
648 .B E
649 for
650 .B G
651 conversions).
652 The precision specifies the number of significant digits.
653 If the precision is missing, 6 digits are given; if the precision is zero,
654 it is treated as 1.
655 Style
656 .B e
657 is used if the exponent from its conversion is less than \-4 or greater
658 than or equal to the precision.
659 Trailing zeros are removed from the
660 fractional part of the result; a decimal point appears only if it is
661 followed by at least one digit.
662 .TP
663 .BR a , A
664 (C99; not in SUSv2) For
665 .B a
666 conversion, the
667 .I double
668 argument is converted to hexadecimal notation (using the letters abcdef)
669 in the style
670 .BR "" [\-] 0x h \&. hhhh p \\*(Pmd;
671 for
672 .B A
673 conversion the prefix
674 .BR 0X ,
675 the letters ABCDEF, and the exponent separator
676 .B P
677 is used.
678 There is one hexadecimal digit before the decimal point,
679 and the number of digits after it is equal to the precision.
680 The default precision suffices for an exact representation of the value
681 if an exact representation in base 2 exists
682 and otherwise is sufficiently large to distinguish values of type
683 .IR double .
684 The digit before the decimal point is unspecified for non-normalized
685 numbers, and non-zero but otherwise unspecified for normalized numbers.
686 .TP
687 .B c
688 If no
689 .B l
690 modifier is present, the
691 .I int
692 argument is converted to an
693 .IR "unsigned char" ,
694 and the resulting character is written.
695 If an
696 .B l
697 modifier is present, the
698 .I wint_t
699 (wide character) argument is converted to a multibyte sequence by a call
700 to the
701 .BR wcrtomb (3)
702 function, with a conversion state starting in the initial state, and the
703 resulting multibyte string is written.
704 .TP
705 .B s
706 If no
707 .B l
708 modifier is present: The
709 .I "const char *"
710 argument is expected to be a pointer to an array of character type (pointer
711 to a string).
712 Characters from the array are written up to (but not
713 including) a terminating null byte ('\\0');
714 if a precision is specified, no more than the number specified
715 are written.
716 If a precision is given, no null byte need be present;
717 if the precision is not specified, or is greater than the size of the
718 array, the array must contain a terminating null byte.
719
720 If an
721 .B l
722 modifier is present: The
723 .I "const wchar_t *"
724 argument is expected to be a pointer to an array of wide characters.
725 Wide characters from the array are converted to multibyte characters
726 (each by a call to the
727 .BR wcrtomb (3)
728 function, with a conversion state starting in the initial state before
729 the first wide character), up to and including a terminating null
730 wide character.
731 The resulting multibyte characters are written up to
732 (but not including) the terminating null byte.
733 If a precision is
734 specified, no more bytes than the number specified are written, but
735 no partial multibyte characters are written.
736 Note that the precision
737 determines the number of
738 .I bytes
739 written, not the number of
740 .I wide characters
741 or
742 .IR "screen positions" .
743 The array must contain a terminating null wide character, unless a
744 precision is given and it is so small that the number of bytes written
745 exceeds it before the end of the array is reached.
746 .TP
747 .B C
748 (Not in C99, but in SUSv2.)
749 Synonym for
750 .BR lc .
751 Don't use.
752 .TP
753 .B S
754 (Not in C99, but in SUSv2.)
755 Synonym for
756 .BR ls .
757 Don't use.
758 .TP
759 .B p
760 The
761 .I "void *"
762 pointer argument is printed in hexadecimal (as if by
763 .B %#x
764 or
765 .BR %#lx ).
766 .TP
767 .B n
768 The number of characters written so far is stored into the integer
769 indicated by the
770 .I "int *"
771 (or variant) pointer argument.
772 No argument is converted.
773 .TP
774 .B m
775 (Glibc extension.)
776 Print output of
777 .IR strerror(errno) .
778 No argument is required.
779 .TP
780 .B %
781 A `%' is written.
782 No argument is converted.
783 The complete conversion
784 specification is `%%'.
785 .SH "CONFORMING TO"
786 The
787 .BR fprintf (),
788 .BR printf (),
789 .BR sprintf (),
790 .BR vprintf (),
791 .BR vfprintf (),
792 and
793 .BR vsprintf ()
794 functions conform to C89 and C99.
795 The
796 .BR snprintf ()
797 and
798 .BR vsnprintf ()
799 functions conform to C99.
800 .PP
801 Concerning the return value of
802 .BR snprintf (),
803 SUSv2 and C99 contradict each other: when
804 .BR snprintf ()
805 is called with
806 .IR size =0
807 then SUSv2 stipulates an unspecified return value less than 1,
808 while C99 allows
809 .I str
810 to be NULL in this case, and gives the return value (as always)
811 as the number of characters that would have been written in case
812 the output string has been large enough.
813 .PP
814 Linux libc4 knows about the five C standard flags.
815 It knows about the length modifiers h,l,L, and the conversions
816 cdeEfFgGinopsuxX, where F is a synonym for f.
817 Additionally, it accepts D,O,U as synonyms for ld,lo,lu.
818 (This is bad, and caused serious bugs later, when
819 support for %D disappeared.)
820 No locale-dependent radix character,
821 no thousands' separator, no NaN or infinity, no %m$ and *m$.
822 .PP
823 Linux libc5 knows about the five C standard flags and the ' flag,
824 locale, %m$ and *m$.
825 It knows about the length modifiers h,l,L,Z,q, but accepts L and q
826 both for \fIlong double\fP and for \fIlong long int\fP (this is a bug).
827 It no longer recognizes FDOU, but adds the conversion character
828 .BR m ,
829 which outputs
830 .IR strerror(errno) .
831 .PP
832 glibc 2.0 adds conversion characters C and S.
833 .PP
834 glibc 2.1 adds length modifiers hh,j,t,z and conversion characters a,A.
835 .PP
836 glibc 2.2 adds the conversion character F with C99 semantics, and the
837 flag character I.
838 .SH NOTES
839 The glibc implementation of the functions
840 .BR snprintf ()
841 and
842 .BR vsnprintf ()
843 conforms to the C99 standard, that is, behaves as described above,
844 since glibc version 2.1.
845 Until glibc 2.0.6 they would return \-1
846 when the output was truncated.
847 .\" .SH HISTORY
848 .\" Unix V7 defines the three routines
849 .\" .BR printf (),
850 .\" .BR fprintf (),
851 .\" .BR sprintf (),
852 .\" and has the flag \-, the width or precision *, the length modifier l,
853 .\" and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx.
854 .\" This is still true for 2.9.1BSD, but 2.10BSD has the flags
855 .\" #, + and <space> and no longer mentions D,O,U,X.
856 .\" 2.11BSD has
857 .\" .BR vprintf (),
858 .\" .BR vfprintf (),
859 .\" .BR vsprintf (),
860 .\" and warns not to use D,O,U,X.
861 .\" 4.3BSD Reno has the flag 0, the length modifiers h and L,
862 .\" and the conversions n, p, E, G, X (with current meaning)
863 .\" and deprecates D,O,U.
864 .\" 4.4BSD introduces the functions
865 .\" .BR snprintf ()
866 .\" and
867 .\" .BR vsnprintf (),
868 .\" and the length modifier q.
869 .\" FreeBSD also has functions
870 .\" .BR asprintf ()
871 .\" and
872 .\" .BR vasprintf (),
873 .\" that allocate a buffer large enough for
874 .\" .BR sprintf ().
875 .\" In glibc there are functions
876 .\" .BR dprintf ()
877 .\" and
878 .\" .BR vdprintf ()
879 .\" that print to a file descriptor instead of a stream.
880 .SH BUGS
881 Because
882 .BR sprintf ()
883 and
884 .BR vsprintf ()
885 assume an arbitrarily long string, callers must be careful not to overflow
886 the actual space; this is often impossible to assure.
887 Note that the length
888 of the strings produced is locale-dependent and difficult to predict.
889 Use
890 .BR snprintf ()
891 and
892 .BR vsnprintf ()
893 instead (or
894 .BR asprintf (3)
895 and
896 .BR vasprintf (3)).
897 .PP
898 Linux libc4.[45] does not have a
899 .BR snprintf (),
900 but provides a libbsd that contains an
901 .BR snprintf ()
902 equivalent to
903 .BR sprintf (),
904 that is, one that ignores the
905 .I size
906 argument.
907 Thus, the use of
908 .BR snprintf ()
909 with early libc4 leads to serious security problems.
910 .PP
911 Code such as
912 .BI printf( foo );
913 often indicates a bug, since
914 .I foo
915 may contain a % character.
916 If
917 .I foo
918 comes from untrusted user input, it may contain %n, causing the
919 .BR printf ()
920 call to write to memory and creating a security hole.
921 .\" .PP
922 .\" Some floating point conversions under early libc4
923 .\" caused memory leaks.
924 .SH EXAMPLE
925 .br
926 .if \w'\*(Pi'=0 .ds Pi pi
927 To print \*(Pi to five decimal places:
928 .RS
929 .nf
930
931 #include <math.h>
932 #include <stdio.h>
933 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
934 .fi
935 .RE
936 .PP
937 To print a date and time in the form `Sunday, July 3, 10:02',
938 where
939 .I weekday
940 and
941 .I month
942 are pointers to strings:
943 .RS
944 .nf
945
946 #include <stdio.h>
947 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
948 weekday, month, day, hour, min);
949 .fi
950 .RE
951 .PP
952 Many countries use the day-month-year order.
953 Hence, an internationalized version must be able to print
954 the arguments in an order specified by the format:
955 .RS
956 .nf
957
958 #include <stdio.h>
959 fprintf(stdout, format,
960 weekday, month, day, hour, min);
961
962 .fi
963 .RE
964 where
965 .I format
966 depends on locale, and may permute the arguments.
967 With the value
968 .RS
969 .nf
970 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
971 .fi
972 .RE
973 one might obtain `Sonntag, 3. Juli, 10:02'.
974 .PP
975 To allocate a sufficiently large string and print into it
976 (code correct for both glibc 2.0 and glibc 2.1):
977 .RS 4
978 .nf
979
980 #include <stdio.h>
981 #include <stdlib.h>
982 #include <stdarg.h>
983
984 char *
985 make_message(const char *fmt, ...)
986 {
987 /* Guess we need no more than 100 bytes. */
988 int n, size = 100;
989 char *p, *np;
990 va_list ap;
991
992 if ((p = malloc(size)) == NULL)
993 return NULL;
994
995 while (1) {
996 /* Try to print in the allocated space. */
997 va_start(ap, fmt);
998 n = vsnprintf(p, size, fmt, ap);
999 va_end(ap);
1000 /* If that worked, return the string. */
1001 if (n > \-1 && n < size)
1002 return p;
1003 /* Else try again with more space. */
1004 if (n > \-1) /* glibc 2.1 */
1005 size = n+1; /* precisely what is needed */
1006 else /* glibc 2.0 */
1007 size *= 2; /* twice the old size */
1008 if ((np = realloc (p, size)) == NULL) {
1009 free(p);
1010 return NULL;
1011 } else {
1012 p = np;
1013 }
1014 }
1015 }
1016 .fi
1017 .RE
1018 .SH "SEE ALSO"
1019 .BR printf (1),
1020 .BR asprintf (3),
1021 .BR dprintf (3),
1022 .BR scanf (3),
1023 .BR setlocale (3),
1024 .BR wcrtomb (3),
1025 .BR wprintf (3),
1026 .BR locale (5)