]> git.saurik.com Git - apple/libc.git/blame - stdio/wprintf.3
Libc-763.11.tar.gz
[apple/libc.git] / stdio / wprintf.3
CommitLineData
224c7076
A
1.\" Copyright (c) 1990, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
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.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
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.
224c7076
A
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)printf.3 8.1 (Berkeley) 6/4/93
33.\" FreeBSD: src/lib/libc/stdio/printf.3,v 1.47 2002/09/06 11:23:55 tjr Exp
1f2f436a 34.\" $FreeBSD: src/lib/libc/stdio/wprintf.3,v 1.6 2007/01/09 00:28:08 imp Exp $
224c7076
A
35.\"
36.Dd July 5, 2003
37.Dt WPRINTF 3
38.Os
39.Sh NAME
40.Nm fwprintf ,
41.Nm swprintf ,
42.Nm vfwprintf ,
43.Nm vswprintf ,
44.Nm vwprintf ,
45.Nm wprintf
46.Nd formatted wide character output conversion
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.In stdio.h
51.In wchar.h
52.Ft int
53.Fo fwprintf
54.Fa "FILE *restrict stream"
55.Fa "const wchar_t *restrict format"
56.Fa ...
57.Fc
58.Ft int
59.Fo swprintf
60.Fa "wchar_t *restrict ws"
61.Fa "size_t n"
62.Fa "const wchar_t *restrict format"
63.Fa ...
64.Fc
65.Ft int
66.Fo wprintf
67.Fa "const wchar_t *restrict format"
68.Fa ...
69.Fc
70.In stdarg.h
71.In stdio.h
72.In wchar.h
73.Ft int
74.Fo vfwprintf
75.Fa "FILE *restrict stream"
76.Fa "const wchar_t *restrict format"
77.Fa "va_list arg"
78.Fc
79.Ft int
80.Fo vswprintf
81.Fa "wchar_t *restrict ws"
82.Fa "size_t n"
83.Fa "const wchar_t *restrict format"
84.Fa "va_list arg"
85.Fc
86.Ft int
87.Fo vwprintf
88.Fa "const wchar_t *restrict format"
89.Fa "va_list arg"
90.Fc
91.Sh DESCRIPTION
92The
93.Fn wprintf
94family of functions produces output according to a
95.Fa format ,
96as described below.
97The
98.Fn wprintf
99and
100.Fn vwprintf
101functions
102write output to
103.Dv stdout ,
104the standard output stream;
105.Fn fwprintf
106and
107.Fn vfwprintf
108write output to the given output
109.Fa stream ;
110.Fn swprintf
111and
112.Fn vswprintf
113write to the wide character string
114.Fa ws .
115.Pp
116Extended locale versions of these functions are documented in
117.Xr wprintf_l 3 .
118See
119.Xr xlocale 3
120for more information.
121.Pp
122These functions write the output under the control of a
123.Fa format
124string that specifies how subsequent arguments
125(or arguments accessed via the variable-length argument facilities of
126.Xr stdarg 3 )
127are converted for output.
128.Pp
129These functions return the number of characters printed
130(not including the trailing
131.Ql \e0 ,
132used to end output to strings).
133.Pp
134The
135.Fn swprintf
136and
137.Fn vswprintf
138functions will fail if
139.Fa n
140or more wide characters were requested to be written,
141.Pp
142The format string is composed of zero or more directives:
143ordinary
144characters (not
145.Cm % ) ,
146which are copied unchanged to the output stream;
147and conversion specifications, each of which results
148in fetching zero or more subsequent arguments.
149Each conversion specification is introduced by
150the
151.Cm %
152character.
153The arguments must correspond properly (after type promotion)
154with the conversion specifier.
155After the
156.Cm % ,
157the following appear in sequence:
158.Bl -bullet
159.It
160An optional field, consisting of a decimal digit string followed by a
161.Cm $ ,
162specifying the next argument to access.
163If this field is not provided, the argument following the last
164argument accessed will be used.
165Arguments are numbered starting at
166.Cm 1 .
167If unaccessed arguments in the format string are interspersed with ones that
168are accessed the results will be indeterminate.
169.It
170Zero or more of the following flags:
171.Bl -tag -width ".So \ Sc (space)"
172.It Sq Cm #
173The value should be converted to an
174.Dq alternate form .
175For
176.Cm c , d , i , n , p , s ,
177and
178.Cm u
179conversions, this option has no effect.
180For
181.Cm o
182conversions, the precision of the number is increased to force the first
183character of the output string to a zero (except if a zero value is printed
184with an explicit precision of zero).
185For
186.Cm x
187and
188.Cm X
189conversions, a non-zero result has the string
190.Ql 0x
191(or
192.Ql 0X
193for
194.Cm X
195conversions) prepended to it.
196For
197.Cm a , A , e , E , f , F , g ,
198and
199.Cm G
200conversions, the result will always contain a decimal point, even if no
201digits follow it (normally, a decimal point appears in the results of
202those conversions only if a digit follows).
203For
204.Cm g
205and
206.Cm G
207conversions, trailing zeros are not removed from the result as they
208would otherwise be.
209.It So Cm 0 Sc (zero)
210Zero padding.
211For all conversions except
212.Cm n ,
213the converted value is padded on the left with zeros rather than blanks.
214If a precision is given with a numeric conversion
215.Cm ( d , i , o , u , i , x ,
216and
217.Cm X ) ,
218the
219.Cm 0
220flag is ignored.
221.It Sq Cm \-
222A negative field width flag;
223the converted value is to be left adjusted on the field boundary.
224Except for
225.Cm n
226conversions, the converted value is padded on the right with blanks,
227rather than on the left with blanks or zeros.
228A
229.Cm \-
230overrides a
231.Cm 0
232if both are given.
233.It So "\ " Sc (space)
234A blank should be left before a positive number
235produced by a signed conversion
236.Cm ( a , A , d , e , E , f , F , g , G ,
237or
238.Cm i ) .
239.It Sq Cm +
240A sign must always be placed before a
241number produced by a signed conversion.
242A
243.Cm +
244overrides a space if both are used.
245.It Sq Cm '
246Decimal conversions
247.Cm ( d , u ,
248or
249.Cm i )
250or the integral portion of a floating point conversion
251.Cm ( f
252or
253.Cm F )
254should be grouped and separated by thousands using
255the non-monetary separator returned by
256.Xr localeconv 3 .
257.El
258.It
259An optional decimal digit string specifying a minimum field width.
260If the converted value has fewer characters than the field width, it will
261be padded with spaces on the left (or right, if the left-adjustment
262flag has been given) to fill out
263the field width.
264.It
265An optional precision, in the form of a period
266.Cm \&.
267followed by an
268optional digit string.
269If the digit string is omitted, the precision is taken as zero.
270This gives the minimum number of digits to appear for
271.Cm d , i , o , u , x ,
272and
273.Cm X
274conversions, the number of digits to appear after the decimal-point for
275.Cm a , A , e , E , f ,
276and
277.Cm F
278conversions, the maximum number of significant digits for
279.Cm g
280and
281.Cm G
282conversions, or the maximum number of characters to be printed from a
283string for
284.Cm s
285conversions.
286.It
287An optional length modifier, that specifies the size of the argument.
288The following length modifiers are valid for the
289.Cm d , i , n , o , u , x ,
290or
291.Cm X
292conversion:
293.Bl -column ".Cm q Em (deprecated)" ".Vt signed char" ".Vt unsigned long long" ".Vt long long *"
294.It Sy Modifier Ta Cm d , i Ta Cm o , u , x , X Ta Cm n
295.It Cm hh Ta Vt "signed char" Ta Vt "unsigned char" Ta Vt "signed char *"
296.It Cm h Ta Vt short Ta Vt "unsigned short" Ta Vt "short *"
297.It Cm l No (ell) Ta Vt long Ta Vt "unsigned long" Ta Vt "long *"
298.It Cm ll No (ell ell) Ta Vt "long long" Ta Vt "unsigned long long" Ta Vt "long long *"
299.It Cm j Ta Vt intmax_t Ta Vt uintmax_t Ta Vt "intmax_t *"
300.It Cm t Ta Vt ptrdiff_t Ta (see note) Ta Vt "ptrdiff_t *"
301.It Cm z Ta (see note) Ta Vt size_t Ta (see note)
302.It Cm q Em (deprecated) Ta Vt quad_t Ta Vt u_quad_t Ta Vt "quad_t *"
303.El
304.Pp
305Note:
306the
307.Cm t
308modifier, when applied to a
309.Cm o , u , x ,
310or
311.Cm X
312conversion, indicates that the argument is of an unsigned type
313equivalent in size to a
314.Vt ptrdiff_t .
315The
316.Cm z
317modifier, when applied to a
318.Cm d
319or
320.Cm i
321conversion, indicates that the argument is of a signed type equivalent in
322size to a
323.Vt size_t .
324Similarly, when applied to an
325.Cm n
326conversion, it indicates that the argument is a pointer to a signed type
327equivalent in size to a
328.Vt size_t .
329.Pp
330The following length modifier is valid for the
331.Cm a , A , e , E , f , F , g ,
332or
333.Cm G
334conversion:
335.Bl -column ".Sy Modifier" ".Cm a , A , e , E , f , F , g , G"
336.It Sy Modifier Ta Cm a , A , e , E , f , F , g , G
337.It Cm L Ta Vt "long double"
338.El
339.Pp
340The following length modifier is valid for the
341.Cm c
342or
343.Cm s
344conversion:
345.Bl -column ".Sy Modifier" ".Vt wint_t" ".Vt wchar_t *"
346.It Sy Modifier Ta Cm c Ta Cm s
347.It Cm l No (ell) Ta Vt wint_t Ta Vt "wchar_t *"
348.El
349.It
350A character that specifies the type of conversion to be applied.
351.El
352.Pp
353A field width or precision, or both, may be indicated by
354an asterisk
355.Ql *
356or an asterisk followed by one or more decimal digits and a
357.Ql $
358instead of a
359digit string.
360In this case, an
361.Vt int
362argument supplies the field width or precision.
363A negative field width is treated as a left adjustment flag followed by a
364positive field width; a negative precision is treated as though it were
365missing.
366If a single format directive mixes positional
367.Pq Li nn$
368and non-positional arguments, the results are undefined.
369.Pp
370The conversion specifiers and their meanings are:
371.Bl -tag -width ".Cm diouxX"
372.It Cm diouxX
373The
374.Vt int
375(or appropriate variant) argument is converted to signed decimal
376.Cm ( d
377and
378.Cm i ) ,
379unsigned octal
380.Pq Cm o ,
381unsigned decimal
382.Pq Cm u ,
383or unsigned hexadecimal
384.Cm ( x
385and
386.Cm X )
387notation.
388The letters
389.Dq Li abcdef
390are used for
391.Cm x
392conversions; the letters
393.Dq Li ABCDEF
394are used for
395.Cm X
396conversions.
397The precision, if any, gives the minimum number of digits that must
398appear; if the converted value requires fewer digits, it is padded on
399the left with zeros.
400.It Cm DOU
401The
402.Vt "long int"
403argument is converted to signed decimal, unsigned octal, or unsigned
404decimal, as if the format had been
405.Cm ld , lo ,
406or
407.Cm lu
408respectively.
409These conversion characters are deprecated, and will eventually disappear.
410.It Cm eE
411The
412.Vt double
413argument is rounded and converted in the style
414.Sm off
415.Oo \- Oc Ar d Li \&. Ar ddd Li e \\*[Pm] Ar dd
416.Sm on
417where there is one digit before the
418decimal-point character
419and the number of digits after it is equal to the precision;
420if the precision is missing,
421it is taken as 6; if the precision is
422zero, no decimal-point character appears.
423An
424.Cm E
425conversion uses the letter
426.Ql E
427(rather than
428.Ql e )
429to introduce the exponent.
430The exponent always contains at least two digits; if the value is zero,
431the exponent is 00.
432.Pp
433For
434.Cm a , A , e , E , f , F , g ,
435and
436.Cm G
437conversions, positive and negative infinity are represented as
438.Li inf
439and
440.Li -inf
441respectively when using the lowercase conversion character, and
442.Li INF
443and
444.Li -INF
445respectively when using the uppercase conversion character.
446Similarly, NaN is represented as
447.Li nan
448when using the lowercase conversion, and
449.Li NAN
450when using the uppercase conversion.
451.It Cm fF
452The
453.Vt double
454argument is rounded and converted to decimal notation in the style
455.Sm off
456.Oo \- Oc Ar ddd Li \&. Ar ddd ,
457.Sm on
458where the number of digits after the decimal-point character
459is equal to the precision specification.
460If the precision is missing, it is taken as 6; if the precision is
461explicitly zero, no decimal-point character appears.
462If a decimal point appears, at least one digit appears before it.
463.It Cm gG
464The
465.Vt double
466argument is converted in style
467.Cm f
468or
469.Cm e
470(or
471.Cm F
472or
473.Cm E
474for
475.Cm G
476conversions).
477The precision specifies the number of significant digits.
478If the precision is missing, 6 digits are given; if the precision is zero,
479it is treated as 1.
480Style
481.Cm e
482is used if the exponent from its conversion is less than \-4 or greater than
483or equal to the precision.
484Trailing zeros are removed from the fractional part of the result; a
485decimal point appears only if it is followed by at least one digit.
486.It Cm aA
487The
488.Vt double
489argument is converted to hexadecimal notation in the style
490.Sm off
491.Oo \- Oc Li 0x Ar h Li \&. Ar hhhp Oo \\*[Pm] Oc Ar d ,
492.Sm on
493where the number of digits after the hexadecimal-point character
494is equal to the precision specification.
495If the precision is missing, it is taken as enough to exactly
496represent the floating-point number; if the precision is
497explicitly zero, no hexadecimal-point character appears.
498This is an exact conversion of the mantissa+exponent internal
499floating point representation; the
500.Sm off
501.Oo \- Oc Li 0x Ar h Li \&. Ar hhh
502.Sm on
503portion represents exactly the mantissa; only denormalized
504mantissas have a zero value to the left of the hexadecimal
505point.
506The
507.Cm p
508is a literal character
509.Ql p ;
510the exponent is preceded by a positive or negative sign
511and is represented in decimal, using only enough characters
512to represent the exponent.
513The
514.Cm A
515conversion uses the prefix
516.Dq Li 0X
517(rather than
518.Dq Li 0x ) ,
519the letters
520.Dq Li ABCDEF
521(rather than
522.Dq Li abcdef )
523to represent the hex digits, and the letter
524.Ql P
525(rather than
526.Ql p )
527to separate the mantissa and exponent.
528.It Cm C
529Treated as
530.Cm c
531with the
532.Cm l
533(ell) modifier.
534.It Cm c
535The
536.Vt int
537argument is converted to an
538.Vt "unsigned char" ,
539then to a
540.Vt wchar_t
541as if by
542.Xr btowc 3 ,
543and the resulting character is written.
544.Pp
545If the
546.Cm l
547(ell) modifier is used, the
548.Vt wint_t
549argument is converted to a
550.Vt wchar_t
551and written.
552.It Cm S
553Treated as
554.Cm s
555with the
556.Cm l
557(ell) modifier.
558.It Cm s
559The
560.Vt "char *"
561argument is expected to be a pointer to an array of character type (pointer
562to a string) containing a multibyte sequence.
563Characters from the array are converted to wide characters and written up to
564(but not including)
565a terminating
566.Dv NUL
567character;
568if a precision is specified, no more than the number specified are
569written.
570If a precision is given, no null character
571need be present; if the precision is not specified, or is greater than
572the size of the array, the array must contain a terminating
573.Dv NUL
574character.
575.Pp
576If the
577.Cm l
578(ell) modifier is used, the
579.Vt "wchar_t *"
580argument is expected to be a pointer to an array of wide characters
581(pointer to a wide string).
582Each wide character in the string
583is written.
584Wide characters from the array are written up to (but not including)
585a terminating wide
586.Dv NUL
587character;
588if a precision is specified, no more than the number specified are
589written (including shift sequences).
590If a precision is given, no null character
591need be present; if the precision is not specified, or is greater than
592the number of characters in
593the string, the array must contain a terminating wide
594.Dv NUL
595character.
596.It Cm p
597The
598.Vt "void *"
599pointer argument is printed in hexadecimal (as if by
600.Ql %#x
601or
602.Ql %#lx ) .
603.It Cm n
604The number of characters written so far is stored into the
605integer indicated by the
606.Vt "int *"
607(or variant) pointer argument.
608No argument is converted.
609.It Cm %
610A
611.Ql %
612is written.
613No argument is converted.
614The complete conversion specification
615is
616.Ql %% .
617.El
618.Pp
619The decimal point
620character is defined in the program's locale (category
621.Dv LC_NUMERIC ) .
622.Pp
623In no case does a non-existent or small field width cause truncation of
624a numeric field; if the result of a conversion is wider than the field
625width, the
626field is expanded to contain the conversion result.
627.Sh SECURITY CONSIDERATIONS
628Refer to
629.Xr printf 3 .
630.Sh SEE ALSO
631.Xr btowc 3 ,
632.Xr fputws 3 ,
633.Xr printf 3 ,
634.Xr putwc 3 ,
635.Xr setlocale 3 ,
636.Xr wcsrtombs 3 ,
637.Xr wprintf_l 3 ,
638.Xr wscanf 3
639.Sh STANDARDS
640Subject to the caveats noted in the
641.Sx BUGS
642section
643of
644.Xr printf 3 ,
645the
646.Fn wprintf ,
647.Fn fwprintf ,
648.Fn swprintf ,
649.Fn vwprintf ,
650.Fn vfwprintf ,
651and
652.Fn vswprintf
653functions
654conform to
655.St -isoC-99 .