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