Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

668 lines
25KB

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