CHAPTER 10 LIBRARY FUNCTIONS
User’s Manual U15556EJ1V0UM
180
sprintf
I/O Functions
f
.................. Converts double argument as a signed value with [
] dddd.dddd format.
dddd is one or more decimal number(s). The number of digits before the decimal point is
determined by the absolute value of the number, and the number of digits after the decimal
point is determined by the required precision. When the precision is omitted, it is interpreted
as 6.
e
................. Converts double argument as a signed value with [
] d.dddd e [sign] ddd format. d is one
decimal number, and dddd is one or more decimal number(s). ddd is exactly a three-digit
decimal number, and the sign is + or
. When the precision is omitted, it is interpreted as 6
E
................. The same format as that of e except E is added instead of e before the exponent.
g
................. Uses whichever shorter method of f or e format when converting double argument based on
the specified precision. e format is used only when the exponent of the value is smaller than
4 or larger than the specified number by precision.
The following 0 are truncated, and the decimal point is displayed only when one or more
numbers follow.
G
................. The same format as that of g except E is added instead of e before the exponent.
c
................. Converts int argument to unsigned char and writes the result as a single character.
s
................. The associated argument is a pointer to a string of characters and the characters in the string
are written up to the terminating null character (but not included in the output). If precision is
specified, the characters exceeding the maximum field width will be truncated off the end.
When the precision is not specified or larger than the array, the array must include a null
character.
p
................. The associated argument is a pointer to
void
and the pointer value is displayed in unsigned
hexadecimal 4 digits (with 0s prefixed to less than a 4-digit pointer value). In the case of the
large model, the pointer value is displayed in unsigned hexadecimal 8 digits (the higher 2
digits are padded by 0 and displayed with 0s prefixed to less than a 6-digit pointer value). The
precision specification if any will be ignored.
n
................. The associated argument is an integer pointer into which the number of characters written thus
far in the string “s” is placed. No conversion is performed.
%
................ Prints a % sign. The associated argument is not converted (but the flag and minimum field
width specifications are effective).
Operations for invalid conversion specifiers are not guaranteed.
When the actual argument is a union or a structure, or the pointer to indicate them (except the character
type array in % s conversion or the pointer in % p conversion), operations are not guaranteed.
The conversion result will not be truncated even when there is no field width or the field width is small. In
other words, when the number of characters of the conversion result are larger than the field width, the
field is extended to the width that includes the conversion result.
The formats of the special output character string in %f, %e, %E, %g, %G conversions are shown below.
non-numeric
→
“(NaN)”
+
∞
→
“(+INF)”
–
∞
→
“(–INF)”
sprintf
writes a null character at the end of the string
s
. (This character is included in the return value count.)
The syntax of
format
commands is illustrated in Figure 10-3.