Literals
A literal is a value written straight into the source. Integers can be written in four bases, floating-point numbers with a point or an exponent, and a suffix can pin a literal to a particular type.
'' Integers in four bases. The prefix names the base.
print "decimal 255 ="; 255
print "hex &hFF ="; &hFF
print "octal &o377 ="; &o377
print "binary &b11111111 ="; &b11111111
'' Floating-point literals: a point, an exponent, or both.
print "1.5 ="; 1.5
print "2.5e3 ="; 2.5e3
print "1e-4 ="; 1e-4
print ".25 ="; .25
'' A suffix fixes the literal's type; sizeof shows the width it chose.
print "sizeof( 1 ) ="; sizeof( 1 )
print "sizeof( 1L ) ="; sizeof( 1L )
print "sizeof( 1LL ) ="; sizeof( 1LL )
print "sizeof( 1UL ) ="; sizeof( 1UL )
print "sizeof( 1.5 ) ="; sizeof( 1.5 )
print "sizeof( 1.5F ) ="; sizeof( 1.5F )
'' Booleans and text.
print "true and false: "; true; " "; false
print "a ustring literal: "; "in double quotes"
print !"an escaped literal:\tafter a tab"
Output
decimal 255 = 255
hex &hFF = 255
octal &o377 = 255
binary &b11111111 = 255
1.5 = 1.5
2.5e3 = 2500
1e-4 = 0.0001
.25 = 0.25
sizeof( 1 ) = 8
sizeof( 1L ) = 4
sizeof( 1LL ) = 8
sizeof( 1UL ) = 4
sizeof( 1.5 ) = 8
sizeof( 1.5F ) = 4
true and false: true false
a ustring literal: in double quotes
an escaped literal: after a tab
| Prefix | Base | Example |
|---|---|---|
| none | 10 | 255 |
&h | 16 | &hFF |
&o | 8 | &o377 |
&b | 2 | &b11111111 |
A literal with a point or an exponent (1.5, 2.5e3, .25) is floating
point. tiko has no digit separator: 1_000_000 is error TKO0420: expected the end of the statement.
A plain whole-number literal is an integer, which is 8 bytes on the 64-bit
targets; a plain floating-point literal is a float64. The suffixes change
that:
| Suffix | Type |
|---|---|
L | int32 |
LL | int64 |
U | uinteger |
UL | uint32 |
ULL | uint64 |
F | float32 |
D | float64 |
Literal types shows how to see the type a literal gets, including one case that surprises people.
true and false are values of type boolean. Text goes in double quotes;
with a ! in front, backslash escapes such as \t are read, so the last line
prints a real tab. Strings has the full set.
Activities
- Print
&h7FFFFFFFand&b10000000and check the values by hand. - Print
cbool( &o17 = &b1111 )and work out why the answer is what it is.