Variables, Type Suffixes & Data Types
A suffix character on the name fixes the type.
6.1 Variable names
Start with a letter; can contain letters, digits, and periods (QBasic) — QB64 also allows underscores but not as the first character.
Up to 40 characters in QBasic; much longer in QB64.
Not case-sensitive: Score, SCORE, and score are the same variable.
Cannot be a keyword (PRINT, NAME, LEN...).
6.2 Type suffixes
A suffix character on the name fixes the type. a%, a&, and a$ are three different variables.
Range / precision -32,768 to 32,767 -2,147,483,648 to 2,147,483,647 _INTEGER64 (QB64) about ±9.22 × 10^18 SINGLE (the default) about 7 significant digits about 15–16 significant digits _FLOAT (QB64) extended-precision floating point up to very long strings (QBasic: 32,767 characters) _BYTE (QB64)
` ` | _BIT (QB64) | 1 bit | -1 or 0 _OFFSET (QB64) _UNSIGNED` (QB64)
~%% 0–255, ~% 0–65,535, ~& 0–4,294,967,295, ~&& 64-bit unsigned
Numeric literals may also carry suffixes: 1.5# is a DOUBLE literal, &HFF is hexadecimal, &O17 octal, &B1010 binary (QB64).
count% = 42
population& = 8100000
bigNumber&& = 9000000000000000000
price! = 19.99
pi# = 3.141592653589793#
greeting$ = "Hello"
flags~%% = 255
PRINT count%, population&, price!, pi#, greeting$6.3 Declaring with DIM ... AS
Explicit declarations are clearer than suffixes:
Fresh numeric variables start at 0; strings start as "" (empty). Fixed-length strings are padded with spaces.
DIM count AS INTEGER
DIM population AS LONG
DIM bigNumber AS _INTEGER64
DIM price AS SINGLE
DIM total AS DOUBLE
DIM playerName AS STRING
DIM code AS STRING * 4 ' fixed-length string: always 4 characters
DIM pixel AS _UNSIGNED LONG ' 32-bit color values
DIM b AS _UNSIGNED _BYTE
DIM x AS INTEGER, y AS INTEGER ' several in one statement (each needs its own AS)
DIM AS DOUBLE vx, vy ' QB64 alternative form: one type for the whole list6.4 Default types and DEFtype
An undeclared variable without a suffix is SINGLE. Change the default by first letter:
DEFINT A-Z ' all undeclared variables are INTEGER (common in QuickBASIC code)
DEFLNG I-N ' variables starting with I..N are LONG
DEFDBL X-Z
DEFSTR S ' variables starting with S are strings
_DEFINE A-Z AS LONG ' QB64: works with any type, including _INTEGER64 and _UNSIGNED6.5 Requiring declarations: OPTION _EXPLICIT
Typos create new variables silently (scroe = scroe + 1). In QB64-PE, put this at the top of your program:
Use OPTION _EXPLICIT in every non-trivial program.
OPTION _EXPLICIT
DIM score AS LONG
score = score + 10
scroe = 5 ' compile error: variable not defined6.6 Type conversion
rounds (banker's rounding on.5: CINT(2.5) = 2, CINT(3.5) = 4) whole number (floor) INT(-2.5) = -3 whole number (truncate) FIX(-2.5) = -2 nearest integer (QB64) number from string VAL("3.5kg") = 3.5, VAL("abc") = 0 string from number leading space for non-negative numbers string without spaces (QB64)
Assigning a value outside a type's range causes an Overflow error (or wraps silently in some compiled cases) — choose types with room to spare.
Classic BASIC: only %, !, #, $ (Microsoft BASIC); many 8-bit BASICs had only floating point and strings. Commodore BASIC recognized % for integers but did arithmetic in floating point anyway. FreeBASIC: Dim x As Integer is 32 or 64 bits (pointer-sized), not 16; use Short for 16-bit, Long for 32-bit, LongInt for 64-bit, UByte, UShort, ULong, ULongInt for unsigned. In -lang fb, all variables must be declared and suffixes on variable names are not allowed. VB.NET: Integer is 32-bit, Long 64-bit, Short 16-bit, Decimal for money, Boolean, Char, Date, Object. Option Strict On and Option Explicit On are recommended. Type inference: Dim count = 5 (with Option Infer On).