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