1 .\"
   2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
   3 .\" permission to reproduce portions of its copyrighted documentation.
   4 .\" Original documentation from The Open Group can be obtained online at
   5 .\" http://www.opengroup.org/bookstore/.
   6 .\"
   7 .\" The Institute of Electrical and Electronics Engineers and The Open
   8 .\" Group, have given us permission to reprint portions of their
   9 .\" documentation.
  10 .\"
  11 .\" In the following statement, the phrase ``this text'' refers to portions
  12 .\" of the system documentation.
  13 .\"
  14 .\" Portions of this text are reprinted and reproduced in electronic form
  15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
  16 .\" Standard for Information Technology -- Portable Operating System
  17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
  18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
  19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
  20 .\" between these versions and the original IEEE and The Open Group
  21 .\" Standard, the original IEEE and The Open Group Standard is the referee
  22 .\" document.  The original Standard can be obtained online at
  23 .\" http://www.opengroup.org/unix/online.html.
  24 .\"
  25 .\" This notice shall appear on any product containing this material.
  26 .\"
  27 .\" The contents of this file are subject to the terms of the
  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.