.\" 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>
.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 ", ...);"
.B #include <stdarg.h>
.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 );
+.fi
.PP
.in -4n
Feature Test Macro Requirements for glibc (see
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.
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
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",
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>
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.