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