Constants and Literals

Top  Previous  Next

Constants are identifier to numeric mappings that cannot change value. Similar to variables in that they are referred to by name but don't actually use memory or generate any code. The compiler substitutes a constant identifier with its numeric identity or string literal at compile time. The three built in statements for defining constants are CONST, SETID, and $DEFINE

The CONST statement creates traditional name to value mappings common in other languages. SETID is unique to the EBASIC language to create built in constants for the language accessed with the '@' operator. SETID constants are specially highlighted in the IDE and used throughout the command sets for built in function constants.

The $DEFINE operator is normally used to create a conditional identifier, but may also be used to create the same kind of constants that the CONST statement does. When using $DEFINE to create a constant the equal sign is not necessary.

Examples:

CONST WM_USER = 0x400
SETID "RTEDIT", 0x200
CONST ID_GAME = "Blaster Version 1.25"
CONST PI = 3.1415
$DEFINE ID_MENU 100
PRINT WM_USER, @RTEDIT, ID_GAME, PI

CONST allows basic  math operations to calculate a numeric identity. Since the final result must resolve to a fixed numeric value you cannot use functions or variables when calculating constants. You can however use other constant identifiers in the calculations.
 

CONST WM_USER = 0x400
CONST MY_MESSAGE = WM_USER + 1
CONST MY_MESSAGE2 = MY_MESSAGE + 1

 

String literals

A string literal is text enclosed in quotes. The compiler supports string escape sequences to insert special ASCII values into the string without having to use CHR$ to generate them. String literals are stored in the executable when compiled.

Examples:
 

Mystring = "This is a string"
' a string with embedded quotes
MyString2 = "This \"is\" a string"

All escape sequences begin with a single backslash ' \ '. Because of this in order to have a backslash in the string itself you need to use a double backslash ' \\ '. This is important to remember when working with filenames.

Myfile = "C:\\data\\inp.text"

Supported escape sequences:

Sequence

Inserted character

\n

New line character

\t

TAB character

\\

A single backslash

\"

A quote

\xnn

An ASCII character whose hex value is 'nn'.

The last sequence deserves a bit of explanation. In other BASIC's it was necessary to use CHR$ to insert a non printable character into a string. For example the ESC character is 0x1B in hexadecimal. A$ = CHR$(0x1B) .  With Emergence BASIC you can insert the character directly into the string by specifying its code in hex. This saves time and results in smaller programs

A$ = "\x1BThere is an <ESC> character at the beginning"

The maximum length of a string literal is 1023 bytes. If you need a longer string literal then append them together with the concatenation operator. It is quite unusual to have a string literal this long, consider using DATA statements to separate your strings.

Unicode literals

A Unicode literal string is prefixed with the L character. This tells the compiler to convert the literal to UTF-16 and return a Unicode string. Unicode strings support new line, tab, backslash and quote escape sequences.

UNAME$ = L"John Smith"

Numeric literals

Numeric literals are sometimes called numeric constants. We use the term literals to differentiate them from the CONST keyword and to avoid confusion. A numeric literal is simply a number, either integer or floating point, entered directly into the source code. Such as when assigning to a variable:
 

A  = 1.3452 : REM The number is the literal.

Direct entering of exponents is also allowed by the compiler
 

A = 1.2e-10: REM Set a to equal 0.00000000012

Hexadecimal numbers may also be directly specified by using the 0x identifier
 

A = 0x500 + 0x2FFF

The older form of &h is still available but only if the hexadecimal number starts with a numeric and not a hex letter
 

A = &h1FFF: REM Acceptable
A = &hFFFF: REM Not acceptable, use a leading 0 as in &h0FFFF

Numeric modifiers

The compiler supports modifiers to the entered number to change its type. If a number is entered without a decimal point it is treated as an INT type (4 bytes) and with a decimal point as a DOUBLE type (8 bytes).  It is sometimes desirable to modify the type of the numeric to tell the compiler how to convert and store it. A standard INT literal is not large enough to hold a UINT64 literal for example. Modifiers appear as a single character following the literal
 

REM INT64, A will be defined as an INT64
REM if not already defined
A = 36674965736284q
 
REM FLOAT. flNum will be defined as type FLOAT
REM if not already defined
flNum = 1.234f

Supported modifiers:

Modifier

Result

q

INT64

f

FLOAT

u

UINT

The u modifier was specifically designed for entering hexadecimal numbers as an unsigned quantity. Normally all non decimal numbers are treated as a signed integer which can cause calculation problems when using hexadecimal numbers.
 

A = 0xFFFFFFFFu -1u

Without the u modifier A, if not already defined, would have a type of INT and a value of -2.  By specifying the u modifier A is defined as a UINT with a value of 0xFFFFFFFE or 4294967294.

Overriding defaults

The numeric modifier for FLOAT can be a bit cumbersome if there are a lot of numeric constants in your program to be treated as a single precision (FLOAT) number. For example many graphics program do not require the precision offered by DOUBLE precision numbers and can benefit in the speed and compactness of using all 32 bit FLOAT types.  To override the compilers treatment of numeric constants containing a decimal point use the $OPTION "FLOAT" preprocessor command.
 

$OPTION "FLOAT"
REM These will all be defined as type FLOAT
flNum = 1.234
Speed = 1.1
xAdj = 1.0
yAdj = 2.0

To return to the default at any point in your source code use the $OPTION "DOUBLE" preprocessor command
 

$OPTION "DOUBLE"
REM These will all be defined as type DOUBLE
dbNum = 1.707546125842
gravity = .000124578