Literals
There are three types of literal constant - numerals, character constants, and string constants.
Numerals are written in the following style
12 5237563 24.6 4.5e13 0.63e-6
A numeral containing either a decimal point or a scale factor (.
or
e
) is fractional, and is stored internally as double precision
floating point (accuracy approximately 17 decimal digits). Integers are
held in a different internal representation, and have unbounded
precision.
The two kinds of number, integer and fractional, are both of type num
,
as far as the type-checker is concerned, and can be freely mixed in
calculations. There is automatic conversion from integer to fractional
when required, but not in the opposite direction. To convert from
fractional to integer, use entier
(see standard environment).
Negative numbers are denoted by applying the prefix -
operator to a
numeral, thus:
-12 -4.5e13
but note that the notation -12 is an expression, not a literal, so if you wish to apply a function to it, you must write f (-12), not f -12, which would be read as an attempt to subtract 12 from f.
Character constants are written using single quotes, thus
'a' '0' '\n'
String constants are written using double quotes, thus
"hello dolly" "" "\n\n\n"
Escape conventions in character and string constants are as in C
,
using the backslash character.
\n newline
\t tab
\f formfeed
\r carriage return
\b backspace
\\ the backslash character itself
\' single quote
\" double quote
\ddd up to three decimal digits, arbitrary ascii character code
Note that literal newlines are not allowed inside character or string quotes, unless escaped by backslash, in which case the newline is ignored. Thus
"hello \
dolly" means the same as "hello dolly".
These conventions are exactly as in C
, except that we use decimal
rather than octal for numeric specification of an arbitrary ascii
character code (the use of octal numbers in computing now seems a
curious anachronism which there is no need to perpetuate).