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