.\" 2000-07-26 jsm28@hermes.cam.ac.uk - three small fixes
.\" 2000-10-16 jsm28@hermes.cam.ac.uk - more fixes
.\"
-.TH PRINTF 3 2013-09-04 "GNU" "Linux Programmer's Manual"
+.TH PRINTF 3 2013-12-30 "GNU" "Linux Programmer's Manual"
.SH NAME
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
vsnprintf \- formatted output conversion
do not write more than
.I size
bytes (including the terminating null byte (\(aq\e0\(aq)).
-If the output was truncated due to this limit then the return value
+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.
pointer argument is printed in hexadecimal (as if by
.B %#x
or
-.BR %#lx ).
+.BR %#lx ).
.TP
.B n
The number of characters written so far is stored into the integer
.PP
To allocate a sufficiently large string and print into it
(code correct for both glibc 2.0 and glibc 2.1):
-.PP
-If truncation occurs in glibc versions prior to 2.0.6, this is treated as an
-error instead of being handled gracefully.
.nf
#include <stdio.h>
char *p, *np;
va_list ap;
- if ((p = malloc(size)) == NULL)
+ p = malloc(size);
+ if (p == NULL)
return NULL;
while (1) {
/* Check error code */
- if (n < 0)
+ if (n < 0) {
+ free(p);
return NULL;
+ }
/* If that worked, return the string */
size = n + 1; /* Precisely what is needed */
- if ((np = realloc (p, size)) == NULL) {
+ np = realloc(p, size);
+ if (np == NULL) {
free(p);
return NULL;
} else {
}
}
.fi
+.PP
+If truncation occurs in glibc versions prior to 2.0.6, this is treated as an
+error instead of being handled gracefully.
.SH SEE ALSO
.BR printf (1),
.BR asprintf (3),
.BR wcrtomb (3),
.BR wprintf (3),
.BR locale (5)
+.SH COLOPHON
+This page is part of release 3.68 of the Linux
+.I man-pages
+project.
+A description of the project,
+information about reporting bugs,
+and the latest version of this page,
+can be found at
+\%http://www.kernel.org/doc/man\-pages/.