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 .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.