In atoms (enclosed by single quotes) and strings it is possible to use escape sequences and symbolic control characters. An escape sequence is an octal or hexadecimal number, for instance \165 or \x67, of which the decimal value is calculated and the complete sequence is replaced by a single character with the same ASCII value as the calculated decimal value. By using this technique it is possible to insert characters into an atom or a string which cannot be input by using the keyboard or which otherwise would produce invalid tokens according to the Prolog syntax.
A hexadecimal escape sequence starts with the characters '\x' and an octal sequence start with just a single '\' character. All valid characters, or until a backslash character is detected, are treated as part of the escape sequence.
| Examples | |
| X="a\1234567". | the octal escape sequence 1234567 is converted to the decimal value 342391, this number is cast to an ASCII character which results in the character 'w', the string created is "aw" |
| X='\8'. | the atom created is '\8' because 8 is not an octal number, valid octal numbers are [0, 1, 2, 3, 4, 5, 6, 7] |
| X='\72\8 34567'. | the octal escape sequence is '72' and this escape sequence is replaced by the character ':', the atom created is ':8 34567' |
| X="\x53\G 84". | the hexadecimal escape sequence '\x53' is translated to the character 'S', the created string is "SG 84" |
Symbolic control characters can be regarded as predefined escape sequences, the entire symbolic character sequence is replaced by a single character. The eight built-in symbolic control characters are:
| Control characters |
| \a | alert, ASCII character nr. 7 |
| \b | backspace, ASCII character nr. 8 |
| \r | carriage return, ASCII character nr. 13 |
| \f | form feed, ASCII character nr. 12 |
| \t | horizontal tab, ASCII character nr. 9 |
| \n | newline, ASCII character nr. 10 |
| \v | vertical tab, ASCII character nr. 11 |
| \\ | backslash, ASCII character nr. 92 |
The compiler replaces each set of two backslash characters with 1 backslash character! This is very important to remember while specifying filenames:
| Examples | |
| consult("c:\\temp\\st23.txt"). | open the file "c:\temp\st23.txt" |
| consult('c:\temp\back.txt'). | the filename string is incorrect: the '\t' sequence is replaced by a tab character and the '\b' sequence is replaced by a backspace character |
If a single backslash character is written to a stream then it is expanded to a sequence of two backslash characters to create a correct character sequence that will be successfully compiled again. The Prolog flag expand_backslash can switch this exapnding of backslash characters on or off.