/usr/share/doc/libnewlib-doc/libc.html/sprintf.html is in libnewlib-doc 2.4.0.20160527-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.3, http://www.gnu.org/software/texinfo/ -->
<head>
<title>The Red Hat newlib C Library: sprintf</title>
<meta name="description" content="The Red Hat newlib C Library: sprintf">
<meta name="keywords" content="The Red Hat newlib C Library: sprintf">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
<link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Stdio.html#Stdio" rel="up" title="Stdio">
<link href="sscanf.html#sscanf" rel="next" title="sscanf">
<link href="siscanf.html#siscanf" rel="prev" title="siscanf">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<a name="sprintf"></a>
<div class="header">
<p>
Next: <a href="sscanf.html#sscanf" accesskey="n" rel="next">sscanf</a>, Previous: <a href="siscanf.html#siscanf" accesskey="p" rel="prev">siscanf</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="sprintf_002c-fprintf_002c-printf_002c-snprintf_002c-asprintf_002c-asnprintf_002d_002d_002dformat-output"></a>
<h3 class="section">4.61 <code>sprintf</code>, <code>fprintf</code>, <code>printf</code>, <code>snprintf</code>, <code>asprintf</code>, <code>asnprintf</code>—format output</h3>
<a name="index-fprintf"></a>
<a name="index-_005ffprintf_005fr"></a>
<a name="index-printf"></a>
<a name="index-_005fprintf_005fr"></a>
<a name="index-asprintf"></a>
<a name="index-_005fasprintf_005fr"></a>
<a name="index-sprintf"></a>
<a name="index-_005fsprintf_005fr"></a>
<a name="index-snprintf"></a>
<a name="index-_005fsnprintf_005fr"></a>
<a name="index-asnprintf"></a>
<a name="index-_005fasnprintf_005fr"></a>
<p><strong>Synopsis</strong>
</p><div class="example">
<pre class="example">#include <stdio.h>
int printf(const char *restrict <var>format</var>, ...);
int fprintf(FILE *restrict <var>fd</var>, const char *restrict <var>format</var>, ...);
int sprintf(char *restrict <var>str</var>, const char *restrict <var>format</var>, ...);
int snprintf(char *restrict <var>str</var>, size_t <var>size</var>, const char *restrict <var>format</var>,
...);
int asprintf(char **restrict <var>strp</var>, const char *restrict <var>format</var>, ...);
char *asnprintf(char *restrict <var>str</var>, size_t *restrict <var>size</var>, const char *restrict <var>format</var>,
...);
int _printf_r(struct _reent *<var>ptr</var>, const char *restrict <var>format</var>, ...);
int _fprintf_r(struct _reent *<var>ptr</var>, FILE *restrict <var>fd</var>,
const char *restrict <var>format</var>, ...);
int _sprintf_r(struct _reent *<var>ptr</var>, char *restrict <var>str</var>,
const char *restrict <var>format</var>, ...);
int _snprintf_r(struct _reent *<var>ptr</var>, char *restrict <var>str</var>, size_t <var>size</var>,
const char *restrict <var>format</var>, ...);
int _asprintf_r(struct _reent *<var>ptr</var>, char **restrict <var>strp</var>,
const char *restrict <var>format</var>, ...);
char *_asnprintf_r(struct _reent *<var>ptr</var>, char *restrict <var>str</var>,
size_t *restrict <var>size</var>, const char *restrict <var>format</var>, ...);
</pre></div>
<p><strong>Description</strong><br>
<code>printf</code> accepts a series of arguments, applies to each a
format specifier from <code>*<var>format</var></code>, and writes the
formatted data to <code>stdout</code>, without a terminating NUL
character. The behavior of <code>printf</code> is undefined if there
are not enough arguments for the format. <code>printf</code> returns
when it reaches the end of the format string. If there are
more arguments than the format requires, excess arguments are
ignored.
</p>
<p><code>fprintf</code> is like <code>printf</code>, except that output is directed
to the stream <var>fd</var> rather than <code>stdout</code>.
</p>
<p><code>sprintf</code> is like <code>printf</code>, except that output is directed
to the buffer <var>str</var>, and a terminating NUL is output.
Behavior is undefined if more output is generated than the
buffer can hold.
</p>
<p><code>snprintf</code> is like <code>sprintf</code>, except that output is
limited to at most <var>size</var> bytes, including the terminating
<code>NUL</code>. As a special case, if <var>size</var> is 0, <var>str</var> can be
NULL, and <code>snprintf</code> merely calculates how many bytes would
be printed.
</p>
<p><code>asprintf</code> is like <code>sprintf</code>, except that the output is
stored in a dynamically allocated buffer, <var>pstr</var>, which
should be freed later with <code>free</code>.
</p>
<p><code>asnprintf</code> is like <code>sprintf</code>, except that the return type
is either the original <var>str</var> if it was large enough, or a
dynamically allocated string if the output exceeds *<var>size</var>;
the length of the result is returned in *<var>size</var>. When
dynamic allocation occurs, the contents of the original
<var>str</var> may have been modified.
</p>
<p>For <code>sprintf</code>, <code>snprintf</code>, and <code>asnprintf</code>, the behavior
is undefined if the output <code>*<var>str</var></code> overlaps with one of
the arguments. Behavior is also undefined if the argument for
<code>%n</code> within <code>*<var>format</var></code> overlaps another argument.
</p>
<p><var>format</var> is a pointer to a character string containing two
types of objects: ordinary characters (other than <code>%</code>),
which are copied unchanged to the output, and conversion
specifications, each of which is introduced by <code>%</code>. (To
include <code>%</code> in the output, use <code>%%</code> in the format string.)
A conversion specification has the following form:
</p>
<div class="smallexample">
<pre class="smallexample"> %[<var>pos</var>][<var>flags</var>][<var>width</var>][.<var>prec</var>][<var>size</var>]<var>type</var>
</pre></div>
<p>The fields of the conversion specification have the following
meanings:
</p>
<ul>
<li> <var>pos</var>
<p>Conversions normally consume arguments in the order that they
are presented. However, it is possible to consume arguments
out of order, and reuse an argument for more than one
conversion specification (although the behavior is undefined
if the same argument is requested with different types), by
specifying <var>pos</var>, which is a decimal integer followed by
’$’. The integer must be between 1 and <NL_ARGMAX> from
limits.h, and if argument <code>%n$</code> is requested, all earlier
arguments must be requested somewhere within <var>format</var>. If
positional parameters are used, then all conversion
specifications except for <code>%%</code> must specify a position.
This positional parameters method is a POSIX extension to the C
standard definition for the functions.
</p>
</li><li> <var>flags</var>
<p><var>flags</var> is an optional sequence of characters which control
output justification, numeric signs, decimal points, trailing
zeros, and octal and hex prefixes. The flag characters are
minus (<code>-</code>), plus (<code>+</code>), space ( ), zero (<code>0</code>), sharp
(<code>#</code>), and quote (<code>'</code>). They can appear in any
combination, although not all flags can be used for all
conversion specification types.
</p>
<dl compact="compact">
<dt><code>'</code></dt>
<dd><p>A POSIX extension to the C standard. However, this
implementation presently treats it as a no-op, which
is the default behavior for the C locale, anyway. (If
it did what it is supposed to, when <var>type</var> were <code>i</code>,
<code>d</code>, <code>u</code>, <code>f</code>, <code>F</code>, <code>g</code>, or <code>G</code>, the
integer portion of the conversion would be formatted
with thousands’ grouping wide characters.)
</p>
</dd>
<dt><code>-</code></dt>
<dd><p>The result of the conversion is left
justified, and the right is padded with
blanks. If you do not use this flag, the
result is right justified, and padded on the
left.
</p>
</dd>
<dt><code>+</code></dt>
<dd><p>The result of a signed conversion (as
determined by <var>type</var> of <code>d</code>, <code>i</code>, <code>a</code>,
<code>A</code>, <code>e</code>, <code>E</code>, <code>f</code>, <code>F</code>, <code>g</code>, or
<code>G</code>) will always begin with a plus or minus
sign. (If you do not use this flag, positive
values do not begin with a plus sign.)
</p>
</dd>
<dt><code>" " (space)</code></dt>
<dd><p>If the first character of a signed conversion
specification is not a sign, or if a signed
conversion results in no characters, the
result will begin with a space. If the space
( ) flag and the plus (<code>+</code>) flag both
appear, the space flag is ignored.
</p>
</dd>
<dt><code>0</code></dt>
<dd><p>If the <var>type</var> character is <code>d</code>, <code>i</code>,
<code>o</code>, <code>u</code>, <code>x</code>, <code>X</code>, <code>a</code>, <code>A</code>,
<code>e</code>, <code>E</code>, <code>f</code>, <code>F</code>, <code>g</code>, or <code>G</code>: leading
zeros are used to pad the field width
(following any indication of sign or base); no
spaces are used for padding. If the zero
(<code>0</code>) and minus (<code>-</code>) flags both appear,
the zero (<code>0</code>) flag will be ignored. For
<code>d</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>x</code>, and <code>X</code>
conversions, if a precision <var>prec</var> is
specified, the zero (<code>0</code>) flag is ignored.
</p>
<p>Note that <code>0</code> is interpreted as a flag, not
as the beginning of a field width.
</p>
</dd>
<dt><code>#</code></dt>
<dd><p>The result is to be converted to an
alternative form, according to the <var>type</var>
character.
</p></dd>
</dl>
<p>The alternative form output with the # flag depends on the <var>type</var>
character:
</p>
<dl compact="compact">
<dt><code>o</code></dt>
<dd><p>Increases precision to force the first
digit of the result to be a zero.
</p>
</dd>
<dt><code>x</code></dt>
<dd><p>A non-zero result will have a <code>0x</code>
prefix.
</p>
</dd>
<dt><code>X</code></dt>
<dd><p>A non-zero result will have a <code>0X</code>
prefix.
</p>
</dd>
<dt><code>a, A, e, E, f, or F</code></dt>
<dd><p>The result will always contain a
decimal point even if no digits follow
the point. (Normally, a decimal point
appears only if a digit follows it.)
Trailing zeros are removed.
</p>
</dd>
<dt><code>g or G</code></dt>
<dd><p>The result will always contain a
decimal point even if no digits follow
the point. Trailing zeros are not
removed.
</p>
</dd>
<dt><code>all others</code></dt>
<dd><p>Undefined.
</p>
</dd>
</dl>
</li><li> <var>width</var>
<p><var>width</var> is an optional minimum field width. You can
either specify it directly as a decimal integer, or
indirectly by using instead an asterisk (<code>*</code>), in
which case an <code>int</code> argument is used as the field
width. If positional arguments are used, then the
width must also be specified positionally as <code>*m$</code>,
with m as a decimal integer. Negative field widths
are treated as specifying the minus (<code>-</code>) flag for
left justfication, along with a positive field width.
The resulting format may be wider than the specified
width.
</p>
</li><li> <var>prec</var>
<p><var>prec</var> is an optional field; if present, it is
introduced with ‘<code>.</code>’ (a period). You can specify
the precision either directly as a decimal integer or
indirectly by using an asterisk (<code>*</code>), in which case
an <code>int</code> argument is used as the precision. If
positional arguments are used, then the precision must
also be specified positionally as <code>*m$</code>, with m as a
decimal integer. Supplying a negative precision is
equivalent to omitting the precision. If only a
period is specified the precision is zero. The effect
depends on the conversion <var>type</var>.
</p>
<dl compact="compact">
<dt><code>d, i, o, u, x, or X</code></dt>
<dd><p>Minimum number of digits to appear. If no
precision is given, defaults to 1.
</p>
</dd>
<dt><code>a or A</code></dt>
<dd><p>Number of digits to appear after the decimal
point. If no precision is given, the
precision defaults to the minimum needed for
an exact representation.
</p>
</dd>
<dt><code>e, E, f or F</code></dt>
<dd><p>Number of digits to appear after the decimal
point. If no precision is given, the
precision defaults to 6.
</p>
</dd>
<dt><code>g or G</code></dt>
<dd><p>Maximum number of significant digits. A
precision of 0 is treated the same as a
precision of 1. If no precision is given, the
precision defaults to 6.
</p>
</dd>
<dt><code>s or S</code></dt>
<dd><p>Maximum number of characters to print from the
string. If no precision is given, the entire
string is printed.
</p>
</dd>
<dt><code>all others</code></dt>
<dd><p>undefined.
</p>
</dd>
</dl>
</li><li> <var>size</var>
<p><var>size</var> is an optional modifier that changes the data
type that the corresponding argument has. Behavior is
unspecified if a size is given that does not match the
<var>type</var>.
</p>
<dl compact="compact">
<dt><code>hh</code></dt>
<dd><p>With <code>d</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>x</code>, or
<code>X</code>, specifies that the argument should be
converted to a <code>signed char</code> or <code>unsigned
char</code> before printing.
</p>
<p>With <code>n</code>, specifies that the argument is a
pointer to a <code>signed char</code>.
</p>
</dd>
<dt><code>h</code></dt>
<dd><p>With <code>d</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>x</code>, or
<code>X</code>, specifies that the argument should be
converted to a <code>short</code> or <code>unsigned short</code>
before printing.
</p>
<p>With <code>n</code>, specifies that the argument is a
pointer to a <code>short</code>.
</p>
</dd>
<dt><code>l</code></dt>
<dd><p>With <code>d</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>x</code>, or
<code>X</code>, specifies that the argument is a
<code>long</code> or <code>unsigned long</code>.
</p>
<p>With <code>c</code>, specifies that the argument has
type <code>wint_t</code>.
</p>
<p>With <code>s</code>, specifies that the argument is a
pointer to <code>wchar_t</code>.
</p>
<p>With <code>n</code>, specifies that the argument is a
pointer to a <code>long</code>.
</p>
<p>With <code>a</code>, <code>A</code>, <code>e</code>, <code>E</code>, <code>f</code>, <code>F</code>,
<code>g</code>, or <code>G</code>, has no effect (because of
vararg promotion rules, there is no need to
distinguish between <code>float</code> and <code>double</code>).
</p>
</dd>
<dt><code>ll</code></dt>
<dd><p>With <code>d</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>x</code>, or
<code>X</code>, specifies that the argument is a
<code>long long</code> or <code>unsigned long long</code>.
</p>
<p>With <code>n</code>, specifies that the argument is a
pointer to a <code>long long</code>.
</p>
</dd>
<dt><code>j</code></dt>
<dd><p>With <code>d</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>x</code>, or
<code>X</code>, specifies that the argument is an
<code>intmax_t</code> or <code>uintmax_t</code>.
</p>
<p>With <code>n</code>, specifies that the argument is a
pointer to an <code>intmax_t</code>.
</p>
</dd>
<dt><code>z</code></dt>
<dd><p>With <code>d</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>x</code>, or
<code>X</code>, specifies that the argument is a <code>size_t</code>.
</p>
<p>With <code>n</code>, specifies that the argument is a
pointer to a <code>size_t</code>.
</p>
</dd>
<dt><code>t</code></dt>
<dd><p>With <code>d</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>x</code>, or
<code>X</code>, specifies that the argument is a
<code>ptrdiff_t</code>.
</p>
<p>With <code>n</code>, specifies that the argument is a
pointer to a <code>ptrdiff_t</code>.
</p>
</dd>
<dt><code>L</code></dt>
<dd><p>With <code>a</code>, <code>A</code>, <code>e</code>, <code>E</code>, <code>f</code>, <code>F</code>,
<code>g</code>, or <code>G</code>, specifies that the argument
is a <code>long double</code>.
</p>
</dd>
</dl>
</li><li> <var>type</var>
<p><var>type</var> specifies what kind of conversion <code>printf</code>
performs. Here is a table of these:
</p>
<dl compact="compact">
<dt><code>%</code></dt>
<dd><p>Prints the percent character (<code>%</code>).
</p>
</dd>
<dt><code>c</code></dt>
<dd><p>Prints <var>arg</var> as single character. If the
<code>l</code> size specifier is in effect, a multibyte
character is printed.
</p>
</dd>
<dt><code>C</code></dt>
<dd><p>Short for <code>%lc</code>. A POSIX extension to the C standard.
</p>
</dd>
<dt><code>s</code></dt>
<dd><p>Prints the elements of a pointer to <code>char</code>
until the precision or a null character is
reached. If the <code>l</code> size specifier is in
effect, the pointer is to an array of
<code>wchar_t</code>, and the string is converted to
multibyte characters before printing.
</p>
</dd>
<dt><code>S</code></dt>
<dd><p>Short for <code>%ls</code>. A POSIX extension to the C standard.
</p>
</dd>
<dt><code>d or i</code></dt>
<dd><p>Prints a signed decimal integer; takes an
<code>int</code>. Leading zeros are inserted as
necessary to reach the precision. A value of 0 with
a precision of 0 produces an empty string.
</p>
</dd>
<dt><code>D</code></dt>
<dd><p>Newlib extension, short for <code>%ld</code>.
</p>
</dd>
<dt><code>o</code></dt>
<dd><p>Prints an unsigned octal integer; takes an
<code>unsigned</code>. Leading zeros are inserted as
necessary to reach the precision. A value of 0 with
a precision of 0 produces an empty string.
</p>
</dd>
<dt><code>O</code></dt>
<dd><p>Newlib extension, short for <code>%lo</code>.
</p>
</dd>
<dt><code>u</code></dt>
<dd><p>Prints an unsigned decimal integer; takes an
<code>unsigned</code>. Leading zeros are inserted as
necessary to reach the precision. A value of 0 with
a precision of 0 produces an empty string.
</p>
</dd>
<dt><code>U</code></dt>
<dd><p>Newlib extension, short for <code>%lu</code>.
</p>
</dd>
<dt><code>x</code></dt>
<dd><p>Prints an unsigned hexadecimal integer (using
<code>abcdef</code> as digits beyond <code>9</code>); takes an
<code>unsigned</code>. Leading zeros are inserted as
necessary to reach the precision. A value of 0 with
a precision of 0 produces an empty string.
</p>
</dd>
<dt><code>X</code></dt>
<dd><p>Like <code>x</code>, but uses <code>ABCDEF</code> as digits
beyond <code>9</code>.
</p>
</dd>
<dt><code>f</code></dt>
<dd><p>Prints a signed value of the form
<code>[-]9999.9999</code>, with the precision
determining how many digits follow the decimal
point; takes a <code>double</code> (remember that
<code>float</code> promotes to <code>double</code> as a vararg).
The low order digit is rounded to even. If
the precision results in at most DECIMAL_DIG
digits, the result is rounded correctly; if
more than DECIMAL_DIG digits are printed, the
result is only guaranteed to round back to the
original value.
</p>
<p>If the value is infinite, the result is
<code>inf</code>, and no zero padding is performed. If
the value is not a number, the result is
<code>nan</code>, and no zero padding is performed.
</p>
</dd>
<dt><code>F</code></dt>
<dd><p>Like <code>f</code>, but uses <code>INF</code> and <code>NAN</code> for
non-finite numbers.
</p>
</dd>
<dt><code>e</code></dt>
<dd><p>Prints a signed value of the form
<code>[-]9.9999e[+|-]999</code>; takes a <code>double</code>.
The digit before the decimal point is non-zero
if the value is non-zero. The precision
determines how many digits appear between
<code>.</code> and <code>e</code>, and the exponent always
contains at least two digits. The value zero
has an exponent of zero. If the value is not
finite, it is printed like <code>f</code>.
</p>
</dd>
<dt><code>E</code></dt>
<dd><p>Like <code>e</code>, but using <code>E</code> to introduce the
exponent, and like <code>F</code> for non-finite
values.
</p>
</dd>
<dt><code>g</code></dt>
<dd><p>Prints a signed value in either <code>f</code> or <code>e</code>
form, based on the given value and
precision—an exponent less than -4 or
greater than the precision selects the <code>e</code>
form. Trailing zeros and the decimal point
are printed only if necessary; takes a
<code>double</code>.
</p>
</dd>
<dt><code>G</code></dt>
<dd><p>Like <code>g</code>, except use <code>F</code> or <code>E</code> form.
</p>
</dd>
<dt><code>a</code></dt>
<dd><p>Prints a signed value of the form
<code>[-]0x1.ffffp[+|-]9</code>; takes a <code>double</code>.
The letters <code>abcdef</code> are used for digits
beyond <code>9</code>. The precision determines how
many digits appear after the decimal point.
The exponent contains at least one digit, and
is a decimal value representing the power of
2; a value of 0 has an exponent of 0.
Non-finite values are printed like <code>f</code>.
</p>
</dd>
<dt><code>A</code></dt>
<dd><p>Like <code>a</code>, except uses <code>X</code>, <code>P</code>, and
<code>ABCDEF</code> instead of lower case.
</p>
</dd>
<dt><code>n</code></dt>
<dd><p>Takes a pointer to <code>int</code>, and stores a count
of the number of bytes written so far. No
output is created.
</p>
</dd>
<dt><code>p</code></dt>
<dd><p>Takes a pointer to <code>void</code>, and prints it in
an implementation-defined format. This
implementation is similar to <code>%#tx</code>), except
that <code>0x</code> appears even for the NULL pointer.
</p>
</dd>
<dt><code>m</code></dt>
<dd><p>Prints the output of <code>strerror(errno)</code>; no
argument is required. A GNU extension.
</p>
</dd>
</dl>
</li></ul>
<p><code>_printf_r</code>, <code>_fprintf_r</code>, <code>_asprintf_r</code>,
<code>_sprintf_r</code>, <code>_snprintf_r</code>, <code>_asnprintf_r</code> are simply
reentrant versions of the functions above.
</p>
<br>
<p><strong>Returns</strong><br>
On success, <code>sprintf</code> and <code>asprintf</code> return the number of bytes in
the output string, except the concluding <code>NUL</code> is not counted.
<code>snprintf</code> returns the number of bytes that would be in the output
string, except the concluding <code>NUL</code> is not counted. <code>printf</code> and
<code>fprintf</code> return the number of characters transmitted.
<code>asnprintf</code> returns the original <var>str</var> if there was enough room,
otherwise it returns an allocated string.
</p>
<p>If an error occurs, the result of <code>printf</code>, <code>fprintf</code>,
<code>snprintf</code>, and <code>asprintf</code> is a negative value, and the result of
<code>asnprintf</code> is NULL. No error returns occur for <code>sprintf</code>. For
<code>printf</code> and <code>fprintf</code>, <code>errno</code> may be set according to
<code>fputc</code>. For <code>asprintf</code> and <code>asnprintf</code>, <code>errno</code> may be set
to ENOMEM if allocation fails, and for <code>snprintf</code>, <code>errno</code> may be
set to EOVERFLOW if <var>size</var> or the output length exceeds INT_MAX.
</p>
<br>
<p><strong>Bugs</strong><br>
The “”’ (quote) flag does not work when locale’s thousands_sep is not empty.
</p>
<br>
<p><strong>Portability</strong><br>
ANSI C requires <code>printf</code>, <code>fprintf</code>, <code>sprintf</code>, and
<code>snprintf</code>. <code>asprintf</code> and <code>asnprintf</code> are newlib extensions.
</p>
<p>The ANSI C standard specifies that implementations must support at
least formatted output of up to 509 characters. This implementation
has no inherent limit.
</p>
<p>Depending on how newlib was configured, not all format specifiers are
supported.
</p>
<p>Supporting OS subroutines required: <code>close</code>, <code>fstat</code>, <code>isatty</code>,
<code>lseek</code>, <code>read</code>, <code>sbrk</code>, <code>write</code>.
</p>
<br>
<hr>
<div class="header">
<p>
Next: <a href="sscanf.html#sscanf" accesskey="n" rel="next">sscanf</a>, Previous: <a href="siscanf.html#siscanf" accesskey="p" rel="prev">siscanf</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|