Literals
Literals are tokens representing fixed numeric or character values.
The data type of a constant is deduced by the compiler using such clues as numeric value and format used in the source code.
Integer Literals
Integral values can be represented in decimal, hexadecimal or binary notation.
In decimal notation, numerals are represented as a sequence of digits (without commas, spaces or dots), with optional prefix +
or -
operator to indicate the sign. Values default to positive (6258
is equivalent to +6258
).
The dollar-sign prefix ($
) or the prefix 0x
indicates a hexadecimal numeral (for example, $8F
or 0x8F
).
The percent-sign prefix (%
) indicates a binary numeral (for example, %0101
).
Here are some examples:
11 ' decimal literal $11 ' hex literal, equals decimal 17 0x11 ' hex literal, equals decimal 17 %11 ' binary literal, equals decimal 3
The allowed range of values is imposed by the largest data type in mikroBasic PRO for ARM – longword
. The compiler will report an error if the literal exceeds 4294967295
($FFFFFFFF
).
Floating Point Literals
A floating-point value consists of:
- Decimal integer
- Decimal point
- Decimal fraction
- e or E and a signed integer exponent (optional)
You can omit either decimal integer or decimal fraction (but not both).
Negative floating constants are taken as positive constants with the unary operator minus (-) prefixed.
mikroBasic PRO for ARM limits floating-point constants to the range of ±1.17549435082 * 10-38 .. ±6.80564774407 * 1038.
Here are some examples:
0. ' = 0.0 -1.23 ' = -1.23 23.45e6 ' = 23.45 * 10^6 2e-5 ' = 2.0 * 10^-5 3E+10 ' = 3.0 * 10^10 .09E34 ' = 0.09 * 10^34
Character Literals
Character literal is one character from the extended ASCII character set, enclosed with quotes (for example, "A"
). Character literal can be assigned to variables of byte
and char
type (variable of byte
will be assigned the ASCII value of the character). Also, you can assign character literal to a string variable.
String Literals
String literal is a sequence of characters from the extended ASCII character set, enclosed with quotes. Whitespace is preserved in string literals, i.e. parser does not “go into” strings but treats them as single tokens.
Length of string literal is a number of characters it consists of. String is stored internally as the given sequence of characters plus a final null
character. This null
character is introduced to terminate the string, it does not count against the string’s total length.
String literal with nothing in between the quotes (null string) is stored as a single null
character.
You can assign string literal to a string variable or to an array of char
.
Here are several string literals:
"Hello world!" ' message, 12 chars long "Temperature is stable" ' message, 21 chars long " " ' two spaces, 2 chars long "C" ' letter, 1 char long "" ' null string, 0 chars long
The quote itself cannot be a part of the string literal, i.e. there is no escape sequence. You could use the built-in function Chr to print a quote: Chr(34)
. Also, see String Splicing.
What do you think about this topic ? Send us feedback!