CINXE.COM

printf(3) - Linux manual page

<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>printf(3) - Linux manual page</title> <link rel="stylesheet" type="text/css" href="../../../style.css" title="style" /> <link rel="stylesheet" type="text/css" href="../style.css" title="style" /> </head> <body> <div class="page-top"><a id="top_of_page"></a></div> <!--%%%TOP_BAR%%%--> <div class="nav-bar"> <table class="nav-table"> <tr> <td class="nav-cell"> <p class="nav-text"> <a href="../../../index.html">man7.org</a> &gt; Linux &gt; <a href="../index.html">man-pages</a> </p> </td> <td class="training-cell"> <p class="training-text"><a class="training-link" href="http://man7.org/training/">Linux/UNIX system programming training</a></p> </td> </tr> </table> </div> <hr class="nav-end" /> <!--%%%PAGE_START%%%--> <h1>printf(3) &mdash; Linux manual page</h1> <table class="sec-table"> <tr> <td> <p class="section-dir"> <a href="#NAME">NAME</a> | <a href="#LIBRARY">LIBRARY</a> | <a href="#SYNOPSIS">SYNOPSIS</a> | <a href="#DESCRIPTION">DESCRIPTION</a> | <a href="#RETURN_VALUE">RETURN&nbsp;VALUE</a> | <a href="#ATTRIBUTES">ATTRIBUTES</a> | <a href="#STANDARDS">STANDARDS</a> | <a href="#HISTORY">HISTORY</a> | <a href="#CAVEATS">CAVEATS</a> | <a href="#BUGS">BUGS</a> | <a href="#EXAMPLES">EXAMPLES</a> | <a href="#SEE_ALSO">SEE&nbsp;ALSO</a> | <a href="#COLOPHON">COLOPHON</a> </p> </td> </tr> <tr> <td class="search-box"> <div class="man-search-box"> <form method="get" action="https://www.google.com/search"> <fieldset class="man-search"> <input type="text" name="q" size="10" maxlength="255" value="" /> <input type="hidden" name="sitesearch" value="man7.org/linux/man-pages" /> <input type="submit" name="sa" value="Search online pages" /> </fieldset> </form> </div> </td> <td> </td> </tr> </table> <!--%%%TEXT_START%%%--> <pre> <span class="headline"><i>printf</i>(3) Library Functions Manual <i>printf</i>(3)</span> </pre> <h2><a id="NAME" href="#NAME"></a>NAME &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf, vdprintf, vsprintf, vsnprintf - formatted output conversion </pre> <h2><a id="LIBRARY" href="#LIBRARY"></a>LIBRARY &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> Standard C library (<i>libc</i>, <i>-lc</i>) </pre> <h2><a id="SYNOPSIS" href="#SYNOPSIS"></a>SYNOPSIS &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> <b>#include &lt;stdio.h&gt;</b> <b>int printf(const char *restrict </b><i>format</i><b>, ...);</b> <b>int fprintf(FILE *restrict </b><i>stream</i><b>,</b> <b>const char *restrict </b><i>format</i><b>, ...);</b> <b>int dprintf(int </b><i>fd</i><b>,</b> <b>const char *restrict </b><i>format</i><b>, ...);</b> <b>int sprintf(char *restrict </b><i>str</i><b>,</b> <b>const char *restrict </b><i>format</i><b>, ...);</b> <b>int snprintf(char </b><i>str</i><b>[restrict .</b><i>size</i><b>], size_t </b><i>size</i><b>,</b> <b>const char *restrict </b><i>format</i><b>, ...);</b> <b>int vprintf(const char *restrict </b><i>format</i><b>, va_list </b><i>ap</i><b>);</b> <b>int vfprintf(FILE *restrict </b><i>stream</i><b>,</b> <b>const char *restrict </b><i>format</i><b>, va_list </b><i>ap</i><b>);</b> <b>int vdprintf(int </b><i>fd</i><b>,</b> <b>const char *restrict </b><i>format</i><b>, va_list </b><i>ap</i><b>);</b> <b>int vsprintf(char *restrict </b><i>str</i><b>,</b> <b>const char *restrict </b><i>format</i><b>, va_list </b><i>ap</i><b>);</b> <b>int vsnprintf(char </b><i>str</i><b>[restrict .</b><i>size</i><b>], size_t </b><i>size</i><b>,</b> <b>const char *restrict </b><i>format</i><b>, va_list </b><i>ap</i><b>);</b> Feature Test Macro Requirements for glibc (see <a href="../man7/feature_test_macros.7.html">feature_test_macros(7)</a>): <b>snprintf</b>(), <b>vsnprintf</b>(): _XOPEN_SOURCE &gt;= 500 || _ISOC99_SOURCE || /* glibc &lt;= 2.19: */ _BSD_SOURCE <b>dprintf</b>(), <b>vdprintf</b>(): Since glibc 2.10: _POSIX_C_SOURCE &gt;= 200809L Before glibc 2.10: _GNU_SOURCE </pre> <h2><a id="DESCRIPTION" href="#DESCRIPTION"></a>DESCRIPTION &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> The functions in the <b>printf</b>() family produce output according to a <i>format</i> as described below. The functions <b>printf</b>() and <b>vprintf</b>() write output to <i>stdout</i>, the standard output stream; <b>fprintf</b>() and <b>vfprintf</b>() write output to the given output <i>stream</i>; <b>sprintf</b>(), <b>snprintf</b>(), <b>vsprintf</b>(), and <b>vsnprintf</b>() write to the character string <i>str</i>. The function <b>dprintf</b>() is the same as <b>fprintf</b>() except that it outputs to a file descriptor, <i>fd</i>, instead of to a <a href="../man3/stdio.3.html">stdio(3)</a> stream. The functions <b>snprintf</b>() and <b>vsnprintf</b>() write at most <i>size</i> bytes (including the terminating null byte ('\0')) to <i>str</i>. The functions <b>vprintf</b>(), <b>vfprintf</b>(), <b>vdprintf</b>(), <b>vsprintf</b>(), <b>vsnprintf</b>() are equivalent to the functions <b>printf</b>(), <b>fprintf</b>(), <b>dprintf</b>(), <b>sprintf</b>(), <b>snprintf</b>(), respectively, except that they are called with a <i>va_list</i> instead of a variable number of arguments. These functions do not call the <i>va_end</i> macro. Because they invoke the <i>va_arg</i> macro, the value of <i>ap</i> is undefined after the call. See <a href="../man3/stdarg.3.html">stdarg(3)</a>. All of these functions write the output under the control of a <i>format</i> string that specifies how subsequent arguments (or arguments accessed via the variable-length argument facilities of <a href="../man3/stdarg.3.html">stdarg(3)</a>) are converted for output. C99 and POSIX.1-2001 specify that the results are undefined if a call to <b>sprintf</b>(), <b>snprintf</b>(), <b>vsprintf</b>(), or <b>vsnprintf</b>() would cause copying to take place between objects that overlap (e.g., if the target string array and one of the supplied input arguments refer to the same buffer). See CAVEATS. <b>Format of the format string</b> The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: ordinary characters (not <b>%</b>), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the character <b>%</b>, and ends with a <i>conversion specifier</i>. In between there may be (in this order) zero or more <i>flags</i>, an optional minimum <i>field width</i>, an optional <i>precision</i> and an optional <i>length modifier</i>. The overall syntax of a conversion specification is: %[argument$][flags][width][.precision][length modifier]conversion The arguments must correspond properly (after type promotion) with the conversion specifier. By default, the arguments are used in the order given, where each '*' (see <i>Field width</i> and <i>Precision</i> below) and each conversion specifier asks for the next argument (and it is an error if insufficiently many arguments are given). One can also specify explicitly which argument is taken, at each place where an argument is required, by writing "%m$" instead of '%' and "*m$" instead of '*', where the decimal integer <i>m</i> denotes the position in the argument list of the desired argument, indexed starting from 1. Thus, printf("%*d", width, num); and printf("%2$*1$d", width, num); are equivalent. The second style allows repeated references to the same argument. The C99 standard does not include the style using '$', which comes from the Single UNIX Specification. If the style using '$' is used, it must be used throughout for all conversions taking an argument and all width and precision arguments, but it may be mixed with "%%" formats, which do not consume an argument. There may be no gaps in the numbers of arguments specified using '$'; for example, if arguments 1 and 3 are specified, argument 2 must also be specified somewhere in the format string. For some numeric conversions a radix character ("decimal point") or thousands' grouping character is used. The actual character used depends on the <b>LC_NUMERIC </b>part of the locale. (See <a href="../man3/setlocale.3.html">setlocale(3)</a>.) The POSIX locale uses '.' as radix character, and does not have a grouping character. Thus, printf("%'.2f", 1234567.89); results in "1234567.89" in the POSIX locale, in "1234567,89" in the nl_NL locale, and in "1.234.567,89" in the da_DK locale. <b>Flag characters</b> The character % is followed by zero or more of the following flags: <b># </b>The value should be converted to an "alternate form". For <b>o </b>conversions, the first character of the output string is made zero (by prefixing a 0 if it was not zero already). For <b>x </b>and <b>X </b>conversions, a nonzero result has the string "0x" (or "0X" for <b>X </b>conversions) prepended to it. For <b>a</b>, <b>A</b>, <b>e</b>, <b>E</b>, <b>f</b>, <b>F</b>, <b>g</b>, and <b>G </b>conversions, the result will always contain a decimal point, even if no digits follow it (normally, a decimal point appears in the results of those conversions only if a digit follows). For <b>g </b>and <b>G</b> conversions, trailing zeros are not removed from the result as they would otherwise be. For <b>m</b>, if <i><a href="../man3/errno.3.html">errno</a></i> contains a valid error code, the output of <i>strerrorname_np(errno)</i> is printed; otherwise, the value stored in <i><a href="../man3/errno.3.html">errno</a></i> is printed as a decimal number. For other conversions, the result is undefined. <b>0 </b>The value should be zero padded. For <b>d</b>, <b>i</b>, <b>o</b>, <b>u</b>, <b>x</b>, <b>X</b>, <b>a</b>, <b>A</b>, <b>e</b>, <b>E</b>, <b>f</b>, <b>F</b>, <b>g</b>, and <b>G </b>conversions, the converted value is padded on the left with zeros rather than blanks. If the <b>0</b> and <b>- </b>flags both appear, the <b>0 </b>flag is ignored. If a precision is given with an integer conversion (<b>d</b>, <b>i</b>, <b>o</b>, <b>u</b>, <b>x</b>, and <b>X</b>), the <b>0 </b>flag is ignored. For other conversions, the behavior is undefined. <b>- </b>The converted value is to be left adjusted on the field boundary. (The default is right justification.) The converted value is padded on the right with blanks, rather than on the left with blanks or zeros. A <b>- </b>overrides a <b>0</b> if both are given. <b>' ' </b>(a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. <b>+ </b>A sign (+ or -) should always be placed before a number produced by a signed conversion. By default, a sign is used only for negative numbers. A <b>+ </b>overrides a space if both are used. The five flag characters above are defined in the C99 standard. POSIX specifies one further flag character. <b>' </b>For decimal conversion (<b>i</b>, <b>d</b>, <b>u</b>, <b>f</b>, <b>F</b>, <b>g</b>, <b>G</b>) the output is to be grouped with thousands' grouping characters as a <i>non-</i> <i>monetary</i> quantity. Misleadingly, this isn't necessarily every thousand: for example Karbi ("mjw_IN"), groups its digits into 3 once, then 2 repeatedly. Compare <a href="../man7/locale.7.html">locale(7)</a> <i>grouping</i> and <i>thousands_sep</i>, contrast with <i>mon_grouping</i>/<i>mon_thousands_sep</i> and <a href="../man3/strfmon.3.html">strfmon(3)</a>. This is a no-op in the default "C" locale. glibc 2.2 adds one further flag character. <b>I </b>For decimal integer conversion (<b>i</b>, <b>d</b>, <b>u</b>) the output uses the locale's alternative output digits, if any. For example, since glibc 2.2.3 this will give Arabic-Indic digits in the Persian ("fa_IR") locale. <b>Field width</b> An optional decimal digit string (with nonzero first digit) specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given). Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer <i>m</i>) to specify that the field width is given in the next argument, or in the <i>m</i>-th argument, respectively, which must be of type <i>int</i>. A negative field width is taken as a '-' flag followed by a positive field width. In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result. <b>Precision</b> An optional precision, in the form of a period ('.') followed by an optional decimal digit string. Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer <i>m</i>) to specify that the precision is given in the next argument, or in the <i>m</i>-th argument, respectively, which must be of type <i>int</i>. If the precision is given as just '.', the precision is taken to be zero. A negative precision is taken as if the precision were omitted. This gives the minimum number of digits to appear for <b>d</b>, <b>i</b>, <b>o</b>, <b>u</b>, <b>x</b>, and <b>X </b>conversions, the number of digits to appear after the radix character for <b>a</b>, <b>A</b>, <b>e</b>, <b>E</b>, <b>f</b>, and <b>F </b>conversions, the maximum number of significant digits for <b>g </b>and <b>G </b>conversions, or the maximum number of characters to be printed from a string for <b>s </b>and <b>S </b>conversions. <b>Length modifier</b> Here, "integer conversion" stands for <b>d</b>, <b>i</b>, <b>o</b>, <b>u</b>, <b>x</b>, or <b>X</b> conversion. <b>hh </b>A following integer conversion corresponds to a <i>signed char</i> or <i>unsigned char</i> argument, or a following <b>n </b>conversion corresponds to a pointer to a <i>signed char</i> argument. <b>h </b>A following integer conversion corresponds to a <i>short</i> or <i>unsigned short</i> argument, or a following <b>n </b>conversion corresponds to a pointer to a <i>short</i> argument. <b>l </b>(ell) A following integer conversion corresponds to a <i>long</i> or <i>unsigned long</i> argument, or a following <b>n </b>conversion corresponds to a pointer to a <i>long</i> argument, or a following <b>c </b>conversion corresponds to a <i>wint_t</i> argument, or a following <b>s </b>conversion corresponds to a pointer to <i>wchar_t</i> argument. On a following <b>a</b>, <b>A</b>, <b>e</b>, <b>E</b>, <b>f</b>, <b>F</b>, <b>g</b>, or <b>G</b> conversion, this length modifier is ignored (C99; not in SUSv2). <b>ll </b>(ell-ell). A following integer conversion corresponds to a <i>long long</i> or <i>unsigned long long</i> argument, or a following <b>n</b> conversion corresponds to a pointer to a <i>long long</i> argument. <b>q </b>A synonym for <b>ll</b>. This is a nonstandard extension, derived from BSD; avoid its use in new code. <b>L </b>A following <b>a</b>, <b>A</b>, <b>e</b>, <b>E</b>, <b>f</b>, <b>F</b>, <b>g</b>, or <b>G </b>conversion corresponds to a <i>long double</i> argument. (C99 allows %LF, but SUSv2 does not.) <b>j </b>A following integer conversion corresponds to an <i>intmax_t</i> or <i>uintmax_t</i> argument, or a following <b>n </b>conversion corresponds to a pointer to an <i>intmax_t</i> argument. <b>z </b>A following integer conversion corresponds to a <i>size_t</i> or <i>ssize_t</i> argument, or a following <b>n </b>conversion corresponds to a pointer to a <i>size_t</i> argument. <b>Z </b>A nonstandard synonym for <b>z </b>that predates the appearance of <b>z</b>. Do not use in new code. <b>t </b>A following integer conversion corresponds to a <i>ptrdiff_t</i> argument, or a following <b>n </b>conversion corresponds to a pointer to a <i>ptrdiff_t</i> argument. SUSv3 specifies all of the above, except for those modifiers explicitly noted as being nonstandard extensions. SUSv2 specified only the length modifiers <b>h </b>(in <b>hd</b>, <b>hi</b>, <b>ho</b>, <b>hx</b>, <b>hX</b>, <b>hn</b>) and <b>l </b>(in <b>ld</b>, <b>li</b>, <b>lo</b>, <b>lx</b>, <b>lX</b>, <b>ln</b>, <b>lc</b>, <b>ls</b>) and <b>L </b>(in <b>Le</b>, <b>LE</b>, <b>Lf</b>, <b>Lg</b>, <b>LG</b>). As a nonstandard extension, the GNU implementations treats <b>ll </b>and <b>L </b>as synonyms, so that one can, for example, write <b>llg </b>(as a synonym for the standards-compliant <b>Lg</b>) and <b>Ld </b>(as a synonym for the standards compliant <b>lld</b>). Such usage is nonportable. <b>Conversion specifiers</b> A character that specifies the type of conversion to be applied. The conversion specifiers and their meanings are: <b>d</b>, <b>i </b>The <i>int</i> argument is converted to signed decimal notation. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. The default precision is 1. When 0 is printed with an explicit precision 0, the output is empty. <b>o</b>, <b>u</b>, <b>x</b>, <b>X</b> The <i>unsigned int</i> argument is converted to unsigned octal (<b>o</b>), unsigned decimal (<b>u</b>), or unsigned hexadecimal (<b>x </b>and <b>X</b>) notation. The letters <b>abcdef </b>are used for <b>x</b> conversions; the letters <b>ABCDEF </b>are used for <b>X </b>conversions. The precision, if any, gives the minimum number of digits that must appear; if the converted value requires fewer digits, it is padded on the left with zeros. The default precision is 1. When 0 is printed with an explicit precision 0, the output is empty. <b>e</b>, <b>E </b>The <i>double</i> argument is rounded and converted in the style [-]d<b>.</b>ddd<b>e</b>±dd where there is one digit (which is nonzero if the argument is nonzero) before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears. An <b>E </b>conversion uses the letter <b>E </b>(rather than <b>e</b>) to introduce the exponent. The exponent always contains at least two digits; if the value is zero, the exponent is 00. <b>f</b>, <b>F </b>The <i>double</i> argument is rounded and converted to decimal notation in the style [-]ddd<b>.</b>ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it. (SUSv2 does not know about <b>F </b>and says that character string representations for infinity and NaN may be made available. SUSv3 adds a specification for <b>F</b>. The C99 standard specifies "[-]inf" or "[-]infinity" for infinity, and a string starting with "nan" for NaN, in the case of <b>f</b> conversion, and "[-]INF" or "[-]INFINITY" or "NAN" in the case of <b>F </b>conversion.) <b>g</b>, <b>G </b>The <i>double</i> argument is converted in style <b>f </b>or <b>e </b>(or <b>F </b>or <b>E</b> for <b>G </b>conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style <b>e </b>is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit. <b>a</b>, <b>A </b>(C99; not in SUSv2, but added in SUSv3) For <b>a </b>conversion, the <i>double</i> argument is converted to hexadecimal notation (using the letters abcdef) in the style [-]<b>0x</b>h<b>.</b>hhhh<b>p</b>±d; for <b>A </b>conversion the prefix <b>0X</b>, the letters ABCDEF, and the exponent separator <b>P </b>is used. There is one hexadecimal digit before the radix point, and the number of digits after it is equal to the precision. The default precision suffices for an exact representation of the value if an exact representation in base 2 exists and otherwise is sufficiently large to distinguish values of type <i>double</i>. The digit before the radix point is unspecified for nonnormalized numbers, and nonzero but otherwise unspecified for normalized numbers. The exponent, <i>d</i>, is the appropriate exponent of 2 expressed as a decimal integer; it always contains at least one digit; if the value is zero, the exponent is 0. <b>c </b>If no <b>l </b>modifier is present, the <i>int</i> argument is converted to an <i>unsigned char</i>, and the resulting character is written. If an <b>l </b>modifier is present, the <i>wint_t</i> (wide character) argument is converted to a multibyte sequence by a call to the <a href="../man3/wcrtomb.3.html">wcrtomb(3)</a> function, with a conversion state starting in the initial state, and the resulting multibyte string is written. <b>s </b>If no <b>l </b>modifier is present: the <i>const char *</i> argument is expected to be a pointer to an array of character type (pointer to a string). Characters from the array are written up to (but not including) a terminating null byte ('\0'); if a precision is specified, no more than the number specified are written. If a precision is given, no null byte need be present; if the precision is not specified, or is greater than the size of the array, the array must contain a terminating null byte. If an <b>l </b>modifier is present: the <i>const wchar_t *</i> argument is expected to be a pointer to an array of wide characters. Wide characters from the array are converted to multibyte characters (each by a call to the <a href="../man3/wcrtomb.3.html">wcrtomb(3)</a> function, with a conversion state starting in the initial state before the first wide character), up to and including a terminating null wide character. The resulting multibyte characters are written up to (but not including) the terminating null byte. If a precision is specified, no more bytes than the number specified are written, but no partial multibyte characters are written. Note that the precision determines the number of <i>bytes</i> written, not the number of <i>wide</i> <i>characters</i> or <i>screen positions</i>. The array must contain a terminating null wide character, unless a precision is given and it is so small that the number of bytes written exceeds it before the end of the array is reached. <b>C </b>(Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.) Synonym for <b>lc</b>. Don't use. <b>S </b>(Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.) Synonym for <b>ls</b>. Don't use. <b>p </b>The <i>void *</i> pointer argument is printed in hexadecimal (as if by <b>%#x </b>or <b>%#lx</b>). <b>n </b>The number of characters written so far is stored into the integer pointed to by the corresponding argument. That argument shall be an <i>int *</i>, or variant whose size matches the (optionally) supplied integer length modifier. No argument is converted. (This specifier is not supported by the bionic C library.) The behavior is undefined if the conversion specification includes any flags, a field width, or a precision. <b>m </b>(glibc extension; supported by uClibc and musl.) Print output of <i>strerror(errno)</i> (or <i>strerrorname_np(errno)</i> in the alternate form). No argument is required. <b>% </b>A '%' is written. No argument is converted. The complete conversion specification is '%%'. </pre> <h2><a id="RETURN_VALUE" href="#RETURN_VALUE"></a>RETURN VALUE &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> Upon successful return, these functions return the number of bytes printed (excluding the null byte used to end output to strings). The functions <b>snprintf</b>() and <b>vsnprintf</b>() do not write more than <i>size</i> bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit, then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of <i>size</i> or more means that the output was truncated. (See also below under CAVEATS.) If an output error is encountered, a negative value is returned. </pre> <h2><a id="ATTRIBUTES" href="#ATTRIBUTES"></a>ATTRIBUTES &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> For an explanation of the terms used in this section, see <a href="../man7/attributes.7.html">attributes(7)</a>. ┌───────────────────────────────┬───────────────┬────────────────┐ │ <b>Interface </b>│ <b>Attribute </b>│ <b>Value </b>│ ├───────────────────────────────┼───────────────┼────────────────┤ │ <b>printf</b>(), <b>fprintf</b>(), │ Thread safety │ MT-Safe locale │ │ <b>sprintf</b>(), <b>snprintf</b>(), │ │ │ │ <b>vprintf</b>(), <b>vfprintf</b>(), │ │ │ │ <b>vsprintf</b>(), <b>vsnprintf</b>() │ │ │ └───────────────────────────────┴───────────────┴────────────────┘ </pre> <h2><a id="STANDARDS" href="#STANDARDS"></a>STANDARDS &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> <b>fprintf</b>() <b>printf</b>() <b>sprintf</b>() <b>vprintf</b>() <b>vfprintf</b>() <b>vsprintf</b>() <b>snprintf</b>() <b>vsnprintf</b>() C11, POSIX.1-2008. <b>dprintf</b>() <b>vdprintf</b>() GNU, POSIX.1-2008. </pre> <h2><a id="HISTORY" href="#HISTORY"></a>HISTORY &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> <b>fprintf</b>() <b>printf</b>() <b>sprintf</b>() <b>vprintf</b>() <b>vfprintf</b>() <b>vsprintf</b>() C89, POSIX.1-2001. <b>snprintf</b>() <b>vsnprintf</b>() SUSv2, C99, POSIX.1-2001. Concerning the return value of <b>snprintf</b>(), SUSv2 and C99 contradict each other: when <b>snprintf</b>() is called with <i>size</i>=0 then SUSv2 stipulates an unspecified return value less than 1, while C99 allows <i>str</i> to be NULL in this case, and gives the return value (as always) as the number of characters that would have been written in case the output string has been large enough. POSIX.1-2001 and later align their specification of <b>snprintf</b>() with C99. <b>dprintf</b>() <b>vdprintf</b>() GNU, POSIX.1-2008. Issue 4 of the X/Open Portability Guide (SUSv1, 1994) adds <b>'</b>. glibc 2.1 adds length modifiers <b>hh</b>, <b>j</b>, <b>t</b>, and <b>z </b>and conversion characters <b>a </b>and <b>A</b>. glibc 2.2 adds the conversion character <b>F </b>with C99 semantics, and the flag character <b>I</b>. glibc 2.35 gives a meaning to the alternate form (<b>#</b>) of the <b>m</b> conversion specifier, that is <i>%#m</i>. </pre> <h2><a id="CAVEATS" href="#CAVEATS"></a>CAVEATS &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> Some programs imprudently rely on code such as the following sprintf(buf, "%s some further text", buf); to append text to <i>buf</i>. However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling <b>sprintf</b>(), <b>snprintf</b>(), <b>vsprintf</b>(), and <b>vsnprintf</b>(). Depending on the version of <a href="../man1/gcc.1.html">gcc(1)</a> used, and the compiler options employed, calls such as the above will <b>not</b> produce the expected results. The glibc implementation of the functions <b>snprintf</b>() and <b>vsnprintf</b>() conforms to the C99 standard, that is, behaves as described above, since glibc 2.1. Until glibc 2.0.6, they would return -1 when the output was truncated. </pre> <h2><a id="BUGS" href="#BUGS"></a>BUGS &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> Because <b>sprintf</b>() and <b>vsprintf</b>() assume an arbitrarily long string, callers must be careful not to overflow the actual space; this is often impossible to assure. Note that the length of the strings produced is locale-dependent and difficult to predict. Use <b>snprintf</b>() and <b>vsnprintf</b>() instead (or <a href="../man3/asprintf.3.html">asprintf(3)</a> and <a href="../man3/vasprintf.3.html">vasprintf(3)</a>). Code such as <b>printf(</b><i>foo</i><b>); </b>often indicates a bug, since <i>foo</i> may contain a % character. If <i>foo</i> comes from untrusted user input, it may contain <b>%n</b>, causing the <b>printf</b>() call to write to memory and creating a security hole. </pre> <h2><a id="EXAMPLES" href="#EXAMPLES"></a>EXAMPLES &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> To print <i>Pi</i> to five decimal places: #include &lt;math.h&gt; #include &lt;stdio.h&gt; fprintf(stdout, "pi = %.5f\n", 4 * atan(1.0)); To print a date and time in the form "Sunday, July 3, 10:02", where <i>weekday</i> and <i>month</i> are pointers to strings: #include &lt;stdio.h&gt; fprintf(stdout, "%s, %s %d, %.2d:%.2d\n", weekday, month, day, hour, min); Many countries use the day-month-year order. Hence, an internationalized version must be able to print the arguments in an order specified by the format: #include &lt;stdio.h&gt; fprintf(stdout, format, weekday, month, day, hour, min); where <i>format</i> depends on locale, and may permute the arguments. With the value: "%1$s, %3$d. %2$s, %4$d:%5$.2d\n" one might obtain "Sonntag, 3. Juli, 10:02". To allocate a sufficiently large string and print into it (code correct for both glibc 2.0 and glibc 2.1): #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdarg.h&gt; char * make_message(const char *fmt, ...) { int n = 0; size_t size = 0; char *p = NULL; va_list ap; /* Determine required size. */ va_start(ap, fmt); n = vsnprintf(p, size, fmt, ap); va_end(ap); if (n &lt; 0) return NULL; size = (size_t) n + 1; /* One extra byte for '\0' */ p = malloc(size); if (p == NULL) return NULL; va_start(ap, fmt); n = vsnprintf(p, size, fmt, ap); va_end(ap); if (n &lt; 0) { free(p); return NULL; } return p; } If truncation occurs in glibc versions prior to glibc 2.0.6, this is treated as an error instead of being handled gracefully. </pre> <h2><a id="SEE_ALSO" href="#SEE_ALSO"></a>SEE ALSO &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> <a href="../man1/printf.1.html">printf(1)</a>, <a href="../man3/asprintf.3.html">asprintf(3)</a>, <a href="../man3/puts.3.html">puts(3)</a>, <a href="../man3/scanf.3.html">scanf(3)</a>, <a href="../man3/setlocale.3.html">setlocale(3)</a>, <a href="../man3/strfromd.3.html">strfromd(3)</a>, <a href="../man3/wcrtomb.3.html">wcrtomb(3)</a>, <a href="../man3/wprintf.3.html">wprintf(3)</a>, <a href="../man5/locale.5.html">locale(5)</a> </pre> <h2><a id="COLOPHON" href="#COLOPHON"></a>COLOPHON &nbsp; &nbsp; &nbsp; &nbsp; <a href="#top_of_page"><span class="top-link">top</span></a></h2><pre> This page is part of the <i>man-pages</i> (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨<a href="https://www.kernel.org/doc/man-pages/">https://www.kernel.org/doc/man-pages/</a>⟩. If you have a bug report for this manual page, see ⟨<a href="https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING">https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING</a>⟩. This page was obtained from the tarball man-pages-6.10.tar.gz fetched from ⟨<a href="https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/">https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/</a>⟩ on 2025-02-02. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which is <i>not</i> part of the original manual page), send a mail to man-pages@man7.org <span class="footline">Linux man-pages 6.10 2024-11-17 <i>printf</i>(3)</span> </pre> <hr class="end-man-text" /> <p>Pages that refer to this page: <a href="../man1/find.1.html">find(1)</a>,&nbsp; <a href="../man1/gawk.1.html">gawk(1)</a>,&nbsp; <a href="../man1/hexdump.1.html">hexdump(1)</a>,&nbsp; <a href="../man1/printf.1.html">printf(1)</a>,&nbsp; <a href="../man1/ps.1.html">ps(1)</a>,&nbsp; <a href="../man1/time.1.html">time(1)</a>,&nbsp; <a href="../man2/perf_event_open.2.html">perf_event_open(2)</a>,&nbsp; <a href="../man3/asprintf.3.html">asprintf(3)</a>,&nbsp; <a href="../man3/avc_init.3.html">avc_init(3)</a>,&nbsp; <a href="../man3/curs_printw.3x.html">curs_printw(3x)</a>,&nbsp; <a href="../man3/curs_termcap.3x.html">curs_termcap(3x)</a>,&nbsp; <a href="../man3/ecvt.3.html">ecvt(3)</a>,&nbsp; <a href="../man3/ecvt_r.3.html">ecvt_r(3)</a>,&nbsp; <a href="../man3/err.3.html">err(3)</a>,&nbsp; <a href="../man3/errno.3.html">errno(3)</a>,&nbsp; <a href="../man3/error.3.html">error(3)</a>,&nbsp; <a href="../man3/FILE.3type.html">FILE(3type)</a>,&nbsp; <a href="../man3/form_field_validation.3x.html">form_field_validation(3x)</a>,&nbsp; <a href="../man3/fwide.3.html">fwide(3)</a>,&nbsp; <a href="../man3/gcvt.3.html">gcvt(3)</a>,&nbsp; <a href="../man3/intmax_t.3type.html">intmax_t(3type)</a>,&nbsp; <a href="../man3/intN_t.3type.html">intN_t(3type)</a>,&nbsp; <a href="../man3/intptr_t.3type.html">intptr_t(3type)</a>,&nbsp; <a href="../man3/lber-encode.3.html">lber-encode(3)</a>,&nbsp; <a href="../man3/localeconv.3.html">localeconv(3)</a>,&nbsp; <a href="../man3/lttng-ust.3.html">lttng-ust(3)</a>,&nbsp; <a href="../man3/lttng_ust_tracef.3.html">lttng_ust_tracef(3)</a>,&nbsp; <a href="../man3/lttng_ust_tracelog.3.html">lttng_ust_tracelog(3)</a>,&nbsp; <a href="../man3/numa.3.html">numa(3)</a>,&nbsp; <a href="../man3/pmextractvalue.3.html">pmextractvalue(3)</a>,&nbsp; <a href="../man3/pmnotifyerr.3.html">pmnotifyerr(3)</a>,&nbsp; <a href="../man3/pmnumberstr.3.html">pmnumberstr(3)</a>,&nbsp; <a href="../man3/pmprintf.3.html">pmprintf(3)</a>,&nbsp; <a href="../man3/pmsprintf.3.html">pmsprintf(3)</a>,&nbsp; <a href="../man3/printf.h.3head.html">printf.h(3head)</a>,&nbsp; <a href="../man3/ptrdiff_t.3type.html">ptrdiff_t(3type)</a>,&nbsp; <a href="../man3/qecvt.3.html">qecvt(3)</a>,&nbsp; <a href="../man3/rpc.3.html">rpc(3)</a>,&nbsp; <a href="../man3/scanf.3.html">scanf(3)</a>,&nbsp; <a href="../man3/sd_bus_error.3.html">sd_bus_error(3)</a>,&nbsp; <a href="../man3/sd_bus_message_new_method_error.3.html">sd_bus_message_new_method_error(3)</a>,&nbsp; <a href="../man3/sd-daemon.3.html">sd-daemon(3)</a>,&nbsp; <a href="../man3/sd-id128.3.html">sd-id128(3)</a>,&nbsp; <a href="../man3/sd_id128_to_string.3.html">sd_id128_to_string(3)</a>,&nbsp; <a href="../man3/sd_journal_print.3.html">sd_journal_print(3)</a>,&nbsp; <a href="../man3/sd_journal_stream_fd.3.html">sd_journal_stream_fd(3)</a>,&nbsp; <a href="../man3/selinux_set_callback.3.html">selinux_set_callback(3)</a>,&nbsp; <a href="../man3/setbuf.3.html">setbuf(3)</a>,&nbsp; <a href="../man3/size_t.3type.html">size_t(3type)</a>,&nbsp; <a href="../man3/sscanf.3.html">sscanf(3)</a>,&nbsp; <a href="../man3/stdarg.3.html">stdarg(3)</a>,&nbsp; <a href="../man3/stdin.3.html">stdin(3)</a>,&nbsp; <a href="../man3/stdio.3.html">stdio(3)</a>,&nbsp; <a href="../man3/strfmon.3.html">strfmon(3)</a>,&nbsp; <a href="../man3/strfromd.3.html">strfromd(3)</a>,&nbsp; <a href="../man3/strftime.3.html">strftime(3)</a>,&nbsp; <a href="../man3/syslog.3.html">syslog(3)</a>,&nbsp; <a href="../man3/uuid_parse.3.html">uuid_parse(3)</a>,&nbsp; <a href="../man3/void.3type.html">void(3type)</a>,&nbsp; <a href="../man3/wprintf.3.html">wprintf(3)</a>,&nbsp; <a href="../man5/slapd.conf.5.html">slapd.conf(5)</a>,&nbsp; <a href="../man5/slapd-config.5.html">slapd-config(5)</a>,&nbsp; <a href="../man5/terminfo.5.html">terminfo(5)</a>,&nbsp; <a href="../man7/feature_test_macros.7.html">feature_test_macros(7)</a>,&nbsp; <a href="../man7/locale.7.html">locale(7)</a>,&nbsp; <a href="../man7/signal-safety.7.html">signal-safety(7)</a>,&nbsp; <a href="../man7/system_data_types.7.html">system_data_types(7)</a>,&nbsp; <a href="../man8/btrfstune.8.html">btrfstune(8)</a>,&nbsp; <a href="../man8/rpm.8.html">rpm(8)</a>,&nbsp; <a href="../man8/slappasswd.8.html">slappasswd(8)</a> </p> <hr/> <hr class="start-footer" /> <div class="footer"> <table class="colophon-table"> <tr> <td class="pub-info"> <p> HTML rendering created 2025-02-02 by <a href="https://man7.org/mtk/index.html">Michael Kerrisk</a>, author of <a href="https://man7.org/tlpi/"><em>The Linux Programming Interface</em></a>. </p> <p> For details of in-depth <strong>Linux/UNIX system programming training courses</strong> that I teach, look <a href="https://man7.org/training/">here</a>. </p> <p> Hosting by <a href="https://www.jambit.com/index_en.html">jambit GmbH</a>. </p> </td> <td class="colophon-divider"> </td> <td class="tlpi-cover"> <a href="https://man7.org/tlpi/"><img src="https://man7.org/tlpi/cover/TLPI-front-cover-vsmall.png" alt="Cover of TLPI" /></a> </td> </tr> </table> </div> <hr class="end-footer" /> <!--BEGIN-SITETRACKING--> <!-- SITETRACKING.man7.org_linux_man-pages --> <!-- Default Statcounter code for man7.org/linux/man-pages http://www.man7.org/linux/man-pages --> <script type="text/javascript"> var sc_project=7422636; var sc_invisible=1; var sc_security="9b6714ff"; </script> <script type="text/javascript" src="https://www.statcounter.com/counter/counter.js" async></script> <noscript><div class="statcounter"><a title="Web Analytics Made Easy - StatCounter" href="https://statcounter.com/" target="_blank"><img class="statcounter" src="https://c.statcounter.com/7422636/0/9b6714ff/1/" alt="Web Analytics Made Easy - StatCounter"></a></div></noscript> <!-- End of Statcounter Code --> <!-- Start of Google Analytics Code --> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-9830363-8']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> <!-- End of Google Analytics Code --> <!--END-SITETRACKING--> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10