]> git.ipfire.org Git - thirdparty/man-pages.git/blobdiff - man3/printf.3
fanotify_init.2, fanotify.7: Document FAN_REPORT_TID
[thirdparty/man-pages.git] / man3 / printf.3
index fb3b27fc0b206632b37152ccda382e302eb43ee6..7dc136f904d8dd47875e7038c3caa7282229543f 100644 (file)
 .\" 2000-07-26 jsm28@hermes.cam.ac.uk - three small fixes
 .\" 2000-10-16 jsm28@hermes.cam.ac.uk - more fixes
 .\"
-.TH PRINTF 3  2016-12-12 "GNU" "Linux Programmer's Manual"
+.TH PRINTF 3  2017-09-15 "GNU" "Linux Programmer's Manual"
 .SH NAME
 printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf, vdprintf,
 vsprintf, vsnprintf \- formatted output conversion
 .SH SYNOPSIS
+.nf
 .B #include <stdio.h>
-.sp
+.PP
 .BI "int printf(const char *" format ", ...);"
-.br
 .BI "int fprintf(FILE *" stream ", const char *" format ", ...);"
-.br
 .BI "int dprintf(int " fd ", const char *" format ", ...);"
-.br
 .BI "int sprintf(char *" str ", const char *" format ", ...);"
-.br
 .BI "int snprintf(char *" str ", size_t " size ", const char *" format ", ...);"
-.sp
+
 .B #include <stdarg.h>
-.sp
+.PP
 .BI "int vprintf(const char *" format ", va_list " ap );
-.br
 .BI "int vfprintf(FILE *" stream ", const char *" format ", va_list " ap );
-.br
 .BI "int vdprintf(int " fd ", const char *" format ", va_list " ap );
-.br
 .BI "int vsprintf(char *" str ", const char *" format ", va_list " ap );
-.br
 .BI "int vsnprintf(char *" str ", size_t " size ", const char *" format \
 ", va_list " ap );
-.sp
+.fi
+.PP
 .in -4n
 Feature Test Macro Requirements for glibc (see
 .BR feature_test_macros (7)):
 .in
-.sp
+.PP
 .ad l
 .BR snprintf (),
 .BR vsnprintf ():
@@ -73,7 +67,7 @@ Feature Test Macro Requirements for glibc (see
 _XOPEN_SOURCE\ >=\ 500 || _ISOC99_SOURCE ||
     || /* Glibc versions <= 2.19: */ _BSD_SOURCE
 .RE
-.sp
+.PP
 .BR dprintf (),
 .BR vdprintf ():
 .PD 0
@@ -112,7 +106,7 @@ and
 .BR vsnprintf ()
 write to the character string
 .IR str .
-
+.PP
 The function
 .BR dprintf ()
 is the same as
@@ -122,7 +116,7 @@ except that it outputs to a file descriptor,
 instead of to a
 .I stdio
 stream.
-
+.PP
 The functions
 .BR snprintf ()
 and
@@ -131,7 +125,7 @@ write at most
 .I size
 bytes (including the terminating null byte (\(aq\e0\(aq)) to
 .IR str .
-
+.PP
 The functions
 .BR vprintf (),
 .BR vfprintf (),
@@ -157,14 +151,14 @@ macro, the value of
 is undefined after the call.
 See
 .BR stdarg (3).
-
+.PP
 All of these functions write the output under the control of a
 .I format
 string that specifies how subsequent arguments (or arguments accessed via
 the variable-length argument facilities of
 .BR stdarg (3))
 are converted for output.
-
+.PP
 C99 and POSIX.1-2001 specify that the results are undefined if a call to
 .BR sprintf (),
 .BR snprintf (),
@@ -197,7 +191,7 @@ an optional
 .I precision
 and an optional
 .IR "length modifier" .
-
+.PP
 The arguments must correspond properly (after type promotion) with the
 conversion specifier.
 By default, the arguments are used in the order
@@ -214,21 +208,21 @@ where the decimal integer \fIm\fP denotes
 the position in the argument list of the desired argument, indexed starting
 from 1.
 Thus,
+.PP
 .in +4n
-.nf
-
+.EX
 printf("%*d", width, num);
-
-.fi
+.EE
 .in
+.PP
 and
+.PP
 .in +4n
-.nf
-
+.EX
 printf("%2$*1$d", width, num);
-
-.fi
+.EE
 .in
+.PP
 are equivalent.
 The second style allows repeated references to the
 same argument.
@@ -242,23 +236,25 @@ There may be no
 gaps in the numbers of arguments specified using \(aq$\(aq; for example, if
 arguments 1 and 3 are specified, argument 2 must also be specified
 somewhere in the format string.
-
+.PP
 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
 part of the locale.
+(See
+.BR setlocale (3).)
 The POSIX locale
 uses \(aq.\(aq as radix character, and does not have a grouping character.
 Thus,
+.PP
 .in +4n
-.nf
-
+.EX
     printf("%\(aq.2f", 1234567.89);
-
-.fi
+.EE
 .in
+.PP
 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.
 .SS Flag characters
@@ -375,6 +371,8 @@ For decimal conversion
 .BR G )
 the output is to be grouped with thousands' grouping characters
 if the locale information indicates any.
+(See
+.BR setlocale (3).)
 Note that many versions of
 .BR gcc (1)
 cannot parse this option and will issue a warning.
@@ -509,6 +507,12 @@ conversion corresponds to a pointer to a
 .I long long int
 argument.
 .TP
+.B q
+A synonym for
+.BR ll .
+This is a nonstandard extension, derived from BSD;
+avoid its use in new code.
+.TP
 .B L
 A following
 .BR a ,
@@ -524,12 +528,6 @@ conversion corresponds to a
 .I long double
 argument.
 (C99 allows %LF, but SUSv2 does not.)
-.\" .TP
-.\" .B q
-.\" ("quad". 4.4BSD and Linux libc5 only.
-.\" Don't use.)
-This is a synonym for
-.BR ll .
 .TP
 .B j
 A following integer conversion corresponds to an
@@ -552,10 +550,13 @@ argument, or a following
 conversion corresponds to a pointer to a
 .I size_t
 argument.
-.\" (Linux libc5 has
-.\" .B Z
-.\" with this meaning.
-.\" Don't use it.)
+.TP
+.B Z
+A nonstandard synonym for
+.BR z
+that predates the appearance of
+.BR z .
+Do not use in new code.
 .TP
 .B t
 A following integer conversion corresponds to a
@@ -566,7 +567,8 @@ conversion corresponds to a pointer to a
 .I ptrdiff_t
 argument.
 .PP
-SUSv3 specifies all of the above.
+SUSv3 specifies all of the above,
+except for those modifiers explicitly noted as being nonstandard extensions.
 SUSv2 specified only the length modifiers
 .B h
 (in
@@ -595,6 +597,21 @@ and
 .BR Lf ,
 .BR Lg ,
 .BR LG ).
+.PP
+As a nonstandard extension, the GNU implementations treats
+.B ll
+and
+.B L
+as synonyms, so that one can, for example, write
+.BR llg
+(as a synonym for the standards-compliant
+.RB Lg )
+and
+.BR Ld
+(as a synonym for the standards compliant
+.BR lld ).
+Such usage is nonportable.
+.\"
 .SS Conversion specifiers
 A character that specifies the type of conversion to be applied.
 The conversion specifiers and their meanings are:
@@ -665,7 +682,7 @@ 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.
-
+.IP
 (SUSv2 does not know about
 .B F
 and says that character string representations for infinity and NaN
@@ -761,7 +778,7 @@ 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.
-
+.IP
 If an
 .B l
 modifier is present: the
@@ -835,7 +852,7 @@ specification is \(aq%%\(aq.
 .SH RETURN VALUE
 Upon successful return, these functions return the number of characters
 printed (excluding the null byte used to end output to strings).
-
+.PP
 The functions
 .BR snprintf ()
 and
@@ -851,7 +868,7 @@ Thus, a return value of
 .I size
 or more means that the output was truncated.
 (See also below under NOTES.)
-
+.PP
 If an output error is encountered, a negative value is returned.
 .SH ATTRIBUTES
 For an explanation of the terms used in this section, see
@@ -875,7 +892,7 @@ T{
 .BR vsnprintf ()
 T}     Thread safety   MT-Safe locale
 .TE
-
+.sp 1
 .SH CONFORMING TO
 .BR fprintf (),
 .BR printf (),
@@ -884,11 +901,11 @@ T}        Thread safety   MT-Safe locale
 .BR vfprintf (),
 .BR vsprintf ():
 POSIX.1-2001, POSIX.1-2008, C89, C99.
-
+.PP
 .BR snprintf (),
 .BR vsnprintf ():
 POSIX.1-2001, POSIX.1-2008, C99.
-
+.PP
 The
 .BR dprintf ()
 and
@@ -946,9 +963,9 @@ glibc 2.2 adds the conversion character \fBF\fP with C99 semantics,
 and the flag character \fBI\fP.
 .SH NOTES
 Some programs imprudently rely on code such as the following
-
+.PP
     sprintf(buf, "%s some further text", buf);
-
+.PP
 to append text to
 .IR buf .
 However, the standards explicitly note that the results are undefined
@@ -964,7 +981,7 @@ Depending on the version of
 used, and the compiler options employed, calls such as the above will
 .B not
 produce the expected results.
-
+.PP
 The glibc implementation of the functions
 .BR snprintf ()
 and
@@ -1054,13 +1071,13 @@ call to write to memory and creating a security hole.
 To print
 .I Pi
 to five decimal places:
+.PP
 .in +4n
-.nf
-
+.EX
 #include <math.h>
 #include <stdio.h>
 fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
-.fi
+.EE
 .in
 .PP
 To print a date and time in the form "Sunday, July 3, 10:02",
@@ -1069,44 +1086,44 @@ where
 and
 .I month
 are pointers to strings:
+.PP
 .in +4n
-.nf
-
+.EX
 #include <stdio.h>
 fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
         weekday, month, day, hour, min);
-.fi
+.EE
 .in
 .PP
 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:
+.PP
 .in +4n
-.nf
-
+.EX
 #include <stdio.h>
 fprintf(stdout, format,
         weekday, month, day, hour, min);
-
-.fi
+.EE
 .in
+.PP
 where
 .I format
 depends on locale, and may permute the arguments.
 With the value:
+.PP
 .in +4n
-.nf
-
+.EX
 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
-
-.fi
+.EE
 .in
+.PP
 one might obtain "Sonntag, 3. Juli, 10:02".
 .PP
 To allocate a sufficiently large string and print into it
 (code correct for both glibc 2.0 and glibc 2.1):
-.nf
-
+.PP
+.EX
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -1134,15 +1151,16 @@ make_message(const char *fmt, ...)
 
     va_start(ap, fmt);
     size = vsnprintf(p, size, fmt, ap);
+    va_end(ap);
+
     if (size < 0) {
         free(p);
         return NULL;
     }
-    va_end(ap);
 
     return p;
 }
-.fi
+.EE
 .PP
 If truncation occurs in glibc versions prior to 2.0.6, this is treated as an
 error instead of being handled gracefully.