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