Next: , Previous: , Up: Standard Formatted I/O   [Contents][Index]


4.3.2 Standard Formatted Output

(require 'printf)

Procedure: printf format arg1 …
Procedure: fprintf port format arg1 …
Procedure: sprintf str format arg1 …
Procedure: sprintf #f format arg1 …
Procedure: sprintf k format arg1 …

Each function converts, formats, and outputs its arg1 … arguments according to the control string format argument and returns the number of characters output.

printf sends its output to the port (current-output-port). fprintf sends its output to the port port. sprintf string-set!s locations of the non-constant string argument str to the output characters.

Two extensions of sprintf return new strings. If the first argument is #f, then the returned string’s length is as many characters as specified by the format and data; if the first argument is a non-negative integer k, then the length of the returned string is also bounded by k.

The string format contains plain characters which are copied to the output stream, and conversion specifications, each of which results in fetching zero or more of the arguments arg1 …. The results are undefined if there are an insufficient number of arguments for the format. If format is exhausted while some of the arg1 … arguments remain unused, the excess arg1 … arguments are ignored.

The conversion specifications in a format string have the form:

% [ flags ] [ width ] [ . precision ] [ type ] conversion

An output conversion specifications consist of an initial ‘%’ character followed in sequence by:

Exact Conversions

b’, ‘B

Print an integer as an unsigned binary number.

Note:%b’ and ‘%B’ are SLIB extensions.

d’, ‘i

Print an integer as a signed decimal number. ‘%d’ and ‘%i’ are synonymous for output, but are different when used with scanf for input (see Standard Formatted Input).

o

Print an integer as an unsigned octal number.

u

Print an integer as an unsigned decimal number.

x’, ‘X

Print an integer as an unsigned hexadecimal number. ‘%x’ prints using the digits ‘0123456789abcdef’. ‘%X’ prints using the digits ‘0123456789ABCDEF’.

Inexact Conversions

f

Print a floating-point number in fixed-point notation.

e’, ‘E

Print a floating-point number in exponential notation. ‘%e’ prints ‘e’ between mantissa and exponont. ‘%E’ prints ‘E’ between mantissa and exponont.

g’, ‘G

Print a floating-point number in either fixed or exponential notation, whichever is more appropriate for its magnitude. Unless an ‘#’ flag has been supplied, trailing zeros after a decimal point will be stripped off. ‘%g’ prints ‘e’ between mantissa and exponont. ‘%G’ prints ‘E’ between mantissa and exponent.

k’, ‘K

Print a number like ‘%g’, except that an SI prefix is output after the number, which is scaled accordingly. ‘%K’ outputs a dot between number and prefix, ‘%k’ does not.

Other Conversions

c

Print a single character. The ‘-’ flag is the only one which can be specified. It is an error to specify a precision.

s

Print a string. The ‘-’ flag is the only one which can be specified. A precision specifies the maximum number of characters to output; otherwise all characters in the string are output.

a’, ‘A

Print a scheme expression. The ‘-’ flag left-justifies the output. The ‘#’ flag specifies that strings and characters should be quoted as by write (which can be read using read); otherwise, output is as display prints. A precision specifies the maximum number of characters to output; otherwise as many characters as needed are output.

Note:%a’ and ‘%A’ are SLIB extensions.

%

Print a literal ‘%’ character. No argument is consumed. It is an error to specify flags, field width, precision, or type modifiers with ‘%%’.


Next: , Previous: , Up: Standard Formatted I/O   [Contents][Index]