Print this page
12953 convert printf(3c) and vprintf(3c) to mdoc
Change-Id: Iffbfe29bbd443e1a7ad0fc33fd7a7851428a3dc8


  28 .\" Common Development and Distribution License (the "License").
  29 .\" You may not use this file except in compliance with the License.
  30 .\"
  31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  32 .\" or http://www.opensolaris.org/os/licensing.
  33 .\" See the License for the specific language governing permissions
  34 .\" and limitations under the License.
  35 .\"
  36 .\" When distributing Covered Code, include this CDDL HEADER in each
  37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  38 .\" If applicable, add the following below this CDDL HEADER, with the
  39 .\" fields enclosed by brackets "[]" replaced with your own identifying
  40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
  41 .\"
  42 .\"
  43 .\" Copyright 1989 AT&T
  44 .\" Portions Copyright 1999, Forrest J. Cavalier III. All Rights Reserved.
  45 .\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
  46 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
  47 .\"
  48 .TH PRINTF 3C "Jan 7, 2009"
  49 .SH NAME
  50 printf, fprintf, sprintf, snprintf, asprintf \- print formatted output
  51 .SH SYNOPSIS
  52 .LP
  53 .nf
  54 #include <stdio.h>
  55 
  56 \fBint\fR \fBprintf\fR(\fBconst char *restrict\fR \fIformat\fR,
  57      \fB/*\fR \fIargs\fR*/ ...);
  58 .fi
  59 
  60 .LP
  61 .nf
  62 \fBint\fR \fBfprintf\fR(\fBFILE *restrict\fR \fIstream\fR, \fBconst char *restrict\fR \fIformat\fR,
  63      \fB/*\fR \fIargs\fR*/ ...);
  64 .fi
  65 
  66 .LP
  67 .nf
  68 \fBint\fR \fBsprintf\fR(\fBchar *restrict\fR \fIs\fR, \fBconst char *restrict\fR \fIformat\fR,
  69      \fB/*\fR \fIargs\fR*/ ...);
  70 .fi
  71 
  72 .LP
  73 .nf
  74 \fBint\fR \fBsnprintf\fR(\fBchar *restrict\fR \fIs\fR, \fBsize_t\fR \fIn\fR,
  75      \fBconst char *restrict\fR \fIformat\fR, \fB/*\fR \fIargs\fR*/ ...);
  76 .fi
  77 
  78 .LP
  79 .nf
  80 \fBint\fR \fBasprintf\fR(\fBchar **\fR \fIret\fR, \fBconst char *restrict\fR \fIformat\fR,
  81      \fB/*\fR \fIargs\fR*/ ...);
  82 .fi
  83 
  84 .SH DESCRIPTION
  85 .sp
  86 .LP
  87 The \fBprintf()\fR function places output on the standard output stream
  88 \fBstdout\fR.
  89 .sp
  90 .LP
  91 The \fBfprintf()\fR function places output on on the named output stream
  92 \fIstream\fR.
  93 .sp
  94 .LP
  95 The \fBsprintf()\fR function places output, followed by the null byte
  96 (\fB\e0\fR), in consecutive bytes starting at \fIs\fR; it is the user's
  97 responsibility to ensure that enough storage is available.
  98 .sp
  99 .LP
 100 The \fBsnprintf()\fR function is identical to \fBsprintf()\fR with the addition
 101 of the argument \fIn\fR, which specifies the size of the buffer referred to by
 102 \fIs\fR. If \fIn\fR is 0, nothing is written and \fIs\fR can be a null pointer.
 103 Otherwise, output bytes beyond the \fIn\fR-1st are discarded instead of being
 104 written to the array and a null byte is written at the end of the bytes
 105 actually written into the array.
 106 .sp
 107 .LP
 108 The \fBasprintf()\fR function is the same as the \fBsprintf()\fR function
 109 except that it returns, in the \fIret\fR argument, a pointer to a buffer
 110 sufficiently large to hold the output string. This pointer should be passed to
 111 \fBfree\fR(3C) to release the allocated storage when it is no longer needed. If
 112 sufficient space cannot be allocated, the \fBasprintf()\fR function returns -1
 113 and sets \fIret\fR to be a \fINULL\fR pointer.
 114 .sp
 115 .LP

































 116 Each of these functions converts, formats, and prints its arguments under
 117 control of the \fIformat\fR. The \fIformat\fR is a character string, beginning
 118 and ending in its initial shift state, if any. The \fIformat\fR is composed of
 119 zero or more directives: \fBordinary characters\fR, which are simply copied to
 120 the output stream and \fBconversion specifications\fR, each of which results in
 121 the fetching of zero or more arguments. The results are undefined if there are
 122 insufficient arguments for the \fIformat\fR. If the \fIformat\fR is exhausted
 123 while arguments remain, the excess arguments are evaluated but are otherwise
 124 ignored.
 125 .sp
 126 .LP
 127 Conversions can be applied to the \fIn\fRth argument after the \fIformat\fR in
 128 the argument list, rather than to the next unused argument. In this case, the
 129 conversion specifier \fB%\fR (see below) is replaced by the sequence
 130 \fB%\fR\fIn\fR\fB$\fR, where \fIn\fR is a decimal integer in the range [1,
 131 \fBNL_ARGMAX\fR], giving the position of the argument in the argument list.

















 132 This feature provides for the definition of format strings that select
 133 arguments in an order appropriate to specific languages (see the \fBEXAMPLES\fR

 134 section).
 135 .sp
 136 .LP
 137 In format strings containing the \fB%\fR\fIn\fR\fB$\fR form of conversion
 138 specifications, numbered arguments in the argument list can be referenced from
 139 the format string as many times as required.
 140 .sp
 141 .LP
 142 In format strings containing the \fB%\fR form of conversion specifications,
 143 each argument in the argument list is used exactly once.
 144 .sp
 145 .LP
 146 All forms of the \fBprintf()\fR functions allow for the insertion of a
 147 language-dependent radix character in the output string. The radix character is
 148 defined by the program's locale (category \fBLC_NUMERIC\fR). In the POSIX
 149 locale, or in a locale where the radix character is not defined, the radix
 150 character defaults to a period (\fB\&.\fR).
 151 .SS "Conversion Specifications"
 152 .sp
 153 .LP
 154 Each conversion specification is introduced by the \fB%\fR character or by the
 155 character sequence \fB%\fR\fIn\fR\fB$\fR, after which the following appear in
 156 sequence:
 157 .RS +4
 158 .TP
 159 .ie t \(bu
 160 .el o
 161 An optional field, consisting of a decimal digit string followed by a \fB$\fR,
 162 specifying the next argument to be converted. If this field is not provided,
 163 the \fIargs\fR following the last argument converted will be used.
 164 .RE
 165 .RS +4
 166 .TP
 167 .ie t \(bu
 168 .el o
 169 Zero or more \fIflags\fR (in any order), which modify the meaning of the

 170 conversion specification.
 171 .RE
 172 .RS +4
 173 .TP
 174 .ie t \(bu
 175 .el o
 176 An optional minimum \fIfield width\fR. If the converted value has fewer bytes
 177 than the field width, it will be padded with spaces by default on the left; it
 178 will be padded on the right, if the left-adjustment flag (\fB\(hy\fR),
 179 described below, is given to the field width. The field width takes the form of
 180 an asterisk (*), described below, or a decimal integer.
 181 .sp
 182 If the conversion specifier is \fBs\fR, a standard-conforming application (see
 183 \fBstandards\fR(5)) interprets the field width as the minimum number of bytes
 184 to be printed; an application that is not standard-conforming interprets the
 185 field width as the minimum number of columns of screen display. For an
 186 application that is not standard-conforming, \fB%10s\fR means if the converted
 187 value has a screen width of 7 columns, 3 spaces would be padded on the right.
 188 .sp
 189 If the format is \fB%ws\fR, then the field width should be interpreted as the
 190 minimum number of columns of screen display.
 191 .RE
 192 .RS +4
 193 .TP
 194 .ie t \(bu
 195 .el o
 196 An optional \fIprecision\fR that gives the minimum number of digits to appear
 197 for the \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, and \fBX\fR conversions
 198 (the field is padded with leading zeros); the number of digits to appear after
 199 the radix character for the \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, and
 200 \fBF\fR conversions, the maximum number of significant digits for the \fBg\fR
 201 and \fBG\fR conversions; or the maximum number of bytes to be printed from a
 202 string in \fBs\fR and \fBS\fR conversions. The precision takes the form of a
 203 period (.) followed either by an asterisk (*), described below, or an optional
 204 decimal digit string, where a null digit string is treated as 0. If a precision
 205 appears with any other conversion specifier, the behavior is undefined.
 206 .sp
 207 If the conversion specifier is \fBs\fR or \fBS\fR, a standard-conforming
 208 application (see \fBstandards\fR(5)) interprets the precision as the maximum
 209 number of bytes to be written; an application that is not standard-conforming
 210 interprets the precision as the maximum number of columns of screen display.
 211 For an application that is not standard-conforming, \fB%.5s\fR would print only
 212 the portion of the string that would display in 5 screen columns. Only complete
 213 characters are written.
 214 .sp
 215 For \fB%ws\fR, the precision should be interpreted as the maximum number of
 216 columns of screen display. The precision takes the form of a period (\fB\&.\fR)
































 217 followed by a decimal digit string; a null digit string is treated as zero.
 218 Padding specified by the precision overrides the padding specified by the field
 219 width.
 220 .RE
 221 .RS +4
 222 .TP
 223 .ie t \(bu
 224 .el o
 225 An optional \fIlength modifier\fR that specified the size of the argument.
 226 .RE
 227 .RS +4
 228 .TP
 229 .ie t \(bu
 230 .el o
 231 A \fIconversion specifier\fR that indicates the type of conversion to be
 232 applied.
 233 .RE
 234 .sp
 235 .LP
 236 A field width, or precision, or both can be indicated by an asterisk
 237 (\fB*\fR) . In this case, an argument of type \fBint\fR supplies the field width or
 238 precision. Arguments specifying field width, or precision, or both must appear
 239 in that order before the argument, if any, to be converted. A negative field
 240 width is taken as a \(mi flag followed by a positive field width. A negative
 241 precision is taken as if the precision were omitted. In format strings
 242 containing the \fB%\fR\fIn\fR\fB$\fR form of a conversion specification, a
 243 field width or precision may be indicated by the sequence
 244 \fB*\fR\fIm\fR\fB$\fR, where \fIm\fR is a decimal integer in the range [1,
 245 \fBNL_ARGMAX\fR] giving the position in the argument list (after the format
 246 argument) of an integer argument containing the field width or precision, for
 247 example:
 248 .sp
 249 .in +2
 250 .nf
 251 printf("%1$d:%2$.*3$d:%4$.*3$d\en", hour, min, precision, sec);
 252 .fi
 253 .in -2
 254 
 255 .sp
 256 .LP
 257 The \fIformat\fR can contain either numbered argument specifications (that is,
 258 \fB%\fR\fIn\fR\fB$\fR and \fB*\fR\fIm\fR\fB$\fR), or unnumbered argument
 259 specifications (that is, \fB%\fR and \fB*\fR), but normally not both. The only
 260 exception to this is that \fB%%\fR can be mixed with the \fB%\fR\fIn\fR\fB$\fR
 261 form. The results of mixing numbered and unnumbered argument specifications in
 262 a \fIformat\fR string are undefined. When numbered argument specifications are
 263 used, specifying the \fIN\fRth argument requires that all the leading
 264 arguments, from the first to the (\fIN-1\fR)th, are specified in the format
 265 string.
 266 .SS "Flag Characters"
 267 .sp
 268 .LP

















 269 The flag characters and their meanings are:
 270 .sp
 271 .ne 2
 272 .na
 273 \fB\fB\&'\fR\fR
 274 .ad
 275 .RS 9n
 276 The integer portion of the result of a decimal conversion (\fB%i\fR, \fB%d\fR,
 277 \fB%u\fR, \fB%f\fR, \fB%F\fR, \fB%g\fR, or \fB%G\fR) will be formatted with
 278 thousands' grouping characters. For other conversions the behavior is
 279 undefined. The non-monetary grouping character is used.
 280 .RE
 281 
 282 .sp
 283 .ne 2
 284 .na
 285 \fB\fB\(mi\fR\fR
 286 .ad
 287 .RS 9n
 288 The result of the conversion will be left-justified within the field. The
 289 conversion will be right-justified if this flag is not specified.
 290 .RE
 291 
 292 .sp
 293 .ne 2
 294 .na
 295 \fB\fB+\fR\fR
 296 .ad
 297 .RS 9n
 298 The result of a signed conversion will always begin with a sign (+ or -). The
 299 conversion will begin with a sign only when a negative value is converted if
 300 this flag is not specified.
 301 .RE
 302 
 303 .sp
 304 .ne 2
 305 .na
 306 \fB\fBspace\fR\fR
 307 .ad
 308 .RS 9n
 309 If the first character of a signed conversion is not a sign or if a signed
 310 conversion results in no characters, a space will be placed before the result.
 311 This means that if the \fBspace\fR and \fB+\fR flags both appear, the space
 312 flag will be ignored.
 313 .RE
 314 
 315 .sp
 316 .ne 2
 317 .na
 318 \fB\fB#\fR\fR
 319 .ad
 320 .RS 9n
 321 The value is to be converted to an alternate form. For \fBc\fR, \fBd\fR,
 322 \fBi\fR, \fBs\fR, and \fBu\fR conversions, the flag has no effect. For an
 323 \fBo\fR conversion, it increases the precision (if necessary) to force the
 324 first digit of the result to be a zero. For \fBx\fR or \fBX\fR conversion, a
 325 non-zero result will have \fB0x\fR (or \fB0X\fR) prepended to it. For \fBa\fR,
 326 \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, and \fBG\fR conversions,
 327 the result will always contain a radix character, even if no digits follow the
 328 radix character. Without this flag, the radix character appears in the result
 329 of these conversions only if a digit follows it. For \fBg\fR and \fBG\fR















 330 conversions, trailing zeros will not be removed from the result as they
 331 normally are.
 332 .RE
 333 
 334 .sp
 335 .ne 2
 336 .na
 337 \fB\fB0\fR\fR
 338 .ad
 339 .RS 9n
 340 For \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, \fBX\fR, \fBa\fR, \fBA\fR,
 341 \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, and \fBG\fR conversions, leading
 342 zeros (following any indication of sign or base) are used to pad to the field
 343 width; no space padding is performed. If the \fB0\fR and \fB\(mi\fR flags both
 344 appear, the \fB0\fR flag will be ignored. For \fBd\fR, \fBi\fR, \fBo\fR,
 345 \fBu\fR, \fBx\fR, and \fBX\fR conversions, if a precision is specified, the
 346 \fB0\fR flag will be ignored. If the \fB0\fR and \fB\&'\fR flags both appear,
 347 the grouping characters are inserted before zero padding. For other
 348 conversions, the behavior is undefined.
 349 .RE
 350 
 351 .SS "Length Modifiers"
 352 .sp
 353 .LP







 354 The length modifiers and their meanings are:
 355 .sp
 356 .ne 2
 357 .na
 358 \fB\fBhh\fR\fR
 359 .ad
 360 .RS 16n
 361 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
 362 \fBX\fR conversion specifier applies to a \fBsigned char\fR or \fBunsigned
 363 char\fR argument (the argument will have been promoted according to the integer
 364 promotions, but its value will be converted to \fBsigned char\fR or \fBunsigned
 365 char\fR before printing); or that a following \fBn\fR conversion specifier
 366 applies to a pointer to a \fBsigned char\fR argument.
 367 .RE
 368 
 369 .sp
 370 .ne 2
 371 .na
 372 \fB\fBh\fR\fR
 373 .ad
 374 .RS 16n
 375 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
 376 \fBX\fR conversion specifier applies to a \fBshort\fR or \fBunsigned short\fR
 377 argument (the argument will have been promoted according to the integer
 378 promotions, but its value will be converted to \fBshort\fR or \fBunsigned
 379 short\fR before printing); or that a following \fBn\fR conversion specifier
 380 applies to a pointer to a \fBshort\fR argument.
 381 .RE
 382 
 383 .sp
 384 .ne 2
 385 .na
 386 \fB\fBl (ell)\fR\fR
 387 .ad
 388 .RS 16n
 389 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
 390 \fBX\fR conversion specifier applies to a \fBlong\fR or \fBunsigned long\fR
 391 argument; that a following \fBn\fR conversion specifier applies to a pointer to
 392 a \fBlong\fR argument; that a following \fBc\fR conversion specifier applies to
 393 a \fBwint_t\fR argument; that a following \fBs\fR conversion specifier applies
 394 to a pointer to a \fBwchar_t\fR argument; or has no effect on a following
 395 \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, or \fBG\fR



































 396 conversion specifier.
 397 .RE
 398 
 399 .sp
 400 .ne 2
 401 .na
 402 \fB\fBll (ell-ell)\fR\fR
 403 .ad
 404 .RS 16n
 405 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
 406 \fBX\fR conversion specifier applies to a \fBlong long\fR or \fBunsigned long
 407 long\fR argument; or that a following \fBn\fR conversion specifier applies to a
 408 pointer to a \fBlong long\fR argument.
 409 .RE
 410 
 411 .sp
 412 .ne 2
 413 .na
 414 \fB\fBj\fR\fR
 415 .ad
 416 .RS 16n
 417 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
 418 \fBX\fR conversion specifier applies to an \fBintmax_t\fR or \fBuintmax_t\fR
 419 argument; or that a following \fBn\fR conversion specifier applies to a pointer
 420 to an \fBintmax_t\fR argument. See NOTES.
 421 .RE
 422 
 423 .sp
 424 .ne 2
 425 .na
 426 \fB\fBz\fR\fR
 427 .ad
 428 .RS 16n
 429 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
 430 \fBX\fR conversion specifier applies to a \fBsize_t\fR or the corresponding
 431 signed integer type argument; or that a following \fBn\fR conversion specifier
 432 applies to a pointer to a signed integer type corresponding to \fBsize_t\fR
 433 argument.
 434 .RE
 435 
 436 .sp
 437 .ne 2
 438 .na
 439 \fB\fBt\fR\fR
 440 .ad
 441 .RS 16n
 442 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
 443 \fBX\fR conversion specifier applies to a \fBptrdiff_t\fR or the corresponding
 444 unsigned type argument; or that a following n conversion specifier applies to a
 445 pointer to a \fBptrdiff_t\fR argument.
 446 .RE
 447 
 448 .sp
 449 .ne 2
 450 .na
 451 \fB\fBL\fR\fR
 452 .ad
 453 .RS 16n
 454 Specifies that a following \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR,
 455 \fBF\fR, \fBg\fR, or \fBG\fR conversion specifier applies to a \fBlong
 456 double\fR argument.
 457 .RE
 458 
 459 .sp
 460 .LP
























 461 If a length modifier appears with any conversion specifier other than as
 462 specified above, the behavior is undefined.
 463 .SS "Conversion Specifiers"
 464 .sp
 465 .LP
 466 Each conversion specifier results in fetching zero or more arguments. The
 467 results are undefined if there are insufficient arguments for the format. If
 468 the format is exhausted while arguments remain, the excess arguments are
 469 ignored.
 470 .sp
 471 .LP
 472 The conversion specifiers and their meanings are:
 473 .sp
 474 .ne 2
 475 .na
 476 \fB\fBd\fR, \fBi\fR\fR
 477 .ad
 478 .RS 8n
 479 The \fBint\fR argument is converted to a signed decimal in the style
 480 \fB[\fR\(mi\fB]\fR\fIdddd\fR. The precision specifies the minimum number of
 481 digits to appear; if the value being converted can be represented in fewer
 482 digits, it will be expanded with leading zeros. The default precision is 1. The
 483 result of converting 0 with an explicit precision of 0 is no characters.
 484 .RE
 485 
 486 .sp
 487 .ne 2
 488 .na
 489 \fB\fBo\fR\fR
 490 .ad
 491 .RS 8n
 492 The \fBunsigned int\fR argument is converted to unsigned octal format in the
 493 style \fIdddd\fR. The precision specifies the minimum number of digits to
 494 appear; if the value being converted can be represented in fewer digits, it
 495 will be expanded with leading zeros. The default precision is 1. The result of
 496 converting 0 with an explicit precision of 0 is no characters.
 497 .RE
 498 
 499 .sp
 500 .ne 2
 501 .na
 502 \fB\fBu\fR\fR
 503 .ad
 504 .RS 8n
 505 The \fBunsigned int\fR argument is converted to unsigned decimal format in the
 506 style \fIdddd\fR. The precision specifies the minimum number of digits to
 507 appear; if the value being converted can be represented in fewer digits, it
 508 will be expanded with leading zeros. The default precision is 1. The result of
 509 converting 0 with an explicit precision of 0 is no characters.
 510 .RE
 511 
 512 .sp
 513 .ne 2
 514 .na
 515 \fB\fBx\fR\fR
 516 .ad
 517 .RS 8n
 518 The \fBunsigned int\fR argument is converted to unsigned hexadecimal format in
 519 the style \fIdddd\fR; the letters \fBabcdef\fR are used. The precision
 520 specifies the minimum number of digits to appear; if the value being converted
 521 can be represented in fewer digits, it will be expanded with leading zeros. The
 522 default precision is 1. The result of converting 0 with an explicit precision
 523 of 0 is no characters.
 524 .RE
 525 
 526 .sp
 527 .ne 2
 528 .na
 529 \fB\fBX\fR\fR
 530 .ad
 531 .RS 8n
 532 Behaves the same as the \fBx\fR conversion specifier except that letters
 533 \fBABCDEF\fR are used instead of \fBabcdef\fR.
 534 .RE
 535 
 536 .sp
 537 .ne 2
 538 .na
 539 \fB\fBf\fR, \fBF\fR\fR
 540 .ad
 541 .RS 8n
 542 The \fBdouble\fR argument is converted to decimal notation in the style
 543 [\fB\(mi\fR]\fIddd\fR\fB\&.\fR\fIddd\fR, where the number of digits after the
 544 radix character (see \fBsetlocale\fR(3C)) is equal to the precision
 545 specification. If the precision is missing it is taken as 6; if the precision
 546 is explicitly 0 and the \fB#\fR flag is not specified, no radix character
 547 appears. If a radix character appears, at least 1 digit appears before it. The
 548 converted value is rounded to fit the specified output format according to the
 549 prevailing floating point rounding direction mode. If the conversion is not
 550 exact, an inexact exception is raised.
 551 .sp
 552 For the \fBf\fR specifier, a double argument representing an infinity or NaN is
 553 converted in the style of the \fBe\fR conversion specifier, except that for an
 554 infinite argument, "infinity" or "Infinity" is printed when the precision is at
 555 least 8 and "inf" or "Inf" is printed otherwise.
 556 .sp
 557 For the F specifier, a double argument representing an infinity or NaN is
 558 converted in the SUSv3 style of the E conversion specifier, except that for an
 559 infinite argument, "INFINITY" is printed when the precision is at least 8 and
 560 or "INF" is printed otherwise.
 561 .RE
 562 
 563 .sp
 564 .ne 2
 565 .na
 566 \fB\fBe\fR, \fBE\fR\fR
 567 .ad
 568 .RS 8n
 569 The \fBdouble\fR argument is converted to the style
 570 [\fB\(mi\fR]\fId\fR\fB\&.\fR\fIddd\fR\fBe\fR\fI\(+-dd\fR, where there is one
 571 digit before the radix character (which is non-zero if the argument is
 572 non-zero) and the number of digits after it is equal to the precision. When the
 573 precision is missing it is taken as 6; if the precision is 0 and the \fB#\fR
 574 flag is not specified, no radix character appears. The \fBE\fR conversion
 575 specifier will produce a number with \fBE\fR instead of \fBe\fR introducing the
 576 exponent. The exponent always contains at least two digits. The converted value
 577 is rounded to fit the specified output format according to the prevailing
 578 floating point rounding direction mode. If the conversion is not exact, an
 579 inexact exception is raised.
 580 .sp










 581 Infinity and NaN values are handled in one of the following ways:
 582 .sp
 583 .ne 2
 584 .na
 585 \fBSUSv3\fR
 586 .ad
 587 .RS 11n
 588 For the \fBe\fR specifier, a \fBdouble\fR argument representing an infinity is
 589 printed as "[\(mi]\fBinfinity\fR", when the precision for the conversion is at
 590 least 7 and as "[\(mi]\fBinf\fR" otherwise. A \fBdouble\fR argument
 591 representing a NaN is printed as "[\(mi]\fBnan\fR". For the \fBE\fR specifier,
 592 "\fBINF\fR", "\fBINFINITY\fR", and "\fBNAN\fR" are printed instead of
 593 "\fBinf\fR", "\fBinfinity\fR", and "\fBnan\fR", respectively. Printing of the
 594 sign follows the rules described above.
 595 .RE
 596 
 597 .sp
 598 .ne 2
 599 .na
 600 \fBDefault\fR
 601 .ad
 602 .RS 11n
 603 A \fBdouble\fR argument representing an infinity is printed as
 604 "[\(mi]\fBInfinity\fR", when the precision for the conversion is at least 7 and
 605 as "[\(mi]\fBInf\fR" otherwise. A double argument representing a NaN is printed
 606 as "[\(mi]\fBNaN\fR". Printing of the sign follows the rules described above.
 607 .RE
 608 
 609 .RE
 610 
 611 .sp
 612 .ne 2
 613 .na
 614 \fB\fBg\fR, \fBG\fR\fR
 615 .ad
 616 .RS 8n
 617 The \fBdouble\fR argument is printed in style \fBf\fR or \fBe\fR (or in style
 618 \fBE\fR in the case of a \fBG\fR conversion specifier), with the precision
 619 specifying the number of significant digits. If an explicit precision is 0, it
 620 is taken as 1. The style used depends on the value converted: style \fBe\fR (or
 621 \fBE\fR) will be used only if the exponent resulting from the conversion is
 622 less than -4 or greater than or equal to the precision. Trailing zeros are
 623 removed from the fractional part of the result. A radix character appears only
 624 if it is followed by a digit.
 625 .sp
 626 A \fBdouble\fR argument representing an infinity or NaN is converted in the
 627 style of the \fBe\fR or \fBE\fR conversion specifier, except that for an
 628 infinite argument, "infinity", "INFINITY", or "Infinity" is printed when the
 629 precision is at least 8 and "inf", "INF", or "Inf" is printed otherwise.
 630 .RE
 631 
 632 .sp
 633 .ne 2
 634 .na
 635 \fB\fBa\fR, \fBA\fR\fR
 636 .ad
 637 .RS 8n
 638 A \fBdouble\fR argument representing a floating-point number is converted in
 639 the style "[-]0\fIxh\fR.\fIhhhhp\fR\(+-\fId\fR", where the single hexadecimal
 640 digit preceding the radix point is 0 if the value converted is zero and 1
 641 otherwise and the number of hexadecimal digits after it is equal to the
 642 precision; if the precision is missing, the number of digits printed after the
 643 radix point is 13 for the conversion of a double value, 16 for the conversion
 644 of a long double value on x86, and 28 for the conversion of a long double value
 645 on SPARC; if the precision is zero and the '#' flag is not specified, no
 646 decimal-point character will appear. The letters "\fBabcdef\fR" are used for
 647 \fBa\fR conversion and the letters "\fBABCDEF\fR" for \fBA\fR conversion. The
 648 \fBA\fR conversion specifier produces a number with '\fBX\fR' and '\fBP\fR'
 649 instead of '\fBx\fR' and '\fBp\fR'. The exponent will always contain at least
 650 one digit, and only as many more digits as necessary to represent the decimal
 651 exponent of 2. If the value is zero, the exponent is zero.
 652 .sp



















































 653 The converted value is rounded to fit the specified output format according to
 654 the prevailing floating point rounding direction mode. If the conversion is not
 655 exact, an inexact exception is raised.
 656 .sp
 657 A \fBdouble\fR argument representing an infinity or NaN is converted in the
 658 SUSv3 style of an \fBe\fR or \fBE\fR conversion specifier.
 659 .RE
 660 
 661 .sp
 662 .ne 2
 663 .na
 664 \fB\fBc\fR\fR
 665 .ad
 666 .RS 8n
 667 The \fBint\fR argument is converted to an \fBunsigned char\fR, and the
 668 resulting byte is printed.
 669 .sp
 670 If an \fBl\fR (ell) qualifier is present, the \fBwint_t\fR argument is
 671 converted as if by an \fBls\fR conversion specification with no precision and
 672 an argument that points to a two-element array of type \fBwchar_t\fR, the first
 673 element of which contains the \fBwint_t\fR argument to the \fBls\fR conversion
 674 specification and the second element contains a null wide-character.
 675 .RE
 676 
 677 .sp
 678 .ne 2
 679 .na
 680 \fB\fBC\fR\fR
 681 .ad
 682 .RS 8n
 683 Same as \fBlc\fR.
 684 .RE
 685 
 686 .sp
 687 .ne 2
 688 .na
 689 \fB\fBwc\fR\fR
 690 .ad
 691 .RS 8n
 692 The \fBint\fR argument is converted to a wide character (\fBwchar_t\fR), and
 693 the resulting wide character is printed.
 694 .RE
 695 
 696 .sp
 697 .ne 2
 698 .na
 699 \fB\fBs\fR\fR
 700 .ad
 701 .RS 8n
 702 The argument must be a pointer to an array of \fBchar\fR. Bytes from the array
 703 are written up to (but not including) any terminating null byte. If a precision
 704 is specified, a standard-conforming application (see \fBstandards\fR(5)) will
 705 write only the number of bytes specified by precision; an application that is
 706 not standard-conforming will write only the portion of the string that will
 707 display in the number of columns of screen display specified by precision. If
 708 the precision is not specified, it is taken to be infinite, so all bytes up to
 709 the first null byte are printed. An argument with a null value will yield
 710 undefined results.
 711 .sp
 712 If an \fBl\fR (ell) qualifier is present, the argument must be a pointer to an
 713 array of type \fBwchar_t\fR. Wide-characters from the array are converted to
 714 characters (each as if by a call to the \fBwcrtomb\fR(3C) function, with the
 715 conversion state described by an \fBmbstate_t\fR object initialized to zero
 716 before the first wide-character is converted) up to and including a terminating
 717 null wide-character. The resulting characters are written up to (but not
 718 including) the terminating null character (byte). If no precision is specified,
 719 the array must contain a null wide-character. If a precision is specified, no
 720 more than that many characters (bytes) are written (including shift sequences,
 721 if any), and the array must contain a null wide-character if, to equal the
 722 character sequence length given by the precision, the function would need to
 723 access a wide-character one past the end of the array. In no case is a partial
 724 character written.
 725 .RE
 726 
 727 .sp
 728 .ne 2
 729 .na
 730 \fB\fBS\fR\fR
 731 .ad
 732 .RS 8n
 733 Same as \fBls\fR.
 734 .RE
 735 
 736 .sp
 737 .ne 2
 738 .na
 739 \fB\fBws\fR\fR
 740 .ad
 741 .RS 8n
 742 The argument must be a pointer to an array of \fBwchar_t\fR. Bytes from the
 743 array are written up to (but not including) any terminating null character. If
 744 the precision is specified, only that portion of the wide-character array that
 745 will display in the number of columns of screen display specified by precision
 746 will be written. If the precision is not specified, it is taken to be infinite,
 747 so all wide characters up to the first null character are printed. An argument
 748 with a null value will yield undefined results.
 749 .RE
 750 
 751 .sp
 752 .ne 2
 753 .na
 754 \fB\fBp\fR\fR
 755 .ad
 756 .RS 8n
 757 The argument must be a pointer to \fBvoid\fR. The value of the pointer is
 758 converted to a set of sequences of printable characters, which should be the
 759 same as the set of sequences that are matched by the \fB%p\fR conversion of the
 760 \fBscanf\fR(3C) function.
 761 .RE
 762 
 763 .sp
 764 .ne 2
 765 .na
 766 \fB\fBn\fR\fR
 767 .ad
 768 .RS 8n
 769 The argument must be a pointer to an integer into which is written the number
 770 of bytes written to the output standard I/O stream so far by this call to one
 771 of the \fBprintf()\fR functions. No argument is converted.
 772 .RE
 773 
 774 .sp
 775 .ne 2
 776 .na
 777 \fB\fB%\fR\fR
 778 .ad
 779 .RS 8n
 780 Print a \fB%\fR; no argument is converted. The entire conversion specification
 781 must be %%.
 782 .RE
 783 
 784 .sp
 785 .LP
 786 If a conversion specification does not match one of the above forms, the
 787 behavior is undefined.
 788 .sp
 789 .LP
 790 In no case does a non-existent or small field width cause truncation of a
 791 field; if the result of a conversion is wider than the field width, the field
 792 is simply expanded to contain the conversion result. Characters generated by
 793 \fBprintf()\fR and \fBfprintf()\fR are printed as if the \fBputc\fR(3C)





 794 function had been called.
 795 .sp
 796 .LP
 797 The \fBst_ctime\fR and \fBst_mtime\fR fields of the file will be marked for
 798 update between the call to a successful execution of \fBprintf()\fR or
 799 \fBfprintf()\fR and the next successful completion of a call to
 800 \fBfflush\fR(3C) or \fBfclose\fR(3C) on the same stream or a call to
 801 \fBexit\fR(3C) or \fBabort\fR(3C).
 802 .SH RETURN VALUES
 803 .sp
 804 .LP
 805 The \fBprintf()\fR, \fBfprintf()\fR, \fBsprintf()\fR, and \fBasprintf()\fR














 806 functions return the number of bytes transmitted (excluding the terminating
 807 null byte in the case of \fBsprintf()\fR and \fBasprintf()\fR).
 808 .sp
 809 .LP
 810 The \fBsnprintf()\fR function returns the number of bytes that would have been
 811 written to \fIs\fR if \fIn\fR had been sufficiently large (excluding the
 812 terminating null byte.) If the value of \fIn\fR is 0 on a call to
 813 \fBsnprintf()\fR, \fIs\fR can be a null pointer and the number of bytes that
 814 would have been written if \fIn\fR had been sufficiently large (excluding the
 815 terminating null byte) is returned.
 816 .sp
 817 .LP










 818 Each function returns a negative value if an output error was encountered.
 819 .SH ERRORS
 820 .sp
 821 .LP
 822 For the conditions under which \fBprintf()\fR and \fBfprintf()\fR will fail and
 823 may fail, refer to \fBfputc\fR(3C) or \fBfputwc\fR(3C).
 824 .sp
 825 .LP
 826 The \fBsnprintf()\fR function will fail if:
 827 .sp
 828 .ne 2
 829 .na
 830 \fB\fBEOVERFLOW\fR\fR
 831 .ad
 832 .RS 13n
 833 The value of \fIn\fR is greater than \fBINT_MAX\fR or the number of bytes
 834 needed to hold the output excluding the terminating null is greater than
 835 \fBINT_MAX\fR.
 836 .RE
 837 
 838 .sp
 839 .LP
 840 The \fBprintf()\fR, \fBfprintf()\fR, \fBsprintf()\fR, and \fBsnprintf()\fR
 841 functions may fail if:
 842 .sp
 843 .ne 2
 844 .na
 845 \fB\fBEILSEQ\fR\fR
 846 .ad
 847 .RS 10n
 848 A wide-character code that does not correspond to a valid character has been
 849 detected.
 850 .RE
 851 
 852 .sp
 853 .ne 2
 854 .na
 855 \fB\fBEINVAL\fR\fR
 856 .ad
 857 .RS 10n
 858 There are insufficient arguments.
 859 .RE
 860 
 861 .sp
 862 .LP
 863 The \fBprintf()\fR, \fBfprintf()\fR, and \fBasprintf()\fR functions may fail
 864 due to an underlying \fBmalloc\fR(3C) failure if:
 865 .sp
 866 .ne 2
 867 .na
 868 \fB\fBEAGAIN\fR\fR
 869 .ad
 870 .RS 10n
 871 Storage space is temporarily unavailable.
 872 .RE
 873 
 874 .sp
 875 .ne 2
 876 .na
 877 \fB\fBENOMEM\fR\fR
 878 .ad
 879 .RS 10n
 880 Insufficient storage space is available.
 881 .RE
 882 
 883 .SH USAGE
 884 .sp
 885 .LP
 886 If the application calling the \fBprintf()\fR functions has any objects of type
 887 \fBwint_t\fR or \fBwchar_t\fR, it must also include the header \fB<wchar.h>\fR
 888 to have these objects defined.
 889 .SS "Escape Character Sequences"
 890 .sp
 891 .LP
 892 It is common to use the following escape sequences built into the C language
 893 when entering format strings for the \fBprintf()\fR functions, but these
 894 sequences are processed by the C compiler, not by the \fBprintf()\fR function.
 895 .sp
 896 .ne 2
 897 .na
 898 \fB\fB\ea\fR\fR
 899 .ad
 900 .RS 7n
 901 Alert. Ring the bell.
 902 .RE
 903 
 904 .sp
 905 .ne 2
 906 .na
 907 \fB\fB\eb\fR\fR
 908 .ad
 909 .RS 7n
 910 Backspace. Move the printing position to one character before the current
 911 position, unless the current position is the start of a line.
 912 .RE
 913 
 914 .sp
 915 .ne 2
 916 .na
 917 \fB\fB\ef\fR\fR
 918 .ad
 919 .RS 7n
 920 Form feed. Move the printing position to the initial printing position of the
 921 next logical page.
 922 .RE
 923 
 924 .sp
 925 .ne 2
 926 .na
 927 \fB\fB\en\fR\fR
 928 .ad
 929 .RS 7n
 930 Newline. Move the printing position to the start of the next line.
 931 .RE
 932 
 933 .sp
 934 .ne 2
 935 .na
 936 \fB\fB\er\fR\fR
 937 .ad
 938 .RS 7n
 939 Carriage return. Move the printing position to the start of the current line.
 940 .RE
 941 
 942 .sp
 943 .ne 2
 944 .na
 945 \fB\fB\et\fR\fR
 946 .ad
 947 .RS 7n
 948 Horizontal tab. Move the printing position to the next implementation-defined
 949 horizontal tab position on the current line.
 950 .RE
 951 
 952 .sp
 953 .ne 2
 954 .na
 955 \fB\fB\ev\fR\fR
 956 .ad
 957 .RS 7n
 958 Vertical tab. Move the printing position to the start of the next
 959 implementation-defined vertical tab position.
 960 .RE
 961 
 962 .sp
 963 .LP
 964 In addition, the C language supports character sequences of the form
 965 .sp
 966 .LP
 967 \eoctal-number
 968 .sp
 969 .LP
 970 and
 971 .sp
 972 .LP
 973 \ehex-number
 974 .sp
 975 .LP
 976 which translates into the character represented by the octal or hexadecimal
 977 number. For example, if ASCII representations are being used, the letter 'a'
 978 may be written as '\e141' and 'Z' as '\e132'. This syntax is most frequently
 979 used to represent the null character as '\e0'. This is exactly equivalent to
 980 the numeric constant zero (0). Note that the octal number does not include the
 981 zero prefix as it would for a normal octal constant. To specify a hexadecimal
 982 number, omit the zero so that the prefix is an 'x' (uppercase 'X' is not
 983 allowed in this context). Support for hexadecimal sequences is an ANSI
 984 extension. See \fBstandards\fR(5).
 985 .SH EXAMPLES
 986 .LP
 987 \fBExample 1 \fRTo print the language-independent date and time format, the








 988 following statement could be used:
 989 .sp
 990 .in +2
 991 .nf
 992 \fBprintf (format, weekday, month, day, hour, min);\fR
 993 .fi
 994 .in -2
 995 
 996 .sp
 997 .LP
 998 For American usage, \fIformat\fR could be a pointer to the string:
 999 
1000 .sp
1001 .in +2
1002 .nf
1003 \fB"%s, %s %d, %d:%.2d\en"\fR
1004 .fi
1005 .in -2
1006 
1007 .sp
1008 .LP
1009 producing the message:
1010 
1011 .sp
1012 .in +2
1013 .nf
1014 \fBSunday, July 3, 10:02\fR
1015 .fi
1016 .in -2
1017 
1018 .sp
1019 .LP
1020 whereas for German usage, \fIformat\fR could be a pointer to the string:
1021 
1022 .sp
1023 .in +2
1024 .nf
1025 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
1026 .fi
1027 .in -2
1028 
1029 .sp
1030 .LP
1031 producing the message:
1032 
1033 .sp
1034 .in +2
1035 .nf
1036 Sonntag, 3. Juli, 10:02
1037 .fi
1038 .in -2
1039 
1040 .LP
1041 \fBExample 2 \fRTo print a date and time in the form \fBSunday, July 3,
1042 10:02\fR, where \fBweekday\fR and \fBmonth\fR are pointers to null-terminated
1043 strings:
1044 .sp
1045 .in +2
1046 .nf
1047 printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min);
1048 .fi
1049 .in -2
1050 
1051 .LP
1052 \fBExample 3 \fRTo print pi to 5 decimal places:
1053 .sp
1054 .in +2
1055 .nf
1056 printf("pi = %.5f", 4 * atan(1.0));
1057 .fi
1058 .in -2
1059 
1060 .SS "Default"
1061 .LP
1062 \fBExample 4 \fRThe following example applies only to applications that are not
1063 standard-conforming. To print a list of names in columns which are 20
1064 characters wide:
1065 .sp
1066 .in +2
1067 .nf
1068 \fBprintf("%20s%20s%20s", lastname, firstname, middlename);\fR
1069 .fi
1070 .in -2
1071 
1072 .SH ATTRIBUTES
1073 .sp
1074 .LP
1075 See \fBattributes\fR(5) for descriptions of the following attributes:
1076 .sp
1077 
1078 .sp
1079 .TS
1080 box;
1081 l | l
1082 l | l .
1083 ATTRIBUTE TYPE  ATTRIBUTE VALUE
1084 _
1085 CSI     Enabled
1086 _
1087 Interface Stability     Committed
1088 _
1089 MT-Level        See below.
1090 _
1091 Standard        See below.
1092 .TE
1093 
1094 .sp
1095 .LP



















1096 All of these functions can be used safely in multithreaded applications, as
1097 long as \fBsetlocale\fR(3C) is not being called to change the locale. The
1098 \fBsprintf()\fR and \fBsnprintf()\fR functions are Async-Signal-Safe.
1099 .sp
1100 .LP
1101 See \fBstandards\fR(5) for the standards conformance of \fBprintf()\fR,
1102 \fBfprintf()\fR, \fBsprintf()\fR, and \fBsnprintf()\fR. The \fBasprintf()\fR
1103 function is modeled on the one that appears in the FreeBSD, NetBSD, and GNU C
1104 libraries.
1105 .SH SEE ALSO
1106 .sp
1107 .LP
1108 \fBexit\fR(2), \fBlseek\fR(2), \fBwrite\fR(2), \fBabort\fR(3C), \fBecvt\fR(3C),
1109 \fBexit\fR(3C), \fBfclose\fR(3C), \fBfflush\fR(3C), \fBfputwc\fR(3C),
1110 \fBfree\fR(3C), \fBmalloc\fR(3C), \fBputc\fR(3C), \fBscanf\fR(3C),
1111 \fBsetlocale\fR(3C), \fBstdio\fR(3C), \fBvprintf\fR(3C), \fBwcstombs\fR(3C),
1112 \fBwctomb\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5), \fBstandards\fR(5)
1113 .SH NOTES
1114 .sp
1115 .LP
1116 If the \fBj\fR length modifier is used, 32-bit applications that were compiled
1117 using \fBc89\fR on releases prior to Solaris 10 will experience undefined
1118 behavior.
1119 .sp
1120 .LP
1121 The \fBsnprintf()\fR return value when \fIn\fR = 0 was changed in the Solaris
1122 10 release. The change was based on the SUSv3 specification. The previous
1123 behavior was based on the initial SUSv2 specification, where \fBsnprintf()\fR
1124 when \fIn\fR = 0 returns an unspecified value less than 1.






































  28 .\" Common Development and Distribution License (the "License").
  29 .\" You may not use this file except in compliance with the License.
  30 .\"
  31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  32 .\" or http://www.opensolaris.org/os/licensing.
  33 .\" See the License for the specific language governing permissions
  34 .\" and limitations under the License.
  35 .\"
  36 .\" When distributing Covered Code, include this CDDL HEADER in each
  37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  38 .\" If applicable, add the following below this CDDL HEADER, with the
  39 .\" fields enclosed by brackets "[]" replaced with your own identifying
  40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
  41 .\"
  42 .\"
  43 .\" Copyright 1989 AT&T
  44 .\" Portions Copyright 1999, Forrest J. Cavalier III. All Rights Reserved.
  45 .\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
  46 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
  47 .\"
  48 .Dd July 10, 2020
  49 .Dt PRINTF 3C
  50 .Os
  51 .Sh NAME
  52 .Nm printf ,
  53 .Nm fprintf ,
  54 .Nm sprintf ,
  55 .Nm snprintf ,
  56 .Nm asprintf
  57 .Nd print formatted output
  58 .Sh LIBRARY
  59 .Lb libc
  60 .Sh SYNOPSIS
  61 .In stdio.h
  62 .Ft int
  63 .Fo printf
  64 .Fa "const char *restrict format"
  65 .Fa "/* args */ ..."
  66 .Fc
  67 .Ft int
  68 .Fo fprintf
  69 .Fa "FILE *restrict stream"
  70 .Fa "const char *restrict format"
  71 .Fa "/* args */ ..."
  72 .Fc
  73 .Ft int
  74 .Fo sprintf
  75 .Fa "char *restrict s"
  76 .Fa "const char *restrict format"
  77 .Fa "/* args */ ..."
  78 .Fc
  79 .Ft int
  80 .Fo snprintf
  81 .Fa "char *restrict s"
  82 .Fa "size_t n"
  83 .Fa "const char *restrict format"
  84 .Fa "/* args */ ..."
  85 .Fc
  86 .Ft int
  87 .Fo asprintf
  88 .Fa "char **ret"
  89 .Fa "const char *restrict format"
  90 .Fa "/* args */ ..."
  91 .Fc
  92 .Sh DESCRIPTION
  93 The
  94 .Fn printf
  95 function places output on the standard output stream
  96 .Dv stdout .
  97 .Pp
  98 The
  99 .Fn fprintf
 100 function places output on on the named output stream
 101 .Fa stream .
 102 .Pp
 103 The
 104 .Fn sprintf
 105 function places output, followed by the null byte
 106 .Pq Sq \e0 ,
 107 in consecutive bytes starting at
 108 .Fa s ;
 109 it is the user's responsibility to ensure that enough storage is available.
 110 .Pp
 111 The
 112 .Fn snprintf
 113 function is identical to
 114 .Fn sprintf
 115 with the addition of the argument
 116 .Fa n ,
 117 which specifies the size of the buffer referred to by
 118 .Fa s .
 119 If
 120 .Fa n
 121 is 0, nothing is written and
 122 .Fa s
 123 can be a
 124 .Dv NULL
 125 pointer.
 126 Otherwise, output bytes beyond the
 127 .Fa n Ns -1st
 128 are discarded instead of being written to the array and a null byte is written
 129 at the end of the bytes actually written into the array.
 130 .Pp
 131 The
 132 .Fn asprintf
 133 function is the same as the
 134 .Fn sprintf
 135 function except that it returns, in the
 136 .Fa ret
 137 argument, a pointer to a buffer sufficiently large to hold the output string.
 138 This pointer should be passed to
 139 .Xr free 3C
 140 to release the allocated storage when it is no longer needed.
 141 If sufficient space cannot be allocated, the
 142 .Fn asprintf
 143 function returns -1 and sets
 144 .Fa ret
 145 to be a
 146 .Dv NULL
 147 pointer.
 148 .Pp
 149 Each of these functions converts, formats, and prints its arguments under
 150 control of the
 151 .Fa format .
 152 The
 153 .Fa format
 154 is a character string, beginning and ending in its initial shift state, if any.
 155 The
 156 .Fa format
 157 is composed of zero or more directives: ordinary characters, which are simply
 158 copied to the output stream and conversion specifications, each of which results
 159 in the fetching of zero or more arguments.
 160 The results are undefined if there are insufficient arguments for the
 161 .Fa format .
 162 If the
 163 .Fa format
 164 is exhausted while arguments remain, the excess arguments are evaluated but are
 165 otherwise ignored.
 166 .Pp
 167 Conversions can be applied to the
 168 .Ar n Ns th
 169 argument after the
 170 .Fa format
 171 in the argument list, rather than to the next unused argument.
 172 In this case, the conversion specifier
 173 .Cm %
 174 .Pq see below
 175 is replaced by the sequence
 176 .Cm % Ns Ar n Ns Cm $ ,
 177 where
 178 .Ar n
 179 is a decimal integer in the range
 180 .Bq 1, Dv NL_ARGMAX ,
 181 giving the position of the argument in the argument list.
 182 This feature provides for the definition of format strings that select
 183 arguments in an order appropriate to specific languages (see the
 184 .Sx EXAMPLES
 185 section).
 186 .Pp
 187 In format strings containing the
 188 .Cm % Ns Ar n Ns Cm $
 189 form of conversion specifications, numbered arguments in the argument list can
 190 be referenced from the format string as many times as required.
 191 .Pp
 192 In format strings containing the
 193 .Cm %
 194 form of conversion specifications, each argument in the argument list is used
 195 exactly once.
 196 .Pp
 197 All forms of the
 198 .Fn printf
 199 functions allow for the insertion of a language-dependent radix character in the
 200 output string.
 201 The radix character is defined by the program's locale
 202 .Pq category Dv LC_NUMERIC .
 203 In the POSIX locale, or in a locale where the radix character is not defined,
 204 the radix character defaults to a period
 205 .Pq \&. .
 206 .Ss Conversion Specifications
 207 Each conversion specification is introduced by the
 208 .Cm %
 209 character or by the character sequence
 210 .Cm % Ns Ar n Ns Cm $ ,
 211 after which the following appear in sequence:
 212 .Bl -bullet
 213 .It
 214 An optional field, consisting of a decimal digit string followed by a
 215 .Cm $ ,
 216 specifying the next argument to be converted.
 217 If this field is not provided, the
 218 .Fa args
 219 following the last argument converted will be used.
 220 .It
 221 Zero or more flags (in any order), which modify the meaning of the
 222 conversion specification.
 223 .It
 224 An optional minimum field width.
 225 If the converted value has fewer bytes than the field width, it will be padded
 226 with spaces by default on the left; it will be padded on the right, if the
 227 left-adjustment flag
 228 .Pq Cm - ,
 229 described below, is given to the field width.
 230 The field width takes the form of an asterisk
 231 .Pq Cm * ,
 232 described below, or a decimal integer.
 233 .Pp
 234 If the conversion specifier is
 235 .Cm s ,
 236 a standard-conforming application
 237 .Pq see Xr standards 5
 238 interprets the field width as the minimum number of bytes to be printed; an
 239 application that is not standard-conforming interprets the field width as the


 240 minimum number of columns of screen display.
 241 For an application that is not standard-conforming,
 242 .Ql %10s
 243 means if the converted value has a screen width of 7 columns, 3 spaces would be
 244 padded on the right.
 245 .Pp
 246 If the format is
 247 .Cm %ws ,
 248 then the field width should be interpreted as the minimum number of columns of
 249 screen display.
 250 .It
 251 An optional precision that gives the minimum number of digits to appear for the
 252 .Cm d , i , o , u , x ,
 253 and
 254 .Cm X
 255 conversions (the field is padded with leading zeros); the number of digits to
 256 appear after the radix character for the
 257 .Cm a , A , e , E , f ,
 258 and
 259 .Cm F
 260 conversions, the maximum number of significant digits for the
 261 .Cm g
 262 and
 263 .Cm G
 264 conversions; or the maximum number of bytes to be printed from a string in
 265 .Cm s
 266 and
 267 .Cm S
 268 conversions.
 269 The precision takes the form of a period
 270 .Pq Cm \&.
 271 followed either by an asterisk
 272 .Pq Cm * ,
 273 described below, or an optional decimal digit string, where a null digit string
 274 is treated as 0.
 275 If a precision appears with any other conversion specifier, the behavior is
 276 undefined.
 277 .Pp
 278 If the conversion specifier is
 279 .Cm s
 280 or
 281 .Cm S ,
 282 a standard-conforming application
 283 .Pq see Xr standards 5
 284 interprets the precision as the maximum number of bytes to be written; an
 285 application that is not standard-conforming interprets the precision as the
 286 maximum number of columns of screen display.
 287 For an application that is not standard-conforming,
 288 .Ql %.5s
 289 would print only the portion of the string that would display in 5 screen
 290 columns.
 291 Only complete characters are written.
 292 .Pp
 293 For
 294 .Cm %ws ,
 295 the precision should be interpreted as the maximum number of columns of screen
 296 display.
 297 The precision takes the form of a period
 298 .Pq Cm \&.
 299 followed by a decimal digit string; a null digit string is treated as zero.
 300 Padding specified by the precision overrides the padding specified by the field
 301 width.
 302 .It
 303 An optional length modifier that specified the size of the argument.
 304 .It
 305 A conversion specifier that indicates the type of conversion to be applied.
 306 .El
 307 .Pp










 308 A field width, or precision, or both can be indicated by an asterisk
 309 .Pq Cm * .
 310 In this case, an argument of type
 311 .Vt int
 312 supplies the field width or precision.
 313 Arguments specifying field width, or precision, or both must appear in that
 314 order before the argument, if any, to be converted.
 315 A negative field width is taken as a
 316 .Cm -
 317 flag followed by a positive field width.
 318 A negative precision is taken as if the precision were omitted.
 319 In format strings containing the
 320 .Cm % Ns Ar n Ns Cm $
 321 form of a conversion specification, a field width or precision may be indicated
 322 by the sequence
 323 .Cm * Ns Ar m Ns Cm $ ,
 324 where
 325 .Ar m
 326 is a decimal integer in the range
 327 .Bq 1, Dv NL_ARGMAX
 328 giving the position in the argument list (after the format argument) of an
 329 integer argument containing the field width or precision, for example:
 330 .Pp
 331 .Dl printf("%1$d:%2$.*3$d:%4$.*3$d\en", hour, min, precision, sec);
 332 .Pp
 333 The
 334 .Fa format
 335 can contain either numbered argument specifications (that is,
 336 .Cm % Ns Ar n Ns Cm $
 337 and
 338 .Cm * Ns Ar m Ns Cm $ ) ,
 339 or unnumbered argument specifications (that is,
 340 .Cm %
 341 and
 342 .Cm * ) ,
 343 but normally not both.
 344 The only exception to this is that
 345 .Cm %%
 346 can be mixed with the
 347 .Cm % Ns Ar n Ns Cm $
 348 form.
 349 The results of mixing numbered and unnumbered argument specifications in a
 350 .Fa format
 351 string are undefined.
 352 When numbered argument specifications are used, specifying the
 353 .Ar N Ns th
 354 argument requires that all the leading arguments, from the first to the
 355 .Po Ar N Ns -1 Pc Ns th ,
 356 are specified in the format string.
 357 .Ss Flag Characters
 358 The flag characters and their meanings are:
 359 .Bl -tag -width "' ' (space)"
 360 .It Cm '
 361 The integer portion of the result of a decimal conversion
 362 .Po Cm %i , %d , %u , %f , %F , %g ,
 363 or
 364 .Cm %G
 365 .Pc
 366 will be formatted with thousands' grouping characters.
 367 For other conversions the behavior is undefined.
 368 The non-monetary grouping character is used.
 369 .It Cm -
 370 The result of the conversion will be left-justified within the field.
 371 The conversion will be right-justified if this flag is not specified.
 372 .It Cm +
 373 The result of a signed conversion will always begin with a sign
 374 .Po
 375 .Cm +
 376 or
 377 .Cm -
 378 .Pc .
 379 The conversion will begin with a sign only when a negative value is converted if









 380 this flag is not specified.
 381 .It Qo "\ " Qc (space)







 382 If the first character of a signed conversion is not a sign or if a signed
 383 conversion results in no characters, a space will be placed before the result.
 384 This means that if the space and
 385 .Cm +
 386 flags both appear, the space flag will be ignored.
 387 .It Cm #
 388 The value is to be converted to an alternate form.
 389 For
 390 .Cm c , d , i , s ,
 391 and
 392 .Cm u
 393 conversions, the flag has no effect.
 394 For an
 395 .Cm o
 396 conversion, it increases the precision (if necessary) to force the
 397 first digit of the result to be a zero.
 398 For
 399 .Cm x
 400 or
 401 .Cm X
 402 conversion, a non-zero result will have
 403 .Ql 0x
 404 .Pq or Ql 0X
 405 prepended to it.
 406 For
 407 .Cm a , A , e , E , f , F , g ,
 408 and
 409 .Cm G
 410 conversions, the result will always contain a radix character, even if no digits
 411 follow the radix character.
 412 Without this flag, the radix character appears in the result of these
 413 conversions only if a digit follows it.
 414 For
 415 .Cm g
 416 and
 417 .Cm G
 418 conversions, trailing zeros will not be removed from the result as they
 419 normally are.
 420 .It Cm 0
 421 For
 422 .Cm d , i , o , u , x , X , a , A , e , E , f , F , g ,
 423 and
 424 .Cm G
 425 conversions, leading zeros (following any indication of sign or base) are used
 426 to pad to the field width; no space padding is performed.
 427 If the
 428 .Cm 0
 429 and
 430 .Cm \-
 431 flags both appear, the
 432 .Cm 0
 433 flag will be ignored.
 434 For
 435 .Cm d , i , o , u , x ,
 436 and
 437 .Cm X
 438 conversions, if a precision is specified, the
 439 .Cm 0
 440 flag will be ignored.
 441 If the
 442 .Cm 0
 443 and
 444 .Cm '
 445 flags both appear, the grouping characters are inserted before zero padding.
 446 For other conversions, the behavior is undefined.
 447 .El
 448 .Ss Length Modifiers
 449 The length modifiers and their meanings are:
 450 .Bl -tag -width "ll (ell-ell)"
 451 .It Cm hh
 452 Specifies that a following
 453 .Cm d , i , o , u , x ,
 454 or
 455 .Cm X
 456 conversion specifier applies to a
 457 .Vt signed char
 458 or
 459 .Vt unsigned char












 460 argument (the argument will have been promoted according to the integer
 461 promotions, but its value will be converted to
 462 .Vt signed char
 463 or
 464 .Vt unsigned char
 465 before printing); or that a following
 466 .Cm n
 467 conversion specifier applies to a pointer to a
 468 .Vt signed char
 469 argument.
 470 .It Cm h
 471 Specifies that a following
 472 .Cm d , i , o , u , x ,
 473 or
 474 .Cm X
 475 conversion specifier applies to a
 476 .Vt short
 477 or
 478 .Vt unsigned short
 479 argument (the argument will have been promoted according to the integer
 480 promotions, but its value will be converted to
 481 .Vt short
 482 or
 483 .Vt unsigned short
 484 before printing); or that a following
 485 .Cm n
 486 conversion specifier applies to a pointer to a
 487 .Vt short
 488 argument.
 489 .It Cm l No (ell)
 490 Specifies that a following
 491 .Cm d , i , o , u , x ,
 492 or
 493 .Cm X
 494 conversion specifier applies to a
 495 .Vt long
 496 or
 497 .Vt unsigned long
 498 argument; that a following
 499 .Cm n
 500 conversion specifier applies to a pointer to a
 501 .Vt long
 502 argument; that a following
 503 .Cm c
 504 conversion specifier applies to a
 505 .Vt wint_t
 506 argument; that a following
 507 .Cm s
 508 conversion specifier applies to a pointer to a
 509 .Vt wchar_t
 510 argument; or has no effect on a following
 511 .Cm a , A , e , E , f , F , g ,
 512 or
 513 .Cm G
 514 conversion specifier.
 515 .It Cm ll No (ell-ell)
 516 Specifies that a following
 517 .Cm d , i , o , u , x ,
 518 or
 519 .Cm X
 520 conversion specifier applies to a
 521 .Vt long long
 522 or
 523 .Vt unsigned long long
 524 argument; or that a following
 525 .Cm n
 526 conversion specifier applies to a pointer to a
 527 .Vt long long























 528 argument.
 529 .It Cm j
 530 Specifies that a following
 531 .Cm d , i , o , u , x ,
 532 or
 533 .Cm X
 534 conversion specifier applies to an
 535 .Vt intmax_t
 536 or
 537 .Vt uintmax_t
 538 argument; or that a following
 539 .Cm n
 540 conversion specifier applies to a pointer to an
 541 .Vt intmax_t
 542 argument.
 543 See
 544 .Sx NOTES .
 545 .It Cm z
 546 Specifies that a following
 547 .Cm d , i , o , u , x ,
 548 or
 549 .Cm X
 550 conversion specifier applies to a
 551 .Vt size_t
 552 or the corresponding signed integer type argument; or that a following
 553 .Cm n
 554 conversion specifier applies to a pointer to a signed integer type corresponding
 555 to
 556 .Vt size_t
 557 argument.
 558 .It Cm t
 559 Specifies that a following
 560 .Cm d , i , o , u , x ,
 561 or
 562 .Cm X
 563 conversion specifier applies to a
 564 .Vt ptrdiff_t
 565 or the corresponding unsigned type argument; or that a following
 566 .Cm n
 567 conversion specifier applies to a pointer to a
 568 .Vt ptrdiff_t
 569 argument.
 570 .It Cm L
 571 Specifies that a following
 572 .Cm a , A , e , E , f , F , g ,
 573 or
 574 .Cm G
 575 conversion specifier applies to a
 576 .Vt long double
 577 argument.
 578 .El
 579 .Pp
 580 If a length modifier appears with any conversion specifier other than as
 581 specified above, the behavior is undefined.
 582 .Ss Conversion Specifiers
 583 Each conversion specifier results in fetching zero or more arguments.
 584 The results are undefined if there are insufficient arguments for the format.
 585 If the format is exhausted while arguments remain, the excess arguments are


 586 ignored.
 587 .Pp

 588 The conversion specifiers and their meanings are:
 589 .Bl -tag -width "d, i"
 590 .It Cm d , i
 591 The
 592 .Vt int
 593 argument is converted to a signed decimal in the style
 594 .Ql [-]dddd .
 595 The precision specifies the minimum number of digits to appear; if the value
 596 being converted can be represented in fewer digits, it will be expanded with
 597 leading zeros.
 598 The default precision is 1.
 599 The result of converting 0 with an explicit precision of 0 is no characters.
 600 .It Cm o
 601 The
 602 .Vt unsigned int
 603 argument is converted to unsigned octal format in the style
 604 .Ql dddd .
 605 The precision specifies the minimum number of digits to appear; if the value
 606 being converted can be represented in fewer digits, it will be expanded with
 607 leading zeros.
 608 The default precision is 1.
 609 The result of converting 0 with an explicit precision of 0 is no characters.
 610 .It Cm u
 611 The
 612 .Vt unsigned int
 613 argument is converted to unsigned decimal format in the style
 614 .Ql dddd .
 615 The precision specifies the minimum number of digits to appear; if the value
 616 being converted can be represented in fewer digits, it will be expanded with
 617 leading zeros.
 618 The default precision is 1.
 619 The result of converting 0 with an explicit precision of 0 is no characters.
 620 .It Cm x
 621 The
 622 .Vt unsigned int
 623 argument is converted to unsigned hexadecimal format in the style
 624 .Ql dddd ;
 625 the letters
 626 .Ql abcdef
 627 are used.
 628 The precision specifies the minimum number of digits to appear; if the value
 629 being converted can be represented in fewer digits, it will be expanded with
 630 leading zeros.
 631 The default precision is 1.
 632 The result of converting 0 with an explicit precision of 0 is no characters.
 633 .It Cm X
 634 Behaves the same as the
 635 .Cm x
 636 conversion specifier except that letters
 637 .Ql ABCDEF
 638 are used instead of
 639 .Ql abcdef .
 640 .It Cm f , F
 641 The
 642 .Vt double
 643 argument is converted to decimal notation in the style
 644 .Ql [-]ddd.ddd ,
 645 where the number of digits after the radix character
 646 .Pq see Xr setlocale 3C
 647 is equal to the precision specification.
 648 If the precision is missing it is taken as 6; if the precision is explicitly 0
 649 and the
 650 .Cm #
 651 flag is not specified, no radix character appears.
 652 If a radix character appears, at least 1 digit appears before it.
 653 The converted value is rounded to fit the specified output format according to
 654 the prevailing floating point rounding direction mode.
 655 If the conversion is not exact, an inexact exception is raised.
 656 .Pp
 657 For the
 658 .Cm f
 659 specifier, a
 660 .Vt double
 661 argument representing an infinity or NaN is converted in the style of the
 662 .Cm e
 663 conversion specifier, except that for an infinite argument,
 664 .Ql infinity
 665 or
 666 .Ql Infinity
 667 is printed when the precision is at least 8 and
 668 .Ql inf
 669 or
 670 .Ql Inf
 671 is printed otherwise.
 672 .Pp
 673 For the
 674 .Cm F
 675 specifier, a
 676 .Vt double
 677 argument representing an infinity or NaN is converted in the SUSv3 style of the
 678 .Cm E
 679 conversion specifier, except that for an infinite argument,
 680 .Ql INFINITY
 681 is printed when the precision is at least 8 and
 682 .Ql INF
 683 is printed otherwise.
 684 .It Cm e , E
 685 The
 686 .Vt double
 687 argument is converted to the style
 688 .Ql [-]d.ddde+-dd ,
 689 where there is one digit before the radix character (which is non-zero if the
 690 argument is non-zero) and the number of digits after it is equal to the
 691 precision.
 692 When the precision is missing it is taken as 6; if the precision is 0 and the
 693 .Cm #
 694 flag is not specified, no radix character appears.
 695 The
 696 .Cm E
 697 conversion specifier will produce a number with
 698 .Ql E
 699 instead of
 700 .Ql e
 701 introducing the exponent.
 702 The exponent always contains at least two digits.
 703 The converted value is rounded to fit the specified output format according to
 704 the prevailing floating point rounding direction mode.
 705 If the conversion is not exact, an inexact exception is raised.
 706 .Pp
 707 Infinity and NaN values are handled in one of the following ways:
 708 .Bl -tag -width "Default"
 709 .It SUSv3
 710 For the
 711 .Cm e
 712 specifier, a
 713 .Vt double
 714 argument representing an infinity is printed as
 715 .Ql [-]infinity ,
 716 when the precision for the conversion is at least 7 and as
 717 .Ql [-]inf
 718 otherwise.
 719 A
 720 .Vt double
 721 argument representing a NaN is printed as
 722 .Ql [-]nan .
 723 For the
 724 .Cm E
 725 specifier,
 726 .Ql INF ,
 727 .Ql INFINITY ,
 728 and
 729 .Ql NAN
 730 are printed instead of
 731 .Ql inf ,
 732 .Ql infinity ,
 733 and
 734 .Ql nan ,
 735 respectively.
 736 Printing of the sign follows the rules described above.
 737 .It Default
 738 A
 739 .Vt double
 740 argument representing an infinity is printed as
 741 .Ql [-]Infinity ,
 742 when the precision for the conversion is at least 7 and as
 743 .Ql [-]Inf
 744 otherwise.
 745 A
 746 .Vt double
 747 argument representing a NaN is printed as
 748 .Ql [-]NaN .
 749 Printing of the sign follows the rules described above.
 750 .El
 751 .It Cm g , G
 752 The
 753 .Vt double
 754 argument is printed in style
 755 .Cm f
 756 or
 757 .Cm e
 758 (or in style
 759 .Cm E
 760 in the case of a
 761 .Cm G
 762 conversion specifier), with the precision specifying the number of significant
 763 digits.
 764 If an explicit precision is 0, it is taken as 1.
 765 The style used depends on the value converted: style
 766 .Cm e
 767 .Pq or Cm E
 768 will be used only if the exponent resulting from the conversion is less than -4
 769 or greater than or equal to the precision.
 770 Trailing zeros are removed from the fractional part of the result.
 771 A radix character appears only if it is followed by a digit.
 772 .Pp
 773 A
 774 .Vt double
 775 argument representing an infinity or NaN is converted in the style of the
 776 .Cm e
 777 or
 778 .Cm E
 779 conversion specifier, except that for an infinite argument,
 780 .Ql infinity ,
 781 .Ql INFINITY ,
 782 or
 783 .Ql Infinity
 784 is printed when the precision is at least 8 and
 785 .Ql inf ,
 786 .Ql INF ,
 787 or
 788 .Ql Inf
 789 is printed otherwise.
 790 .It Cm a , A
 791 A
 792 .Vt double
 793 argument representing a floating-point number is converted in the style
 794 .Ql [-]0xh.hhhhp+-d ,
 795 where the single hexadecimal digit preceding the radix point is 0 if the value
 796 converted is zero and 1 otherwise and the number of hexadecimal digits after it
 797 is equal to the precision; if the precision is missing, the number of digits
 798 printed after the radix point is 13 for the conversion of a
 799 .Vt double
 800 value, 16 for the conversion of a
 801 .Vt long double
 802 value on x86, and 28 for the conversion of a
 803 .Vt long double
 804 value on SPARC; if the precision is zero and the
 805 .Cm #
 806 flag is not specified, no decimal-point character will appear.
 807 The letters
 808 .Ql abcdef
 809 are used for
 810 .Cm a
 811 conversion and the letters
 812 .Ql ABCDEF
 813 for
 814 .Cm A
 815 conversion.
 816 The
 817 .Cm A
 818 conversion specifier produces a number with
 819 .Ql X
 820 and
 821 .Ql P
 822 instead of
 823 .Ql x
 824 and
 825 .Ql p .
 826 The exponent will always contain at least one digit, and only as many more
 827 digits as necessary to represent the decimal exponent of 2.
 828 If the value is zero, the exponent is zero.
 829 .Pp
 830 The converted value is rounded to fit the specified output format according to
 831 the prevailing floating point rounding direction mode.
 832 If the conversion is not exact, an inexact exception is raised.
 833 .Pp
 834 A
 835 .Vt double
 836 argument representing an infinity or NaN is converted in the SUSv3 style of an
 837 .Cm e
 838 or
 839 .Cm E
 840 conversion specifier.
 841 .It Cm c
 842 The
 843 .Vt int
 844 argument is converted to an
 845 .Vt unsigned char ,
 846 and the resulting byte is printed.
 847 .Pp
 848 If an
 849 .Cm l No (ell)
 850 qualifier is present, the
 851 .Vt wint_t
 852 argument is converted as if by an
 853 .Cm ls
 854 conversion specification with no precision and an argument that points to a
 855 two-element array of type
 856 .Vt wchar_t ,
 857 the first element of which contains the
 858 .Vt wint_t
 859 argument to the
 860 .Cm ls
 861 conversion specification and the second element contains a null wide-character.
 862 .It Cm C
 863 Same as
 864 .Cm lc .
 865 .It Cm wc
 866 The
 867 .Vt int
 868 argument is converted to a wide character
 869 .Pq Vt wchar_t ,
 870 and the resulting wide character is printed.
 871 .It Cm s
 872 The argument must be a pointer to an array of
 873 .Vt char .
 874 Bytes from the array are written up to (but not including) any terminating null
 875 byte.
 876 If a precision is specified, a standard-conforming application
 877 .Pq see Xr standards 5
 878 will write only the number of bytes specified by precision; an application that
 879 is not standard-conforming will write only the portion of the string that will
 880 display in the number of columns of screen display specified by precision.
 881 If the precision is not specified, it is taken to be infinite, so all bytes up
 882 to the first null byte are printed.
 883 An argument with a null value will yield undefined results.
 884 .Pp
 885 If an
 886 .Cm l No (ell)
 887 qualifier is present, the argument must be a pointer to an array of type
 888 .Vt wchar_t .
 889 Wide-characters from the array are converted to characters (each as if by a call
 890 to the
 891 .Xr wcrtomb 3C
 892 function, with the conversion state described by an
 893 .Vt mbstate_t
 894 object initialized to zero before the first wide-character is converted) up to
 895 and including a terminating null wide-character.
 896 The resulting characters are written up to (but not including) the terminating
 897 null character (byte).
 898 If no precision is specified, the array must contain a null wide-character.
 899 If a precision is specified, no more than that many characters (bytes) are
 900 written (including shift sequences, if any), and the array must contain a null
 901 wide-character if, to equal the character sequence length given by the
 902 precision, the function would need to access a wide-character one past the end
 903 of the array.
 904 In no case is a partial character written.
 905 .It Cm S
 906 Same as
 907 .Cm ls .
 908 .It Cm ws
 909 The argument must be a pointer to an array of
 910 .Vt wchar_t .
 911 Bytes from the array are written up to (but not including) any terminating null
 912 character.
 913 If the precision is specified, only that portion of the wide-character array
 914 that will display in the number of columns of screen display specified by
 915 precision will be written.
 916 If the precision is not specified, it is taken to be infinite, so all wide
 917 characters up to the first null character are printed.
 918 An argument with a null value will yield undefined results.
 919 .It Cm p
 920 The argument must be a pointer to
 921 .Vt void .
 922 The value of the pointer is converted to a set of sequences of printable
 923 characters, which should be the same as the set of sequences that are matched by
 924 the
 925 .Cm %p
 926 conversion of the
 927 .Xr scanf 3C
 928 function.
 929 .It Cm n
















 930 The argument must be a pointer to an integer into which is written the number
 931 of bytes written to the output standard I/O stream so far by this call to one
 932 of the
 933 .Fn printf
 934 functions.
 935 No argument is converted.
 936 .It Cm %
 937 Print a
 938 .Ql % ;
 939 no argument is converted.
 940 The entire conversion specification must be
 941 .Cm %% .
 942 .El
 943 .Pp



 944 If a conversion specification does not match one of the above forms, the
 945 behavior is undefined.
 946 .Pp

 947 In no case does a non-existent or small field width cause truncation of a
 948 field; if the result of a conversion is wider than the field width, the field
 949 is simply expanded to contain the conversion result.
 950 Characters generated by
 951 .Fn printf
 952 and
 953 .Fn fprintf
 954 are printed as if the
 955 .Xr putc 3C
 956 function had been called.
 957 .Pp
 958 The
 959 .Va st_ctime
 960 and
 961 .Va st_mtime
 962 fields of the file will be marked for update between the call to a successful
 963 execution of
 964 .Fn printf
 965 or
 966 .Fn fprintf
 967 and the next successful completion of a call to
 968 .Xr fflush 3C
 969 or
 970 .Xr fclose 3C
 971 on the same stream or a call to
 972 .Xr exit 3C
 973 or
 974 .Xr abort 3C .
 975 .Sh RETURN VALUES
 976 The
 977 .Fn printf ,
 978 .Fn fprintf ,
 979 .Fn sprintf ,
 980 and
 981 .Fn asprintf
 982 functions return the number of bytes transmitted (excluding the terminating
 983 null byte in the case of
 984 .Fn sprintf
 985 and
 986 .Fn asprintf ) .
 987 .Pp
 988 The
 989 .Fn snprintf
 990 function returns the number of bytes that would have been written to
 991 .Fa s
 992 if
 993 .Fa n
 994 had been sufficiently large (excluding the terminating null byte).
 995 If the value of
 996 .Fa n
 997 is 0 on a call to
 998 .Fn snprintf ,
 999 .Fa s
1000 can be a null pointer and the number of bytes that would have been written if
1001 .Fa n
1002 had been sufficiently large (excluding the terminating null byte) is returned.
1003 .Pp
1004 Each function returns a negative value if an output error was encountered.
1005 .Sh USAGE
1006 If the application calling the
1007 .Fn printf
1008 functions has any objects of type
1009 .Vt wint_t
1010 or
1011 .Vt wchar_t ,
1012 it must also include the header
1013 .In wchar.h




























































1014 to have these objects defined.
1015 .Ss Escape Character Sequences


1016 It is common to use the following escape sequences built into the C language
1017 when entering format strings for the
1018 .Fn printf
1019 functions, but these sequences are processed by the C compiler, not by the
1020 .Fn printf
1021 function.
1022 .Bl -tag -width "\ea"
1023 .It \ea
1024 Alert.
1025 Ring the bell.
1026 .It \eb
1027 Backspace.
1028 Move the printing position to one character before the current position, unless
1029 the current position is the start of a line.
1030 .It \ef
1031 Form feed.
1032 Move the printing position to the initial printing position of the next logical
1033 page.
1034 .It \en
1035 Newline.
1036 Move the printing position to the start of the next line.
1037 .It \er
1038 Carriage return.
1039 Move the printing position to the start of the current line.
1040 .It \et
1041 Horizontal tab.
1042 Move the printing position to the next implementation-defined horizontal tab
1043 position on the current line.
1044 .It \ev
1045 Vertical tab.
1046 Move the printing position to the start of the next implementation-defined
1047 vertical tab position.
1048 .El
1049 .Pp






































1050 In addition, the C language supports character sequences of the form
1051 .Cm \e Ns Ar octal-number




1052 and
1053 .Cm \e Ns Ar hex-number




1054 which translates into the character represented by the octal or hexadecimal
1055 number.
1056 For example, if ASCII representations are being used, the letter 'a' may be
1057 written as
1058 .Ql \e141
1059 and 'Z' as
1060 .Ql \e132 .
1061 This syntax is most frequently used to represent the null character as
1062 .Ql \e0 .
1063 This is exactly equivalent to the numeric constant zero (0).
1064 Note that the octal number does not include the zero prefix as it would for a
1065 normal octal constant.
1066 To specify a hexadecimal number, omit the zero so that the prefix is an 'x'
1067 (uppercase 'X' is not allowed in this context).
1068 Support for hexadecimal sequences is an ANSI extension.
1069 See
1070 .Xr standards 5 .
1071 .Sh EXAMPLES
1072 .Sy Example 1
1073 To print the language-independent date and time format, the
1074 following statement could be used:
1075 .Pp
1076 .Dl printf (format, weekday, month, day, hour, min);
1077 .Pp
1078 For American usage,
1079 .Fa format
1080 could be a pointer to the string:
1081 .Pp
1082 .Dl Qq %s, %s %d, %d:%.2d\en
1083 .Pp











1084 producing the message:
1085 .Pp
1086 .Dl Sunday, July 3, 10:02
1087 .Pp
1088 whereas for German usage,
1089 .Fa format
1090 could be a pointer to the string:
1091 .Pp
1092 .Dl Qq %1$s, %3$d. %2$s, %4$d:%5$.2d\en
1093 .Pp












1094 producing the message:
1095 .Pp
1096 .Dl Sonntag, 3. Juli, 10:02
1097 .Pp
1098 .Sy Example 2
1099 To print a date and time in the form
1100 .Ql Sunday, July 3, 10:02 ,
1101 where
1102 .Va weekday
1103 and
1104 .Va month
1105 are pointers to null-terminated strings:
1106 .Pp
1107 .Dl printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min);
1108 .Pp
1109 .Sy Example 3
1110 To print pi to 5 decimal places:
1111 .Pp
1112 .Dl printf("pi = %.5f", 4 * atan(1.0));
1113 .Pp
1114 .Sy Example 4
1115 The following example applies only to applications that are not
1116 standard-conforming.
1117 To print a list of names in columns which are 20 characters wide:
1118 .Pp
1119 .Dl printf("%20s%20s%20s", lastname, firstname, middlename);
1120 .Sh ERRORS
1121 For the conditions under which
1122 .Fn printf
1123 and
1124 .Fn fprintf
1125 will fail and may fail, refer to
1126 .Xr fputc 3C
1127 or
1128 .Xr fputwc 3C .
1129 .Pp
1130 The
1131 .Fn snprintf
1132 function will fail if:
1133 .Bl -tag -width Er
1134 .It Er EOVERFLOW
1135 The value of
1136 .Fa n
1137 is greater than
1138 .Dv INT_MAX
1139 or the number of bytes needed to hold the output excluding the terminating null
1140 is greater than
1141 .Dv INT_MAX .
1142 .El
1143 .Pp
1144 The
1145 .Fn printf ,
1146 .Fn fprintf ,
1147 .Fn sprintf ,
1148 and
1149 .Fn snprintf
1150 functions may fail if:
1151 .Bl -tag -width Er
1152 .It Er EILSEQ
1153 A wide-character code that does not correspond to a valid character has been
1154 detected.
1155 .It Er EINVAL
1156 There are insufficient arguments.
1157 .El
1158 .Pp
1159 The
1160 .Fn printf ,
1161 .Fn fprintf ,
1162 and
1163 .Fn asprintf
1164 functions may fail due to an underlying
1165 .Xr malloc 3C
1166 failure if:
1167 .Bl -tag -width Er
1168 .It Er EAGAIN
1169 Storage space is temporarily unavailable.
1170 .It Er ENOMEM
1171 Insufficient storage space is available.
1172 .El
1173 .Sh CODE SET INDEPENDENCE
1174 .Sy Enabled
1175 .Sh INTERFACE STABILITY
1176 .Sy Committed
1177 .Sh MT-LEVEL
1178 All of these functions can be used safely in multithreaded applications, as
1179 long as
1180 .Xr setlocale 3C
1181 is not being called to change the locale.
1182 The
1183 .Fn sprintf
1184 and
1185 .Fn snprintf
1186 functions are
1187 .Sy Async-Signal-Safe .
1188 .Sh SEE ALSO
1189 .Xr exit 2 ,
1190 .Xr lseek 2 ,
1191 .Xr write 2 ,
1192 .Xr abort 3C ,
1193 .Xr ecvt 3C ,
1194 .Xr exit 3C ,
1195 .Xr fclose 3C ,
1196 .Xr fflush 3C ,
1197 .Xr fputwc 3C ,
1198 .Xr free 3C ,
1199 .Xr malloc 3C ,
1200 .Xr putc 3C ,
1201 .Xr scanf 3C ,
1202 .Xr setlocale 3C ,
1203 .Xr stdio 3C ,
1204 .Xr vprintf 3C ,
1205 .Xr wcstombs 3C ,
1206 .Xr wctomb 3C ,
1207 .Xr attributes 5 ,
1208 .Xr environ 5 ,
1209 .Xr standards 5
1210 .Sh STANDARDS
1211 See
1212 .Xr standards 5
1213 for the standards conformance of
1214 .Fn printf ,
1215 .Fn fprintf ,
1216 .Fn sprintf ,
1217 and
1218 .Fn snprintf .
1219 The
1220 .Fn asprintf
1221 function is modeled on the one that appears in the
1222 .Fx ,
1223 .Nx ,
1224 and GNU C libraries.
1225 .Sh NOTES
1226 If the
1227 .Cm j
1228 length modifier is used, 32-bit applications that were compiled using
1229 .Nm c89
1230 on releases prior to Solaris 10 will experience undefined behavior.
1231 .Pp
1232 The
1233 .Fn snprintf
1234 return value when
1235 .Fa n
1236 is 0 was changed in the Solaris 10 release.
1237 The change was based on the SUSv3 specification.
1238 The previous behavior was based on the initial SUSv2 specification, where
1239 .Fn snprintf
1240 when
1241 .Fa n
1242 is 0 returns an unspecified value less than 1.