2.16.1.3 Character Escape Syntax
Within quoted atoms (using single quotes: '<atom>'
)
special characters are represented using escape sequences. An escape
sequence is led in by the backslash (
)
character. The list of escape sequences is compatible with the ISO
standard but contains some extensions, and the interpretation of
numerically specified characters is slightly more flexible to improve
compatibility. Undefined escape characters raise a \
syntax_error
exception.25Up to SWI-PrologĀ 6.1.9,
undefined escape characters were copied verbatim, i.e., removing the
backslash.
\a
- Alert character. Normally the ASCII character 7 (beep).
\b
- Backspace character.
\c
- No output. All input characters up to but not including the first
non-layout character are skipped. This allows for the specification of
pretty-looking long lines. Not supported by ISO. Example:
format('This is a long line that looks better if it was \c split across multiple physical lines in the input')
\<NEWLINE>
- When in ISO mode (see the Prolog flag iso),
only skip this sequence. In native mode, white space that follows the
newline is skipped as well and a warning is printed, indicating that
this construct is deprecated and advising to use
\c
. We advise using\c
or putting the layout before the
, as shown below. Using\
\c
is supported by various other Prolog implementations and will remain supported by SWI-Prolog. The style shown below is the most compatible solution.26Future versions will interpret
<return> according to ISO.\
format('This is a long line that looks better if it was \ split across multiple physical lines in the input')
instead of
format('This is a long line that looks better if it was\ split across multiple physical lines in the input')
Note that SWI-Prolog also allows unescaped newlines to appear in quoted material. This is not allowed be the ISO standard, but used to be common practice before.
\e
- Escape character (ASCII 27). Not ISO, but widely supported.
\f
- Form-feed character.
\n
- Next-line character.
\r
- Carriage-return only (i.e., go back to the start of the line).
\s
- Space character. Intended to allow writing
0'\s
to get the character code of the space character. Not ISO. \t
- Horizontal tab character.
\v
- Vertical tab character (ASCII 11).
\
xXX..\
- Hexadecimal specification of a character. The closing
\
is obligatory according to the ISO standard, but optional in SWI-Prolog to enhance compatibility with the older Edinburgh standard. The code\xa\3
emits the character 10 (hexadecimal `a') followed by `3'. Characters specified this way are interpreted as Unicode characters. See also\u
. \uXXXX
- Unicode character specification where the character is specified using
exactly 4 hexadecimal digits. This is an extension to the ISO
standard, fixing two problems. First, where
\x
defines a numeric character code, it doesn't specify the character set in which the character should be interpreted. Second, it is not needed to use the idiosyncratic closing
ISO Prolog syntax.\
\UXXXXXXXX
- Same as
\uXXXX
, but using 8 digits to cover the whole Unicode set. \40
- Octal character specification. The rules and remarks for hexadecimal specifications apply to octal specifications as well.
\
\
- Escapes the backslash itself. Thus,
'\\'
is an atom consisting of a single
.\
\'
- Single quote. Note that
'\''
and''''
both describe the atom with a singleĀ'
, i.e.,'\'' == ''''
is true. \"
- Double quote.
\`
- Back quote.
Character escaping is only available if
current_prolog_flag(character_escapes, true)
is active
(default). See current_prolog_flag/2.
Character escapes conflict with writef/2
in two ways: \40
is interpreted as decimal 40 by writef/2,
but as octal 40 (decimal 32) by read
. Also, the writef/2
sequence
\l
is illegal. It is advised to use the more widely
supported
format/[2,3]
predicate instead. If you insist upon using writef/2,
either switch character_escapes
to false
, or use double \\
, as in writef('\\l')
.