Jump to content

Mercury Programming/Types

From Wikibooks, open books for an open world

Types

[edit | edit source]

A value of type 'char' is a single unicode character (implemented using UTF8 encoding and so may occupy 1 to 4 bytes). There are several ways to specify a 'char' as a literal value:

  • As an ordinary character between single quotes, for example 'a' - unless:
    • the character is a single quote, in which case use 4 single quotes: '''', or use 3 single quotes and with a backslash as the second character: '\''.
    • the character is a backslash, in which case use 2 backslashes between single quotes: '\\'.
    • the character is the double quote, in which case put it between single quotes: '”', or give it a leading backslash and put them between single quotes: '\”'.
  • As a hexadecimal value giving the Unicode code point (see Unicode) of the required character; the hexadecimal value must have a leading 'x', placed between '\' characters, which are in turn placed between single quotes, for example '\x63\' (equivalent to the character 'c').
  • As an octal value giving the Unicode code point of the required character; the octal value must be placed between '\' characters, which are in turn placed between single quotes, for example '\251\' (equivalent to the character '©').
  • As a 4 digit hexadecimal value giving the Unicode code point of the required character; the value must be prefixed with '\u', and the result placed between single quotes, for example '\u00B5' (equivalent to the Greek lower case character μ).
  • As an 8 digit hexadecimal value giving the Unicode code point of the required character; the value must be prefixed with '\U', and the result placed between single quotes, for example '\U000000B1' (equivalent to the character '±'). Note that the highest allowed value is '\U0010FFFF'.
  • Several special characters can be specified as shown in the table below:
Character Specified Using
beep '\a'
backspace '\b'
carriage return '\r'
form feed '\f'
tab '\t'
new line '\n'
vertical tab '\v'


A simple Mercury program demonstrating 'char' literals is in Section A.1