2004-11-03 13:51:07 +00:00
|
|
|
.\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl)
|
|
|
|
.\"
|
|
|
|
.\" This is free documentation; you can redistribute it and/or
|
|
|
|
.\" modify it under the terms of the GNU General Public License as
|
|
|
|
.\" published by the Free Software Foundation; either version 2 of
|
|
|
|
.\" the License, or (at your option) any later version.
|
|
|
|
.\"
|
|
|
|
.\" The GNU General Public License's references to "object code"
|
|
|
|
.\" and "executables" are to be interpreted as the output of any
|
|
|
|
.\" document formatting or typesetting system, including
|
|
|
|
.\" intermediate and printed output.
|
|
|
|
.\"
|
|
|
|
.\" This manual is distributed in the hope that it will be useful,
|
|
|
|
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
.\" GNU General Public License for more details.
|
|
|
|
.\"
|
|
|
|
.\" You should have received a copy of the GNU General Public
|
|
|
|
.\" License along with this manual; if not, write to the Free
|
|
|
|
.\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
|
|
|
|
.\" USA.
|
|
|
|
.\"
|
|
|
|
.\"
|
|
|
|
.\" Earlier versions of this page influenced the present text.
|
|
|
|
.\" It was derived from a Berkeley page with version
|
|
|
|
.\" @(#)printf.3 6.14 (Berkeley) 7/30/91
|
|
|
|
.\" converted for Linux by faith@cs.unc.edu, updated by
|
|
|
|
.\" Helmut.Geyer@iwr.uni-heidelberg.de, agulbra@troll.no and Bruno Haible.
|
|
|
|
.\"
|
|
|
|
.\" 1999-11-25 aeb - Rewritten, using SUSv2 and C99.
|
|
|
|
.\" 2000-07-26 jsm28@hermes.cam.ac.uk - three small fixes
|
|
|
|
.\" 2000-10-16 jsm28@hermes.cam.ac.uk - more fixes
|
|
|
|
.\"
|
_exit.2, brk.2, capget.2, chdir.2, chmod.2, chown.2, chroot.2, getdtablesize.2, gethostname.2, getpagesize.2, getsid.2, killpg.2, mknod.2, mknodat.2, poll.2, posix_fadvise.2, pread.2, readlink.2, setpgid.2, setreuid.2, sigaltstack.2, stat.2, symlink.2, sync.2, truncate.2, vfork.2, wait.2, wait4.2, a64l.3, abs.3, acos.3, acosh.3, asin.3, asinh.3, atan.3, atan2.3, atoi.3, cbrt.3, ceil.3, copysign.3, cosh.3, dirfd.3, div.3, ecvt.3, erf.3, erfc.3, exp.3, exp2.3, fabs.3, fdim.3, ffs.3, floor.3, fma.3, fmax.3, fmin.3, fmod.3, fpclassify.3, frexp.3, ftw.3, fwide.3, gcvt.3, getcwd.3, getdate.3, getgrent.3, gethostid.3, getpass.3, getpwent.3, getsubopt.3, getw.3, hypot.3, ilogb.3, index.3, isalpha.3, isgreater.3, iswblank.3, j0.3, ldexp.3, lockf.3, log.3, log10.3, log1p.3, logb.3, lrint.3, lround.3, mkstemp.3, mktemp.3, modf.3, mq_receive.3, mq_send.3, nan.3, nextafter.3, posix_fallocate.3, posix_memalign.3, printf.3, qecvt.3, random.3, realpath.3, remainder.3, remquo.3, rint.3, round.3, scalb.3, scalbln.3, scanf.3, siginterrupt.3, signbit.3, sigset.3, sinh.3, sqrt.3, strcasecmp.3, strcat.3, strchr.3, strcmp.3, strcoll.3, strcpy.3, strfry.3, strpbrk.3, strsep.3, strspn.3, strstr.3, strtod.3, strtok.3, strtol.3, strtoul.3, strxfrm.3, tanh.3, tgamma.3, trunc.3, ttyslot.3, ualarm.3, usleep.3, wprintf.3, armscii-8.7, cp1251.7, iso_8859-10.7, iso_8859-11.7, iso_8859-13.7, iso_8859-14.7, iso_8859-3.7, iso_8859-5.7, iso_8859-6.7, iso_8859-8.7, koi8-u.7: tstamp
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-09-19 17:19:13 +00:00
|
|
|
.TH PRINTF 3 2010-09-20 "GNU" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
2008-06-09 21:12:25 +00:00
|
|
|
printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf,
|
|
|
|
vsnprintf \- formatted output conversion
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #include <stdio.h>
|
|
|
|
.sp
|
|
|
|
.BI "int printf(const char *" format ", ...);"
|
|
|
|
.br
|
|
|
|
.BI "int fprintf(FILE *" stream ", 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
|
|
|
|
.BI "int vprintf(const char *" format ", va_list " ap );
|
|
|
|
.br
|
|
|
|
.BI "int vfprintf(FILE *" stream ", const char *" format ", va_list " ap );
|
|
|
|
.br
|
|
|
|
.BI "int vsprintf(char *" str ", const char *" format ", va_list " ap );
|
|
|
|
.br
|
2008-07-06 15:10:32 +00:00
|
|
|
.BI "int vsnprintf(char *" str ", size_t " size ", const char *" format \
|
|
|
|
", va_list " ap );
|
2007-07-08 12:11:40 +00:00
|
|
|
.sp
|
|
|
|
.in -4n
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
|
|
.BR feature_test_macros (7)):
|
|
|
|
.in
|
|
|
|
.sp
|
|
|
|
.ad l
|
|
|
|
.BR snprintf (),
|
|
|
|
.BR vsnprintf ():
|
2010-09-18 16:16:36 +00:00
|
|
|
.RS 4
|
2010-09-18 16:17:21 +00:00
|
|
|
_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _ISOC99_SOURCE ||
|
|
|
|
_POSIX_C_SOURCE\ >=\ 200112L;
|
2010-09-18 16:16:36 +00:00
|
|
|
.br
|
|
|
|
or
|
2007-07-08 12:11:40 +00:00
|
|
|
.I "cc -std=c99"
|
2010-09-18 16:16:36 +00:00
|
|
|
.RE
|
|
|
|
.ad
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
|
|
|
The functions in the
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR printf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
family produce output according to a
|
|
|
|
.I format
|
2007-04-12 22:42:49 +00:00
|
|
|
as described below.
|
|
|
|
The functions
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR printf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR vprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
write output to
|
|
|
|
.IR stdout ,
|
|
|
|
the standard output stream;
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR vfprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
write output to the given output
|
|
|
|
.IR stream ;
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR sprintf (),
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR snprintf (),
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR vsprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR vsnprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
write to the character string
|
2008-07-18 08:37:37 +00:00
|
|
|
.IR str .
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
The functions
|
2007-06-07 13:17:35 +00:00
|
|
|
.BR snprintf ()
|
|
|
|
and
|
|
|
|
.BR vsnprintf ()
|
|
|
|
write at most
|
|
|
|
.I size
|
2008-06-09 15:49:35 +00:00
|
|
|
bytes (including the trailing null byte (\(aq\e0\(aq)) to
|
2007-06-07 13:17:35 +00:00
|
|
|
.IR str .
|
|
|
|
.PP
|
|
|
|
The functions
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR vprintf (),
|
|
|
|
.BR vfprintf (),
|
|
|
|
.BR vsprintf (),
|
|
|
|
.BR vsnprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
are equivalent to the functions
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR printf (),
|
|
|
|
.BR fprintf (),
|
|
|
|
.BR sprintf (),
|
|
|
|
.BR snprintf (),
|
2007-11-18 07:21:08 +00:00
|
|
|
respectively, except that they are called with a
|
|
|
|
.I va_list
|
|
|
|
instead of a variable number of arguments.
|
2007-04-12 22:42:49 +00:00
|
|
|
These functions do not call the
|
2004-11-03 13:51:07 +00:00
|
|
|
.I va_end
|
2007-04-12 22:42:49 +00:00
|
|
|
macro.
|
2007-11-18 07:21:08 +00:00
|
|
|
Because they invoke the
|
|
|
|
.I va_arg
|
|
|
|
macro, the value of
|
2004-11-03 13:51:07 +00:00
|
|
|
.I ap
|
2007-04-12 22:42:49 +00:00
|
|
|
is undefined after the call.
|
2007-11-18 07:21:08 +00:00
|
|
|
See
|
2007-12-10 14:48:43 +00:00
|
|
|
.BR stdarg (3).
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
These eight 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.
|
2008-12-19 16:47:04 +00:00
|
|
|
|
|
|
|
C99 and POSIX.1-2001 specify that the results are undefined if a call to
|
|
|
|
.BR sprintf (),
|
|
|
|
.BR snprintf (),
|
|
|
|
.BR vsprintf (),
|
|
|
|
or
|
|
|
|
.BR vsnprintf ()
|
2009-09-15 04:24:17 +00:00
|
|
|
would cause copying to take place between objects that overlap
|
2008-12-19 16:47:04 +00:00
|
|
|
(e.g., if the target string array and one of the supplied input arguments
|
|
|
|
refer to the same buffer).
|
|
|
|
See NOTES.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SS "Return value"
|
|
|
|
Upon successful return, these functions return the number of characters
|
2008-06-09 21:12:25 +00:00
|
|
|
printed (not including the
|
|
|
|
trailing \(aq\e0\(aq used to end output to strings).
|
2007-06-07 13:17:35 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
The functions
|
2005-10-28 13:43:45 +00:00
|
|
|
.BR snprintf ()
|
2007-04-12 22:42:49 +00:00
|
|
|
and
|
2005-10-28 13:43:45 +00:00
|
|
|
.BR vsnprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
do not write more than
|
|
|
|
.I size
|
2008-06-09 15:49:35 +00:00
|
|
|
bytes (including the trailing \(aq\e0\(aq).
|
2004-11-03 13:51:07 +00:00
|
|
|
If the output was truncated due to this limit then the return value
|
2008-06-09 15:49:35 +00:00
|
|
|
is the number of characters (not including the trailing \(aq\e0\(aq)
|
2004-11-03 13:51:07 +00:00
|
|
|
which would have been written to the final string if enough space
|
2007-04-12 22:42:49 +00:00
|
|
|
had been available.
|
|
|
|
Thus, a return value of
|
2004-11-03 13:51:07 +00:00
|
|
|
.I size
|
2007-04-12 22:42:49 +00:00
|
|
|
or more means that the output was truncated.
|
|
|
|
(See also below under NOTES.)
|
2007-06-07 13:17:35 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
If an output error is encountered, a negative value is returned.
|
|
|
|
.SS "Format of the format string"
|
|
|
|
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
|
|
|
|
.BR % ),
|
|
|
|
which are copied unchanged to the output stream;
|
|
|
|
and conversion specifications, each of which results in fetching zero or
|
2007-04-12 22:42:49 +00:00
|
|
|
more subsequent arguments.
|
|
|
|
Each conversion specification is introduced by
|
2004-11-03 13:51:07 +00:00
|
|
|
the character
|
|
|
|
.BR % ,
|
|
|
|
and ends with a
|
|
|
|
.IR "conversion specifier" .
|
|
|
|
In between there may be (in this order) zero or more
|
|
|
|
.IR flags ,
|
|
|
|
an optional minimum
|
|
|
|
.IR "field width" ,
|
|
|
|
an optional
|
|
|
|
.I precision
|
|
|
|
and an optional
|
|
|
|
.IR "length modifier" .
|
|
|
|
|
|
|
|
The arguments must correspond properly (after type promotion) with the
|
2007-04-12 22:42:49 +00:00
|
|
|
conversion specifier.
|
|
|
|
By default, the arguments are used in the order
|
2008-06-09 21:03:52 +00:00
|
|
|
given, where each \(aq*\(aq and each conversion specifier asks for the next
|
2004-11-03 13:51:07 +00:00
|
|
|
argument (and it is an error if insufficiently many arguments are given).
|
|
|
|
One can also specify explicitly which argument is taken,
|
2008-06-09 21:03:52 +00:00
|
|
|
at each place where an argument is required, by writing "%m$" instead
|
2008-06-09 21:12:25 +00:00
|
|
|
of \(aq%\(aq and "*m$" instead of \(aq*\(aq,
|
|
|
|
where the decimal integer m denotes
|
2004-11-03 13:51:07 +00:00
|
|
|
the position in the argument list of the desired argument, indexed starting
|
2007-04-12 22:42:49 +00:00
|
|
|
from 1.
|
|
|
|
Thus,
|
2007-12-19 06:16:04 +00:00
|
|
|
.in +4n
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
2007-04-05 13:29:41 +00:00
|
|
|
|
2007-12-19 06:16:04 +00:00
|
|
|
printf("%*d", width, num);
|
2007-04-05 13:29:41 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2007-12-19 06:16:04 +00:00
|
|
|
.in
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2007-12-19 06:16:04 +00:00
|
|
|
.in +4n
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
2007-04-05 13:29:41 +00:00
|
|
|
|
2007-12-19 06:16:04 +00:00
|
|
|
printf("%2$*1$d", width, num);
|
2007-04-05 13:29:41 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2007-12-19 06:16:04 +00:00
|
|
|
.in
|
2007-04-12 22:42:49 +00:00
|
|
|
are equivalent.
|
|
|
|
The second style allows repeated references to the
|
|
|
|
same argument.
|
2008-06-09 21:03:52 +00:00
|
|
|
The C99 standard does not include the style using \(aq$\(aq,
|
2007-04-12 22:42:49 +00:00
|
|
|
which comes from the Single Unix Specification.
|
|
|
|
If the style using
|
2008-06-09 21:03:52 +00:00
|
|
|
\(aq$\(aq is used, it must be used throughout for all conversions taking an
|
2004-11-03 13:51:07 +00:00
|
|
|
argument and all width and precision arguments, but it may be mixed
|
2008-06-09 21:03:52 +00:00
|
|
|
with "%%" formats which do not consume an argument.
|
2007-04-12 22:42:49 +00:00
|
|
|
There may be no
|
2008-06-09 21:03:52 +00:00
|
|
|
gaps in the numbers of arguments specified using \(aq$\(aq; for example, if
|
2004-11-03 13:51:07 +00:00
|
|
|
arguments 1 and 3 are specified, argument 2 must also be specified
|
|
|
|
somewhere in the format string.
|
|
|
|
|
2008-06-09 21:03:52 +00:00
|
|
|
For some numeric conversions a radix character ("decimal point") or
|
2007-04-12 22:42:49 +00:00
|
|
|
thousands' grouping character is used.
|
|
|
|
The actual character used
|
2007-06-22 19:42:52 +00:00
|
|
|
depends on the
|
|
|
|
.B LC_NUMERIC
|
|
|
|
part of the locale.
|
2007-04-12 22:42:49 +00:00
|
|
|
The POSIX locale
|
2008-06-09 21:03:52 +00:00
|
|
|
uses \(aq.\(aq as radix character, and does not have a grouping character.
|
2004-11-03 13:51:07 +00:00
|
|
|
Thus,
|
2007-12-19 07:19:23 +00:00
|
|
|
.in +4n
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
2007-12-23 09:17:10 +00:00
|
|
|
|
2008-06-09 21:03:52 +00:00
|
|
|
printf("%\(aq.2f", 1234567.89);
|
2007-12-23 09:17:10 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2007-12-19 07:19:23 +00:00
|
|
|
.in
|
2008-06-09 21:03:52 +00:00
|
|
|
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.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SS "The flag characters"
|
|
|
|
The character % is followed by zero or more of the following flags:
|
|
|
|
.TP
|
|
|
|
.B #
|
2007-12-16 14:08:46 +00:00
|
|
|
The value should be converted to an "alternate form".
|
2004-11-03 13:51:07 +00:00
|
|
|
For
|
2007-09-20 16:26:31 +00:00
|
|
|
.B o
|
2004-11-03 13:51:07 +00:00
|
|
|
conversions, the first character of the output string is made zero
|
|
|
|
(by prefixing a 0 if it was not zero already).
|
|
|
|
For
|
|
|
|
.B x
|
|
|
|
and
|
|
|
|
.B X
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
conversions, a nonzero result has the string "0x" (or "0X" for
|
2004-11-03 13:51:07 +00:00
|
|
|
.B X
|
2007-04-12 22:42:49 +00:00
|
|
|
conversions) prepended to it.
|
|
|
|
For
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR a ,
|
|
|
|
.BR A ,
|
|
|
|
.BR e ,
|
|
|
|
.BR E ,
|
|
|
|
.BR f ,
|
|
|
|
.BR F ,
|
|
|
|
.BR g ,
|
|
|
|
and
|
|
|
|
.B G
|
|
|
|
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
|
2007-04-12 22:42:49 +00:00
|
|
|
conversions only if a digit follows).
|
|
|
|
For
|
2004-11-03 13:51:07 +00:00
|
|
|
.B g
|
|
|
|
and
|
|
|
|
.B G
|
|
|
|
conversions, trailing zeros are not removed from the result as they would
|
|
|
|
otherwise be.
|
|
|
|
For other conversions, the result is undefined.
|
|
|
|
.TP
|
|
|
|
.B \&0
|
2007-04-12 22:42:49 +00:00
|
|
|
The value should be zero padded.
|
|
|
|
For
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR d ,
|
|
|
|
.BR i ,
|
|
|
|
.BR o ,
|
|
|
|
.BR u ,
|
|
|
|
.BR x ,
|
|
|
|
.BR X ,
|
|
|
|
.BR a ,
|
|
|
|
.BR A ,
|
|
|
|
.BR e ,
|
|
|
|
.BR E ,
|
|
|
|
.BR f ,
|
|
|
|
.BR F ,
|
|
|
|
.BR g ,
|
|
|
|
and
|
|
|
|
.B G
|
|
|
|
conversions, the converted value is padded on the left with zeros rather
|
|
|
|
than blanks.
|
|
|
|
If the
|
|
|
|
.B \&0
|
|
|
|
and
|
|
|
|
.B \-
|
|
|
|
flags both appear, the
|
|
|
|
.B \&0
|
|
|
|
flag is ignored.
|
|
|
|
If a precision is given with a numeric conversion
|
2007-10-03 06:40:03 +00:00
|
|
|
.RB ( d ,
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR i ,
|
|
|
|
.BR o ,
|
|
|
|
.BR u ,
|
|
|
|
.BR x ,
|
|
|
|
and
|
|
|
|
.BR X ),
|
|
|
|
the
|
|
|
|
.B \&0
|
|
|
|
flag is ignored.
|
|
|
|
For other conversions, the behavior is undefined.
|
|
|
|
.TP
|
|
|
|
.B \-
|
|
|
|
The converted value is to be left adjusted on the field boundary.
|
2007-04-12 22:42:49 +00:00
|
|
|
(The default is right justification.)
|
|
|
|
Except for
|
2004-11-03 13:51:07 +00:00
|
|
|
.B n
|
|
|
|
conversions, the converted value is padded on the right with blanks, rather
|
2007-04-12 22:42:49 +00:00
|
|
|
than on the left with blanks or zeros.
|
|
|
|
A
|
2004-11-03 13:51:07 +00:00
|
|
|
.B \-
|
|
|
|
overrides a
|
|
|
|
.B \&0
|
|
|
|
if both are given.
|
|
|
|
.TP
|
2008-06-09 21:03:52 +00:00
|
|
|
.B \(aq \(aq
|
2004-11-03 13:51:07 +00:00
|
|
|
(a space) A blank should be left before a positive number
|
|
|
|
(or empty string) produced by a signed conversion.
|
|
|
|
.TP
|
|
|
|
.B +
|
2005-07-14 13:49:21 +00:00
|
|
|
A sign (+ or \-) should always be placed before a number produced by a signed
|
2007-04-12 22:42:49 +00:00
|
|
|
conversion.
|
|
|
|
By default a sign is used only for negative numbers.
|
|
|
|
A
|
2004-11-03 13:51:07 +00:00
|
|
|
.B +
|
|
|
|
overrides a space if both are used.
|
|
|
|
.PP
|
|
|
|
The five flag characters above are defined in the C standard.
|
|
|
|
The SUSv2 specifies one further flag character.
|
2007-04-12 22:42:49 +00:00
|
|
|
.TP
|
2008-06-09 21:03:52 +00:00
|
|
|
.B \(aq
|
2004-11-03 13:51:07 +00:00
|
|
|
For decimal conversion
|
2007-10-03 06:40:03 +00:00
|
|
|
.RB ( i ,
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR d ,
|
|
|
|
.BR u ,
|
|
|
|
.BR f ,
|
|
|
|
.BR F ,
|
|
|
|
.BR g ,
|
|
|
|
.BR G )
|
|
|
|
the output is to be grouped with thousands' grouping characters
|
2007-04-12 22:42:49 +00:00
|
|
|
if the locale information indicates any.
|
|
|
|
Note that many versions of
|
2005-11-02 13:55:25 +00:00
|
|
|
.BR gcc (1)
|
2007-04-12 22:42:49 +00:00
|
|
|
cannot parse this option and will issue a warning.
|
|
|
|
SUSv2 does not
|
2008-06-09 21:03:52 +00:00
|
|
|
include \fI%\(aqF\fP.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
glibc 2.2 adds one further flag character.
|
|
|
|
.TP
|
|
|
|
.B I
|
|
|
|
For decimal integer conversion
|
2007-10-03 06:40:03 +00:00
|
|
|
.RB ( i ,
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR d ,
|
|
|
|
.BR u )
|
|
|
|
the output uses the locale's alternative output digits, if any.
|
|
|
|
For example, since glibc 2.2.3 this will give Arabic-Indic digits
|
2008-06-09 21:03:52 +00:00
|
|
|
in the Persian ("fa_IR") locale.
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" outdigits keyword in locale file
|
|
|
|
.SS "The field width"
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
An optional decimal digit string (with nonzero first digit) specifying
|
2007-04-12 22:42:49 +00:00
|
|
|
a minimum field width.
|
|
|
|
If the converted value has fewer characters
|
2004-11-03 13:51:07 +00:00
|
|
|
than the field width, it will be padded with spaces on the left
|
|
|
|
(or right, if the left-adjustment flag has been given).
|
2008-06-09 21:03:52 +00:00
|
|
|
Instead of a decimal digit string one may write "*" or "*m$"
|
|
|
|
(for some decimal integer \fIm\fP) to specify that the field width
|
|
|
|
is given in the next argument, or in the \fIm\fP-th argument, respectively,
|
2004-11-03 13:51:07 +00:00
|
|
|
which must be of type
|
|
|
|
.IR int .
|
2008-06-09 21:03:52 +00:00
|
|
|
A negative field width is taken as a \(aq\-\(aq flag followed by a
|
2004-11-03 13:51:07 +00:00
|
|
|
positive field width.
|
2007-12-25 22:02:19 +00:00
|
|
|
In no case does a nonexistent or small field width cause truncation of a
|
2004-11-03 13:51:07 +00:00
|
|
|
field; if the result of a conversion is wider than the field width, the
|
|
|
|
field is expanded to contain the conversion result.
|
|
|
|
.SS "The precision"
|
2008-06-09 21:03:52 +00:00
|
|
|
An optional precision, in the form of a period (\(aq.\(aq) followed by an
|
2004-11-03 13:51:07 +00:00
|
|
|
optional decimal digit string.
|
2008-06-09 21:03:52 +00:00
|
|
|
Instead of a decimal digit string one may write "*" or "*m$"
|
2004-11-03 13:51:07 +00:00
|
|
|
(for some decimal integer m) to specify that the precision
|
|
|
|
is given in the next argument, or in the m-th argument, respectively,
|
|
|
|
which must be of type
|
|
|
|
.IR int .
|
2008-06-09 21:03:52 +00:00
|
|
|
If the precision is given as just \(aq.\(aq, or the precision is negative,
|
2004-11-03 13:51:07 +00:00
|
|
|
the precision is taken to be zero.
|
|
|
|
This gives the minimum number of digits to appear for
|
|
|
|
.BR d ,
|
|
|
|
.BR i ,
|
|
|
|
.BR o ,
|
|
|
|
.BR u ,
|
|
|
|
.BR x ,
|
|
|
|
and
|
|
|
|
.B X
|
|
|
|
conversions, the number of digits to appear after the radix character for
|
|
|
|
.BR a ,
|
|
|
|
.BR A ,
|
|
|
|
.BR e ,
|
|
|
|
.BR E ,
|
|
|
|
.BR f ,
|
|
|
|
and
|
|
|
|
.B F
|
|
|
|
conversions, the maximum number of significant digits for
|
|
|
|
.B g
|
|
|
|
and
|
|
|
|
.B G
|
|
|
|
conversions, or the maximum number of characters to be printed from a
|
|
|
|
string for
|
|
|
|
.B s
|
|
|
|
and
|
|
|
|
.B S
|
|
|
|
conversions.
|
|
|
|
.SS "The length modifier"
|
2008-06-09 21:03:52 +00:00
|
|
|
Here, "integer conversion" stands for
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR d ,
|
|
|
|
.BR i ,
|
|
|
|
.BR o ,
|
|
|
|
.BR u ,
|
|
|
|
.BR x ,
|
|
|
|
or
|
2007-09-20 16:26:31 +00:00
|
|
|
.B X
|
2004-11-03 13:51:07 +00:00
|
|
|
conversion.
|
|
|
|
.TP
|
|
|
|
.B hh
|
|
|
|
A following integer conversion corresponds to a
|
|
|
|
.I signed char
|
|
|
|
or
|
|
|
|
.I unsigned char
|
|
|
|
argument, or a following
|
|
|
|
.B n
|
|
|
|
conversion corresponds to a pointer to a
|
|
|
|
.I signed char
|
|
|
|
argument.
|
|
|
|
.TP
|
|
|
|
.B h
|
|
|
|
A following integer conversion corresponds to a
|
|
|
|
.I short int
|
|
|
|
or
|
|
|
|
.I unsigned short int
|
|
|
|
argument, or a following
|
|
|
|
.B n
|
|
|
|
conversion corresponds to a pointer to a
|
|
|
|
.I short int
|
|
|
|
argument.
|
|
|
|
.TP
|
|
|
|
.B l
|
|
|
|
(ell) A following integer conversion corresponds to a
|
|
|
|
.I long int
|
|
|
|
or
|
|
|
|
.I unsigned long int
|
|
|
|
argument, or a following
|
|
|
|
.B n
|
|
|
|
conversion corresponds to a pointer to a
|
|
|
|
.I long int
|
|
|
|
argument, or a following
|
|
|
|
.B c
|
|
|
|
conversion corresponds to a
|
|
|
|
.I wint_t
|
|
|
|
argument, or a following
|
|
|
|
.B s
|
|
|
|
conversion corresponds to a pointer to
|
|
|
|
.I wchar_t
|
|
|
|
argument.
|
|
|
|
.TP
|
|
|
|
.B ll
|
|
|
|
(ell-ell).
|
|
|
|
A following integer conversion corresponds to a
|
|
|
|
.I long long int
|
|
|
|
or
|
|
|
|
.I unsigned long long int
|
|
|
|
argument, or a following
|
|
|
|
.B n
|
|
|
|
conversion corresponds to a pointer to a
|
|
|
|
.I long long int
|
|
|
|
argument.
|
|
|
|
.TP
|
2007-09-20 16:26:31 +00:00
|
|
|
.B L
|
2004-11-03 13:51:07 +00:00
|
|
|
A following
|
|
|
|
.BR a ,
|
|
|
|
.BR A ,
|
|
|
|
.BR e ,
|
|
|
|
.BR E ,
|
|
|
|
.BR f ,
|
|
|
|
.BR F ,
|
|
|
|
.BR g ,
|
|
|
|
or
|
|
|
|
.B G
|
|
|
|
conversion corresponds to a
|
|
|
|
.I long double
|
|
|
|
argument.
|
|
|
|
(C99 allows %LF, but SUSv2 does not.)
|
|
|
|
.TP
|
|
|
|
.B q
|
2008-06-09 21:03:52 +00:00
|
|
|
("quad". 4.4BSD and Linux libc5 only.
|
2007-04-12 22:42:49 +00:00
|
|
|
Don't use.)
|
2004-11-03 13:51:07 +00:00
|
|
|
This is a synonym for
|
|
|
|
.BR ll .
|
|
|
|
.TP
|
|
|
|
.B j
|
|
|
|
A following integer conversion corresponds to an
|
|
|
|
.I intmax_t
|
|
|
|
or
|
|
|
|
.I uintmax_t
|
|
|
|
argument.
|
|
|
|
.TP
|
|
|
|
.B z
|
|
|
|
A following integer conversion corresponds to a
|
|
|
|
.I size_t
|
|
|
|
or
|
|
|
|
.I ssize_t
|
2007-04-12 22:42:49 +00:00
|
|
|
argument.
|
|
|
|
(Linux libc5 has
|
2004-11-03 13:51:07 +00:00
|
|
|
.B Z
|
2007-04-12 22:42:49 +00:00
|
|
|
with this meaning.
|
|
|
|
Don't use it.)
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B t
|
|
|
|
A following integer conversion corresponds to a
|
|
|
|
.I ptrdiff_t
|
|
|
|
argument.
|
|
|
|
.PP
|
|
|
|
The SUSv2 only knows about the length modifiers
|
|
|
|
.B h
|
|
|
|
(in
|
|
|
|
.BR hd ,
|
|
|
|
.BR hi ,
|
|
|
|
.BR ho ,
|
|
|
|
.BR hx ,
|
|
|
|
.BR hX ,
|
|
|
|
.BR hn )
|
|
|
|
and
|
|
|
|
.B l
|
|
|
|
(in
|
|
|
|
.BR ld ,
|
|
|
|
.BR li ,
|
|
|
|
.BR lo ,
|
|
|
|
.BR lx ,
|
|
|
|
.BR lX ,
|
|
|
|
.BR ln ,
|
|
|
|
.BR lc ,
|
|
|
|
.BR ls )
|
|
|
|
and
|
|
|
|
.B L
|
|
|
|
(in
|
|
|
|
.BR Le ,
|
|
|
|
.BR LE ,
|
|
|
|
.BR Lf ,
|
|
|
|
.BR Lg ,
|
|
|
|
.BR LG ).
|
|
|
|
.SS "The conversion specifier"
|
|
|
|
A character that specifies the type of conversion to be applied.
|
|
|
|
The conversion specifiers and their meanings are:
|
|
|
|
.TP
|
2008-07-12 20:49:46 +00:00
|
|
|
.BR d ", " i
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
|
|
|
.I int
|
|
|
|
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
|
2007-04-12 22:42:49 +00:00
|
|
|
padded on the left with zeros.
|
|
|
|
The default precision is 1.
|
2004-11-03 13:51:07 +00:00
|
|
|
When 0 is printed with an explicit precision 0, the output is empty.
|
|
|
|
.TP
|
2008-07-12 20:49:46 +00:00
|
|
|
.BR o ", " u ", " x ", " X
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
|
|
|
.I "unsigned int"
|
|
|
|
argument is converted to unsigned octal
|
2007-10-03 06:40:03 +00:00
|
|
|
.RB ( o ),
|
2004-11-03 13:51:07 +00:00
|
|
|
unsigned decimal
|
2007-10-03 06:40:03 +00:00
|
|
|
.RB ( u ),
|
2004-11-03 13:51:07 +00:00
|
|
|
or unsigned hexadecimal
|
2007-10-03 06:40:03 +00:00
|
|
|
.RB ( x
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
|
|
|
.BR X )
|
2007-04-12 22:42:49 +00:00
|
|
|
notation.
|
|
|
|
The letters
|
2004-11-03 13:51:07 +00:00
|
|
|
.B abcdef
|
|
|
|
are used for
|
|
|
|
.B x
|
|
|
|
conversions; the letters
|
|
|
|
.B ABCDEF
|
|
|
|
are used for
|
|
|
|
.B X
|
2007-04-12 22:42:49 +00:00
|
|
|
conversions.
|
|
|
|
The precision, if any, gives the minimum number of digits
|
2004-11-03 13:51:07 +00:00
|
|
|
that must appear; if the converted value requires fewer digits, it is
|
2007-04-12 22:42:49 +00:00
|
|
|
padded on the left with zeros.
|
|
|
|
The default precision is 1.
|
2004-11-03 13:51:07 +00:00
|
|
|
When 0 is printed with an explicit precision 0, the output is empty.
|
|
|
|
.TP
|
2008-07-12 20:49:46 +00:00
|
|
|
.BR e ", " E
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
|
|
|
.I double
|
|
|
|
argument is rounded and converted in the style
|
|
|
|
.if \w'\*(Pm'=0 .ds Pm \(+-
|
2007-10-03 06:40:03 +00:00
|
|
|
.RB [\-]d \&. ddd e \\*(Pmdd
|
2007-04-27 17:04:30 +00:00
|
|
|
where there is one digit before the decimal-point character and the number
|
2004-11-03 13:51:07 +00:00
|
|
|
of digits after it is equal to the precision; if the precision is missing,
|
2007-04-27 17:04:30 +00:00
|
|
|
it is taken as 6; if the precision is zero, no decimal-point character
|
2007-04-12 22:42:49 +00:00
|
|
|
appears.
|
|
|
|
An
|
2004-11-03 13:51:07 +00:00
|
|
|
.B E
|
|
|
|
conversion uses the letter
|
|
|
|
.B E
|
|
|
|
(rather than
|
|
|
|
.BR e )
|
2007-04-12 22:42:49 +00:00
|
|
|
to introduce the exponent.
|
|
|
|
The exponent always contains at least two
|
2004-11-03 13:51:07 +00:00
|
|
|
digits; if the value is zero, the exponent is 00.
|
|
|
|
.TP
|
2008-07-12 20:49:46 +00:00
|
|
|
.BR f ", " F
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
|
|
|
.I double
|
|
|
|
argument is rounded and converted to decimal notation in the style
|
2007-10-03 06:40:03 +00:00
|
|
|
.RB [\-]ddd \&. ddd,
|
2007-04-27 17:04:30 +00:00
|
|
|
where the number of digits after the decimal-point character is equal to
|
2007-04-12 22:42:49 +00:00
|
|
|
the precision specification.
|
|
|
|
If the precision is missing, it is taken as
|
2007-04-27 17:04:30 +00:00
|
|
|
6; if the precision is explicitly zero, no decimal-point character appears.
|
2004-11-03 13:51:07 +00:00
|
|
|
If a decimal point appears, at least one digit appears before it.
|
|
|
|
|
|
|
|
(The SUSv2 does not know about
|
|
|
|
.B F
|
|
|
|
and says that character string representations for infinity and NaN
|
2007-04-12 22:42:49 +00:00
|
|
|
may be made available.
|
2008-06-09 21:03:52 +00:00
|
|
|
The C99 standard specifies "[\-]inf" or "[\-]infinity"
|
|
|
|
for infinity, and a string starting with "nan" for NaN, in the case of
|
2004-11-03 13:51:07 +00:00
|
|
|
.B f
|
2008-06-09 21:03:52 +00:00
|
|
|
conversion, and "[\-]INF" or "[\-]INFINITY" or "NAN*" in the case of
|
2004-11-03 13:51:07 +00:00
|
|
|
.B F
|
|
|
|
conversion.)
|
|
|
|
.TP
|
2008-07-12 20:49:46 +00:00
|
|
|
.BR g ", " G
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
|
|
|
.I double
|
|
|
|
argument is converted in style
|
|
|
|
.B f
|
|
|
|
or
|
|
|
|
.B e
|
|
|
|
(or
|
|
|
|
.B F
|
|
|
|
or
|
|
|
|
.B E
|
|
|
|
for
|
|
|
|
.B G
|
2007-04-12 22:42:49 +00:00
|
|
|
conversions).
|
|
|
|
The precision specifies the number of significant digits.
|
2004-11-03 13:51:07 +00:00
|
|
|
If the precision is missing, 6 digits are given; if the precision is zero,
|
2007-04-12 22:42:49 +00:00
|
|
|
it is treated as 1.
|
|
|
|
Style
|
2004-11-03 13:51:07 +00:00
|
|
|
.B e
|
|
|
|
is used if the exponent from its conversion is less than \-4 or greater
|
2007-04-12 22:42:49 +00:00
|
|
|
than or equal to the precision.
|
|
|
|
Trailing zeros are removed from the
|
2004-11-03 13:51:07 +00:00
|
|
|
fractional part of the result; a decimal point appears only if it is
|
|
|
|
followed by at least one digit.
|
|
|
|
.TP
|
2008-07-12 20:49:46 +00:00
|
|
|
.BR a ", " A
|
2004-11-03 13:51:07 +00:00
|
|
|
(C99; not in SUSv2) For
|
|
|
|
.B a
|
|
|
|
conversion, the
|
|
|
|
.I double
|
|
|
|
argument is converted to hexadecimal notation (using the letters abcdef)
|
|
|
|
in the style
|
2007-10-03 06:40:03 +00:00
|
|
|
.RB [\-] 0x h \&. hhhh p \\*(Pmd;
|
2004-11-03 13:51:07 +00:00
|
|
|
for
|
|
|
|
.B A
|
|
|
|
conversion the prefix
|
|
|
|
.BR 0X ,
|
|
|
|
the letters ABCDEF, and the exponent separator
|
|
|
|
.B P
|
|
|
|
is used.
|
|
|
|
There is one hexadecimal digit before the decimal 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
|
|
|
|
.IR double .
|
accept.2, access.2, acct.2, clock_nanosleep.2, mbind.2, mincore.2, remap_file_pages.2, sched_setscheduler.2, set_mempolicy.2, splice.2, stat.2, syslog.2, timer_create.2, timerfd_create.2, truncate.2, fenv.3, ferror.3, fflush.3, fgetwc.3, fgetws.3, flockfile.3, fputwc.3, fputws.3, fread.3, getopt.3, gets.3, getwchar.3, glob.3, iconv.3, longjmp.3, pow.3, printf.3, puts.3, putwchar.3, regex.3, rpc.3, scanf.3, setjmp.3, termios.3, unlocked_stdio.3, wcswidth.3, hd.4, rtc.4, st.4, core.5, dir_colors.5, elf.5, proc.5, arp.7, ascii.7, boot.7, bootparam.7, charsets.7, futex.7, ip.7, iso_8859-11.7, man-pages.7, man.7, mdoc.samples.7, path_resolution.7, pipe.7, posixoptions.7, unicode.7, unix.7, uri.7, utf-8.7, ld.so.8: s/non-/non/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 17:20:12 +00:00
|
|
|
The digit before the decimal point is unspecified for nonnormalized
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
numbers, and nonzero but otherwise unspecified for normalized numbers.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B c
|
|
|
|
If no
|
|
|
|
.B l
|
|
|
|
modifier is present, the
|
|
|
|
.I int
|
|
|
|
argument is converted to an
|
|
|
|
.IR "unsigned char" ,
|
|
|
|
and the resulting character is written.
|
|
|
|
If an
|
|
|
|
.B l
|
|
|
|
modifier is present, the
|
|
|
|
.I wint_t
|
|
|
|
(wide character) argument is converted to a multibyte sequence by a call
|
|
|
|
to the
|
2007-05-12 00:30:29 +00:00
|
|
|
.BR wcrtomb (3)
|
2004-11-03 13:51:07 +00:00
|
|
|
function, with a conversion state starting in the initial state, and the
|
|
|
|
resulting multibyte string is written.
|
|
|
|
.TP
|
|
|
|
.B s
|
|
|
|
If no
|
|
|
|
.B l
|
|
|
|
modifier is present: The
|
|
|
|
.I "const char *"
|
|
|
|
argument is expected to be a pointer to an array of character type (pointer
|
2007-04-12 22:42:49 +00:00
|
|
|
to a string).
|
|
|
|
Characters from the array are written up to (but not
|
2008-06-09 21:03:52 +00:00
|
|
|
including) a terminating null byte (\(aq\\0\(aq);
|
2006-01-13 02:09:44 +00:00
|
|
|
if a precision is specified, no more than the number specified
|
2007-04-12 22:42:49 +00:00
|
|
|
are written.
|
|
|
|
If a precision is given, no null byte need be present;
|
2004-11-03 13:51:07 +00:00
|
|
|
if the precision is not specified, or is greater than the size of the
|
2006-01-13 02:09:44 +00:00
|
|
|
array, the array must contain a terminating null byte.
|
2004-11-03 13:51:07 +00:00
|
|
|
|
|
|
|
If an
|
|
|
|
.B l
|
|
|
|
modifier is present: The
|
|
|
|
.I "const wchar_t *"
|
|
|
|
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
|
2007-05-12 00:30:29 +00:00
|
|
|
.BR wcrtomb (3)
|
2004-11-03 13:51:07 +00:00
|
|
|
function, with a conversion state starting in the initial state before
|
|
|
|
the first wide character), up to and including a terminating null
|
2007-04-12 22:42:49 +00:00
|
|
|
wide character.
|
|
|
|
The resulting multibyte characters are written up to
|
|
|
|
(but not including) the terminating null byte.
|
|
|
|
If a precision is
|
2004-11-03 13:51:07 +00:00
|
|
|
specified, no more bytes than the number specified are written, but
|
2007-04-12 22:42:49 +00:00
|
|
|
no partial multibyte characters are written.
|
|
|
|
Note that the precision
|
2004-11-03 13:51:07 +00:00
|
|
|
determines the number of
|
|
|
|
.I bytes
|
|
|
|
written, not the number of
|
|
|
|
.I wide characters
|
|
|
|
or
|
|
|
|
.IR "screen positions" .
|
|
|
|
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.
|
|
|
|
.TP
|
|
|
|
.B C
|
|
|
|
(Not in C99, but in SUSv2.)
|
|
|
|
Synonym for
|
|
|
|
.BR lc .
|
|
|
|
Don't use.
|
|
|
|
.TP
|
|
|
|
.B S
|
|
|
|
(Not in C99, but in SUSv2.)
|
|
|
|
Synonym for
|
|
|
|
.BR ls .
|
|
|
|
Don't use.
|
|
|
|
.TP
|
|
|
|
.B p
|
|
|
|
The
|
|
|
|
.I "void *"
|
|
|
|
pointer argument is printed in hexadecimal (as if by
|
|
|
|
.B %#x
|
|
|
|
or
|
|
|
|
.BR %#lx ).
|
|
|
|
.TP
|
|
|
|
.B n
|
|
|
|
The number of characters written so far is stored into the integer
|
|
|
|
indicated by the
|
|
|
|
.I "int *"
|
2007-04-12 22:42:49 +00:00
|
|
|
(or variant) pointer argument.
|
|
|
|
No argument is converted.
|
|
|
|
.TP
|
2006-06-07 22:45:42 +00:00
|
|
|
.B m
|
2007-04-12 22:42:49 +00:00
|
|
|
(Glibc extension.)
|
|
|
|
Print output of
|
2006-06-07 22:45:42 +00:00
|
|
|
.IR strerror(errno) .
|
|
|
|
No argument is required.
|
2004-11-03 13:51:07 +00:00
|
|
|
.TP
|
|
|
|
.B %
|
2008-06-09 21:03:52 +00:00
|
|
|
A \(aq%\(aq is written.
|
2007-04-12 22:42:49 +00:00
|
|
|
No argument is converted.
|
|
|
|
The complete conversion
|
2008-06-09 21:03:52 +00:00
|
|
|
specification is \(aq%%\(aq.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "CONFORMING TO"
|
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR fprintf (),
|
|
|
|
.BR printf (),
|
|
|
|
.BR sprintf (),
|
|
|
|
.BR vprintf (),
|
|
|
|
.BR vfprintf (),
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR vsprintf ()
|
2006-08-03 13:57:30 +00:00
|
|
|
functions conform to C89 and C99.
|
2004-11-03 13:51:07 +00:00
|
|
|
The
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR snprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR vsnprintf ()
|
2006-08-03 13:57:30 +00:00
|
|
|
functions conform to C99.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
Concerning the return value of
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR snprintf (),
|
2006-08-03 13:57:30 +00:00
|
|
|
SUSv2 and C99 contradict each other: when
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR snprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
is called with
|
|
|
|
.IR size =0
|
|
|
|
then SUSv2 stipulates an unspecified return value less than 1,
|
|
|
|
while C99 allows
|
|
|
|
.I str
|
|
|
|
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.
|
|
|
|
.PP
|
|
|
|
Linux libc4 knows about the five C standard flags.
|
2008-07-12 20:49:46 +00:00
|
|
|
It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
|
|
|
|
and the conversions
|
|
|
|
\fBc\fP, \fBd\fP, \fBe\fP, \fBE\fP, \fBf\fP, \fBF\fP,
|
|
|
|
\fBg\fP, \fBG\fP, \fBi\fP, \fBn\fP, \fBo\fP, \fBp\fP,
|
|
|
|
\fBs\fP, \fBu\fP, \fBx\fP, and \fBX\fP,
|
|
|
|
where \fBF\fP is a synonym for \fBf\fP.
|
|
|
|
Additionally, it accepts \fBD\fP, \fBO\fP, and \fBU\fP as synonyms
|
|
|
|
for \fBld\fP, \fBlo\fP, and \fBlu\fP.
|
2004-11-03 13:51:07 +00:00
|
|
|
(This is bad, and caused serious bugs later, when
|
2008-07-12 20:49:46 +00:00
|
|
|
support for \fB%D\fP disappeared.)
|
2007-04-12 22:42:49 +00:00
|
|
|
No locale-dependent radix character,
|
2008-07-12 20:49:46 +00:00
|
|
|
no thousands' separator, no NaN or infinity, no "%m$" and "*m$".
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2008-06-09 21:03:52 +00:00
|
|
|
Linux libc5 knows about the five C standard flags and the \(aq flag,
|
2008-07-12 20:49:46 +00:00
|
|
|
locale, "%m$" and "*m$".
|
|
|
|
It knows about the length modifiers \fBh\fP, \fBl\fP, \fBL\fP,
|
2008-09-29 09:00:36 +00:00
|
|
|
\fBZ\fP, and \fBq\fP, but accepts \fBL\fP and \fBq\fP
|
2007-06-25 10:03:10 +00:00
|
|
|
both for \fIlong double\fP and for \fIlong long int\fP (this is a bug).
|
2008-07-12 20:49:46 +00:00
|
|
|
It no longer recognizes \fBF\fP, \fBD\fP, \fBO\fP, and \fBU\fP,
|
|
|
|
but adds the conversion character
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR m ,
|
|
|
|
which outputs
|
|
|
|
.IR strerror(errno) .
|
|
|
|
.PP
|
2008-07-12 20:49:46 +00:00
|
|
|
glibc 2.0 adds conversion characters \fBC\fP and \fBS\fP.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2008-07-12 20:49:46 +00:00
|
|
|
glibc 2.1 adds length modifiers \fBhh\fP, \fBj\fP, \fBt\fP, and \fBz\fP
|
2008-09-29 09:00:36 +00:00
|
|
|
and conversion characters \fBa\fP and \fBA\fP.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2008-07-12 20:49:46 +00:00
|
|
|
glibc 2.2 adds the conversion character \fBF\fP with C99 semantics,
|
|
|
|
and the flag character \fBI\fP.
|
2007-05-19 04:30:20 +00:00
|
|
|
.SH NOTES
|
2008-12-19 16:47:04 +00:00
|
|
|
Some programs imprudently rely on code such as the following
|
|
|
|
|
|
|
|
sprintf(buf, "%s some further text", buf);
|
|
|
|
|
|
|
|
to append text to
|
|
|
|
.IR buf .
|
|
|
|
However, the standards explicitly note that the results are undefined
|
|
|
|
if source and destination buffers overlap when calling
|
|
|
|
.BR sprintf (),
|
|
|
|
.BR snprintf (),
|
|
|
|
.BR vsprintf (),
|
|
|
|
and
|
|
|
|
.BR vsnprintf ().
|
|
|
|
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=7075
|
|
|
|
Depending on the version of
|
|
|
|
.BR gcc (1)
|
|
|
|
used, and the compiler options employed, calls such as the above will
|
|
|
|
.B not
|
|
|
|
produce the expected results.
|
|
|
|
|
2007-05-19 04:30:20 +00:00
|
|
|
The glibc implementation of the functions
|
|
|
|
.BR snprintf ()
|
|
|
|
and
|
|
|
|
.BR vsnprintf ()
|
2007-06-08 11:56:22 +00:00
|
|
|
conforms to the C99 standard, that is, behaves as described above,
|
2007-05-19 04:30:20 +00:00
|
|
|
since glibc version 2.1.
|
|
|
|
Until glibc 2.0.6 they would return \-1
|
|
|
|
when the output was truncated.
|
2007-05-18 11:59:14 +00:00
|
|
|
.\" .SH HISTORY
|
|
|
|
.\" Unix V7 defines the three routines
|
|
|
|
.\" .BR printf (),
|
|
|
|
.\" .BR fprintf (),
|
|
|
|
.\" .BR sprintf (),
|
|
|
|
.\" and has the flag \-, the width or precision *, the length modifier l,
|
|
|
|
.\" and the conversions doxfegcsu, and also D,O,U,X as synonyms for ld,lo,lu,lx.
|
|
|
|
.\" This is still true for 2.9.1BSD, but 2.10BSD has the flags
|
|
|
|
.\" #, + and <space> and no longer mentions D,O,U,X.
|
|
|
|
.\" 2.11BSD has
|
|
|
|
.\" .BR vprintf (),
|
|
|
|
.\" .BR vfprintf (),
|
|
|
|
.\" .BR vsprintf (),
|
|
|
|
.\" and warns not to use D,O,U,X.
|
|
|
|
.\" 4.3BSD Reno has the flag 0, the length modifiers h and L,
|
|
|
|
.\" and the conversions n, p, E, G, X (with current meaning)
|
|
|
|
.\" and deprecates D,O,U.
|
|
|
|
.\" 4.4BSD introduces the functions
|
|
|
|
.\" .BR snprintf ()
|
|
|
|
.\" and
|
|
|
|
.\" .BR vsnprintf (),
|
|
|
|
.\" and the length modifier q.
|
|
|
|
.\" FreeBSD also has functions
|
|
|
|
.\" .BR asprintf ()
|
|
|
|
.\" and
|
|
|
|
.\" .BR vasprintf (),
|
|
|
|
.\" that allocate a buffer large enough for
|
|
|
|
.\" .BR sprintf ().
|
|
|
|
.\" In glibc there are functions
|
|
|
|
.\" .BR dprintf ()
|
|
|
|
.\" and
|
|
|
|
.\" .BR vdprintf ()
|
|
|
|
.\" that print to a file descriptor instead of a stream.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH BUGS
|
|
|
|
Because
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR sprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR vsprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
assume an arbitrarily long string, callers must be careful not to overflow
|
2007-04-12 22:42:49 +00:00
|
|
|
the actual space; this is often impossible to assure.
|
|
|
|
Note that the length
|
2004-11-03 13:51:07 +00:00
|
|
|
of the strings produced is locale-dependent and difficult to predict.
|
|
|
|
Use
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR snprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR vsnprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
instead (or
|
2007-07-08 12:11:40 +00:00
|
|
|
.BR asprintf (3)
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
2007-08-27 08:36:21 +00:00
|
|
|
.BR vasprintf (3)).
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
Linux libc4.[45] does not have a
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR snprintf (),
|
2004-11-03 13:51:07 +00:00
|
|
|
but provides a libbsd that contains an
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR snprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
equivalent to
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR sprintf (),
|
2007-06-08 11:56:22 +00:00
|
|
|
that is, one that ignores the
|
2004-11-03 13:51:07 +00:00
|
|
|
.I size
|
|
|
|
argument.
|
|
|
|
Thus, the use of
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR snprintf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
with early libc4 leads to serious security problems.
|
|
|
|
.PP
|
|
|
|
Code such as
|
|
|
|
.BI printf( foo );
|
|
|
|
often indicates a bug, since
|
|
|
|
.I foo
|
2007-04-12 22:42:49 +00:00
|
|
|
may contain a % character.
|
|
|
|
If
|
2004-11-03 13:51:07 +00:00
|
|
|
.I foo
|
2008-07-12 20:49:46 +00:00
|
|
|
comes from untrusted user input, it may contain \fB%n\fP, causing the
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR printf ()
|
2004-11-03 13:51:07 +00:00
|
|
|
call to write to memory and creating a security hole.
|
|
|
|
.\" .PP
|
2008-07-23 02:57:20 +00:00
|
|
|
.\" Some floating-point conversions under early libc4
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" caused memory leaks.
|
2007-05-19 04:30:20 +00:00
|
|
|
.SH EXAMPLE
|
|
|
|
.if \w'\*(Pi'=0 .ds Pi pi
|
|
|
|
To print \*(Pi to five decimal places:
|
2007-12-19 06:16:04 +00:00
|
|
|
.in +4n
|
2007-05-19 04:30:20 +00:00
|
|
|
.nf
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
fprintf(stdout, "pi = %.5f\en", 4 * atan(1.0));
|
|
|
|
.fi
|
2007-12-19 06:16:04 +00:00
|
|
|
.in
|
2007-05-19 04:30:20 +00:00
|
|
|
.PP
|
2008-06-09 21:03:52 +00:00
|
|
|
To print a date and time in the form "Sunday, July 3, 10:02",
|
2007-05-19 04:30:20 +00:00
|
|
|
where
|
|
|
|
.I weekday
|
|
|
|
and
|
|
|
|
.I month
|
|
|
|
are pointers to strings:
|
2007-12-19 06:16:04 +00:00
|
|
|
.in +4n
|
2007-05-19 04:30:20 +00:00
|
|
|
.nf
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
fprintf(stdout, "%s, %s %d, %.2d:%.2d\en",
|
|
|
|
weekday, month, day, hour, min);
|
|
|
|
.fi
|
2007-12-19 06:16:04 +00:00
|
|
|
.in
|
2007-05-19 04:30:20 +00:00
|
|
|
.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:
|
2007-12-19 06:16:04 +00:00
|
|
|
.in +4n
|
2007-05-19 04:30:20 +00:00
|
|
|
.nf
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
fprintf(stdout, format,
|
|
|
|
weekday, month, day, hour, min);
|
|
|
|
|
|
|
|
.fi
|
2007-12-19 06:16:04 +00:00
|
|
|
.in
|
2007-05-19 04:30:20 +00:00
|
|
|
where
|
|
|
|
.I format
|
|
|
|
depends on locale, and may permute the arguments.
|
2007-12-23 09:17:10 +00:00
|
|
|
With the value:
|
2007-12-19 06:16:04 +00:00
|
|
|
.in +4n
|
2007-05-19 04:30:20 +00:00
|
|
|
.nf
|
2007-12-23 09:17:10 +00:00
|
|
|
|
2007-05-19 04:30:20 +00:00
|
|
|
"%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
|
2007-12-23 09:17:10 +00:00
|
|
|
|
2007-05-19 04:30:20 +00:00
|
|
|
.fi
|
2007-12-19 06:16:04 +00:00
|
|
|
.in
|
2008-06-09 21:03:52 +00:00
|
|
|
one might obtain "Sonntag, 3. Juli, 10:02".
|
2007-05-19 04:30:20 +00:00
|
|
|
.PP
|
|
|
|
To allocate a sufficiently large string and print into it
|
|
|
|
(code correct for both glibc 2.0 and glibc 2.1):
|
|
|
|
.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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.fi
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR printf (1),
|
|
|
|
.BR asprintf (3),
|
|
|
|
.BR dprintf (3),
|
|
|
|
.BR scanf (3),
|
2006-05-01 01:52:57 +00:00
|
|
|
.BR setlocale (3),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR wcrtomb (3),
|
|
|
|
.BR wprintf (3),
|
|
|
|
.BR locale (5)
|