Printf Format Specifiers Reference
printf is a C Standard Library function that formats text and writes it to
the standard output. Versions of the function can write to other destinations
or be used simply to format to another string/buffer. JANOS provides this
handy function for use in Java programming and in PHP rendering/scripting.
This function uses a
format specification string defining how any number
of variable values and types may be combined into string form. For example
here we use the function in scripting from the command line:
!printf( "Pi is %.10f", pi());
Pi is 3.1415926536
The function call is unique in that is accepts one or more arguments or
parameters following the format string. The syntax is:
printf(
format ,
parameter list ... )
The format string includes text to be copied to the output or buffer as well
as one or more
format specifiers relating in order to the supplied parameter
values/strings.
FORMAT SPECIFIERS
The
format string is the string that contains text to be written. It can
optionally contain one or more format specifiers or embedded tags that are
replaced with values taken one at a time from the remaining parameters. The
format tag syntax is as follows:
%[flags][width][.precision][length]specifier
The
specifier must be one of the following:
c character
d or i signed decimal integer
e scientific notation using lower case
E scientific notation using upper case
f decimal floating point
g uses the shorter of %e or %f
G uses the shorter of %E or %f
o signed octal
p hexadecimal pointer using lowercase alpha
s ASCIIZ (nul terminated) string
u unsigned decimal integer
x hexadecimal using lowercase alpha
X hexadecimal using uppercase alpha
The
flags includes special characters that indicate handling of the
formatted value. These are:
- Indicates that the result is to be left-justified within the
given field
width.
+ Causes the sign to always be included when formatting values
even when the value is positive.
(space) Reserves a character spot for the sign (uses a space instead
of '+' for a positive value).
# When used with %o, %x or %X specifiers the resulting value is
preceded with '0', '0x' or '0X' respectively for values that
are different than zero. When used with %e, %E and %f, the
written output will contain a decimal point even if no digits
would follow. By default, if no digits follow, no decimal point
is written. When used with %g or %G the result is the same as
with %e or %E but trailing zeros
are not removed.
0 The '0' flag causes numbers to be left-padded with zeroes '0'
instead of spaces when some form of padding is specified
(see
width sub-specifier).
The
width specification defines the length of the rendered test field. This
can be dynamically set.
(number) Defines the minimum number of characters to be printed. If the
value to be printed is shorter than this number, the result is
padded appropriately with blank spaces. The value is not
truncated if the formatted result is longer. In that case the
rendered text will be wider than specified.
* An asterisk is used when the
width is specified by the next
parameter in the parameter list of the function call. In this
manner the width can be dynamically controlled.
The
precision specifier follows a decimal point in the format string.
.number When used with integer specifiers (%d, %i, %o, %u, %x, %X)
this specifies the minimum number of digits to be written. If
the value to be written is shorter than this number, the result
is padded with leading zeros. The value is not truncated if the
result is longer. A precision of '0' means that no character
is written for the value 0. For %e, %E and %f specifiers this
is the number of digits to be printed after the decimal point.
For %g and %G specifiers this is the maximum number of
significant digits to be printed. For %s this is the maximum
number of characters to be printed. By default all characters
are printed until the ending nul character is encountered. For
%c type it has no effect. When no
precision is specified, the
default is 1. If the period is specified without an explicit
value for precision 0 is assumed.
.* The
precision is defined by the next parameter in the
function parameter list.
The
length specifies the size of the supplied parameter.
h The argument is interpreted as a 16-bit (2 byte) short int or
unsigned short int. This applies to integer specifiers %i, %d,
%o, %u, %x, and %X.
l (lowercase L) Interprets the argument as a long int or unsigned
long int 32-bit (4 bytes) as it applies to integer specifiers
%i, %d, %o, %u, %x, and %X. When used with %c or %s specifiers
it indicates the use of wide characters (16-bit Unicode).
ll (two lowercase Ls) Interprets the argument as a long long int
or unsigned long long int 64-bit (8 bytes) as it applies to
integer specifiers %i, %d, %o, %u, %x, and %X.
L The argument is interpreted as long double 64-bit (8 byte). This
applies to floating point specifiers %e, %E, %f, %g and %G.
Note that Java applications and PHP scripts have access to the underlying
C Standard Library
printf() function. In both cases the supplied parameters
are cast to best match the defined format specifier. This may or may not
provide expected results. It is helpful to use the command line scripting
as shown above to test formatting strings if you have any question as to the
outcome.
SEE ALSO
HELP Topics:
OUTPUT,
SPRINTF
[/flash/manpages/reference.hlp:597]