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