aboutsummaryrefslogtreecommitdiffstats
path: root/man3/printf.3
diff options
context:
space:
mode:
Diffstat (limited to 'man3/printf.3')
-rw-r--r--man3/printf.374
1 files changed, 42 insertions, 32 deletions
diff --git a/man3/printf.3 b/man3/printf.3
index 256f3b20e6..bcf152fd8c 100644
--- a/man3/printf.3
+++ b/man3/printf.3
@@ -155,13 +155,17 @@ the position in the argument list of the desired argument, indexed starting
from 1. Thus,
.RS
.nf
- printf("%*d", width, num);
+
+ printf("%*d", width, num);
+
.fi
.RE
and
.RS
.nf
- printf("%2$*1$d", width, num);
+
+ printf("%2$*1$d", width, num);
+
.fi
.RE
are equivalent. The second style allows repeated references to the
@@ -181,7 +185,7 @@ uses `.' as radix character, and does not have a grouping character.
Thus,
.RS
.nf
- printf("%'.2f", 1234567.89);
+ printf("%'.2f", 1234567.89);
.fi
.RE
results in `1234567.89' in the POSIX locale, in `1234567,89' in the
@@ -711,6 +715,7 @@ specification is `%%'.
To print \*(Pi to five decimal places:
.RS
.nf
+
#include <math.h>
#include <stdio.h>
fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
@@ -725,9 +730,10 @@ and
are pointers to strings:
.RS
.nf
+
#include <stdio.h>
fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
- weekday, month, day, hour, min);
+ weekday, month, day, hour, min);
.fi
.RE
.PP
@@ -736,9 +742,11 @@ Hence, an internationalized version must be able to print
the arguments in an order specified by the format:
.RS
.nf
+
#include <stdio.h>
fprintf(stdout, format,
- weekday, month, day, hour, min);
+ weekday, month, day, hour, min);
+
.fi
.RE
where
@@ -755,40 +763,42 @@ To allocate a sufficiently large string and print into it
(code correct for both glibc 2.0 and glibc 2.1):
.RS
.nf
+
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char *
-make_message(const char *fmt, ...) {
- /* Guess we need no more than 100 bytes. */
- int n, size = 100;
- char *p, *np;
- va_list ap;
+make_message(const char *fmt, ...)
+{
+ /* Guess we need no more than 100 bytes. */
+ int n, size = 100;
+ char *p, *np;
+ va_list ap;
- if ((p = malloc(size)) == NULL)
- return NULL;
+ if ((p = malloc(size)) == NULL)
+ return NULL;
- while (1) {
- /* Try to print in the allocated space. */
- va_start(ap, fmt);
- n = vsnprintf(p, size, fmt, ap);
- va_end(ap);
- /* If that worked, return the string. */
- if (n > \-1 && n < size)
- return p;
- /* Else try again with more space. */
- if (n > \-1) /* glibc 2.1 */
- size = n+1; /* precisely what is needed */
- else /* glibc 2.0 */
- size *= 2; /* twice the old size */
- if ((np = realloc (p, size)) == NULL) {
- free(p);
- return NULL;
- } else {
- p = np;
- }
- }
+ while (1) {
+ /* Try to print in the allocated space. */
+ va_start(ap, fmt);
+ n = vsnprintf(p, size, fmt, ap);
+ va_end(ap);
+ /* If that worked, return the string. */
+ if (n > \-1 && n < size)
+ return p;
+ /* Else try again with more space. */
+ if (n > \-1) /* glibc 2.1 */
+ size = n+1; /* precisely what is needed */
+ else /* glibc 2.0 */
+ size *= 2; /* twice the old size */
+ if ((np = realloc (p, size)) == NULL) {
+ free(p);
+ return NULL;
+ } else {
+ p = np;
+ }
+ }
}
.fi
.RE