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