Constants
Constants are literal data items written into a User Language Program. According to the different data types, there are also different types of constants:
- Character constants
- Integer constants
- Real constants
- String constants
Character Constants
A character constant consists of a single character or an escape sequence enclosed in single quotes, like
'a'
'='
'\n'
The type of a character constant is char
.
Integer Constants
Depending on the first (and possibly the second) character, an integer constant is assumed to be expressed in different base values:
first | second | constant interpreted as |
0 | 1-7 | octal (base 8) |
0 | x,X | hexadecimal (base 16) |
1-9 | decimal (base 10) |
The type of an integer constant is int.
Examples:
16 | decimal |
020 | octal |
0x10 | hexadecimal |
Real Constants
A real constant follows the general pattern
[-]int.frac[e|E[±]exp]
which stands for
- optional sign
- decimal integer
- decimal point
- decimal fraction
- e or E and a signed integer exponent
You can omit either the decimal integer or the decimal fraction (but not both). You can omit either the decimal point or the letter e or E and the signed integer exponent (but not both).
The type of a real constant is real
.
Examples:
Constant | Value |
---|---|
23.45e6 | 23.45 x 10^6 |
.0 | 0.0 |
0. | 0.0 |
1. | 1.0 |
-1.23 | -1.23 |
2e-5 | 2.0 x 10^-5 |
3E+10 | 3.0 x 10^10 |
.09E34 | 0.09 x 10^34 |
String Constants
A string constant consists of a sequence of characters or escape sequences enclosed in double quotes, like
"Hello world\n"
The type of a string constant is string
.
String constants can be of any length (provided there is enough free memory available). String constants can be concatenated by simply writing them next to each other to form larger strings:
string s = "Hello" " world\n";
It is also possible to extend a string constant over more than one line by escaping the newline character with a backslash (\
):
string s = "Hello \
world\n";
Escape Sequences
An escape sequence consists of a backslash (\
), followed by one or more special characters:
Sequence | Value |
---|---|
\a |
audible bell |
\b |
backspace |
\f |
form feed |
\n |
new line |
\r |
carriage return |
\t |
horizontal tab |
\v |
vertical tab |
\\ |
backslash |
\' |
single quote |
\" |
double quote |
\O |
O = up to 3 octal digits |
\xH |
H = up to 2 hex digits |
Any character following the initial backslash that is not mentioned in this list will be treated as that character (without the backslash).
Escape sequences can be used in character constants and string constants.
Examples:
'\n'
"A tab\tinside a text\n"
"Ring the bell\a\n"