next up previous contents index
Next: 1.6 String Manipulation Up: 1. Library Utilities Previous: 1.4 Ground, Numbervars, Subsumption,   Contents   Index

1.5 Formatted Output

format(+String,+Control)
format
format(+Stream,+String,+Control)
format

format/2 and format/3 act as a Prolog analog to the C stdio function printf(), allowing formatted output 1.5.

Output is formatted according to String which can contain either a format control sequence, or any other character which will appear verbatim in the output. Control sequences act as place-holders for the actual terms that will be output. Thus

        ?- format("Hello ~q!",world).

will print Hello world!.

If there is only one control sequence, the corresponding element may be supplied alone in Control. If there are more, Control must be a list of these elements. If there are none then Control must be an empty list. There have to be as many elements in Control as control sequences in String.

The character ~ introduces a control sequence. To print a ~ just repeat it:

        ?- format("Hello ~~world!", []).
will output Hello ~world!.

The general format of a control sequence is ~NC. The character C determines the type of the control sequence. N is an optional numeric argument. An alternative form of N is *. * implies that the next argument in Arguments should be used as a numeric argument in the control sequence. For example:

?- format("Hello~4cworld!", [0'x]).

and

?- format("Hello~*cworld!", [4,0'x]).

both produce

Helloxxxxworld!

The following control sequences are available in XSB.


next up previous contents index
Next: 1.6 String Manipulation Up: 1. Library Utilities Previous: 1.4 Ground, Numbervars, Subsumption,   Contents   Index
Terrance Swift 2007-10-06