Format
1. General Form: (format Destination String Arg*)
Format is the general printing routine, similar to C's printf/fprintf/sprintf procedures. It can be used to print to the screen, into a string, or into an arbitrary "stream" (generally a file, but can also be a process).
2. Destination: t/NIL/Stream
* T
(format t String Args) prints String to the screen, but with the Args substituted into the String in places where there were entries like ~X. The printing is a side effect; it always returns NIL. For instance,
(format t "Hello") prints Hello (no quotes) on the screen, then returns NIL
(format t "The value of pi is ~S" pi) prints 'The value of pi is 3.141592653...'
For you C programmers, (format t String Arg1 Arg2 Arg3) is very similar to C's "printf(String, Arg1, Arg2, Arg3)", or
"fprintf(stdio, String, Arg1, Arg2, Arg3)".
* NIL
(format NIL String Args) is similar to the above, but instead of printing the result, it returns it as a string. This is similar to C's sprintf routine. E.g.
(format NIL "(first '~S) is ~S" '(A B C) (first '(A B C)))
returns the string "(first '(A B C)) is A".
3. Directives
~%: Insert a CR. This is like \n in a C printf call.
~&: Insert a CR unless already at beginning of line.
~S: S-expression. Insert an arbitrary Lisp expression, and print it just as that expression
would normally be printed if it were returned as a value.
~A: ASCII. Similar to ~S, but if one of the args is a string, its double quotes will be dropped.
~D: Integer (like %d in C)
~widthD: extra spaces. (format NIL "~5D" 6) returns " 6"
~:D: commas. (format NIL "~:D" 1234567) returns "1,234,567"
~F: Floating point (like %f in C)
~width,decimalsF
~ : for long lines. This lets you hit CR in the string (to keep it within 80 columns), but not really
put a CR in the output.
(many more : Justifying, Roman numerals, iteration, binary/hex, plural, etc.)