Beginner

Variables and Types in BASIC

Learn about variables, data types, and type conversions in BASIC with practical Docker-ready examples

BASIC was designed to be approachable, and its variable system reflects that philosophy. In classic BASIC dialects, variables required no declaration — you simply used them. Modern FreeBASIC adds optional explicit typing while preserving BASIC’s readable style.

BASIC uses a type-suffix convention inherited from the earliest dialects: a variable name ending in $ holds a string, while names ending in % or ! denote integers and single-precision floats. In FreeBASIC you can also declare variables explicitly with DIM, giving you full control over types. Both approaches work side by side.

In this tutorial you’ll learn how to declare and use numeric and string variables, understand BASIC’s type suffixes, work with constants, and see how FreeBASIC handles type conversions.

Variable Declarations and Basic Types

FreeBASIC supports both implicit and explicit variable declarations. The DIM statement is the modern way to declare variables with a specific type.

Create a file named variables.bas:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
' Variables and Types in BASIC (FreeBASIC)

' Explicit declarations with DIM
DIM age AS INTEGER
DIM price AS DOUBLE
DIM name AS STRING
DIM isValid AS BOOLEAN

' Assign values
age = 25
price = 9.99
name = "Alice"
isValid = TRUE

PRINT "Integer:  "; age
PRINT "Double:   "; price
PRINT "String:   "; name
PRINT "Boolean:  "; IIF(isValid, "true", "false")

Type Suffixes — The Classic BASIC Style

Before DIM existed, BASIC programmers used name suffixes to signal the type. FreeBASIC still supports this convention, which you will encounter in older code.

Create a file named variables_suffixes.bas:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
' Classic BASIC type suffix convention
' $ suffix = string
' % suffix = integer (short)
' ! suffix = single-precision float
' # suffix = double-precision float

DIM name$   AS STRING
DIM count%  AS INTEGER
DIM ratio!  AS SINGLE
DIM precise# AS DOUBLE

name$   = "Dartmouth"
count%  = 1964
ratio!  = 3.14
precise# = 2.718281828459045

PRINT "Language birthplace: "; name$
PRINT "Year created:        "; count%
PRINT "Single precision:    "; ratio!
PRINT "Double precision:    "; precise#

Constants

BASIC supports named constants via the CONST keyword. Constants make programs easier to read and prevent accidental modification of fixed values.

Create a file named variables_constants.bas:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
' Named constants in FreeBASIC
CONST MAX_SCORE AS INTEGER = 100
CONST PI AS DOUBLE = 3.14159265358979
CONST GREETING AS STRING = "Hello from BASIC!"
CONST VERSION AS SINGLE = 1.0

PRINT "Max score:  "; MAX_SCORE
PRINT "Pi:         "; PI
PRINT "Greeting:   "; GREETING
PRINT "Version:    "; VERSION

' Compute circle area using constant
DIM radius AS DOUBLE
radius = 5.0
PRINT "Circle area:"; PI * radius * radius

Type Conversions

FreeBASIC provides built-in functions for converting between types. Understanding conversions is important when mixing numeric and string data.

Create a file named variables_conversions.bas:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
' Type conversion examples in FreeBASIC
DIM numStr AS STRING
DIM num    AS INTEGER
DIM dbl    AS DOUBLE
DIM result AS STRING

' String to number
numStr = "42"
num = VAL(numStr)
PRINT "String to integer: "; num

' Number to string
dbl = 3.14159
result = STR(dbl)
PRINT "Double to string: ["; result; "]"

' Integer and double interplay
DIM i AS INTEGER
DIM d AS DOUBLE
d = 7.9
i = INT(d)          ' truncates toward negative infinity
PRINT "INT(7.9) = "; i

i = CINT(d)         ' rounds to nearest integer
PRINT "CINT(7.9) = "; i

' Explicit casts
DIM a AS INTEGER
DIM b AS DOUBLE
a = 10
b = CDBL(a) / 3.0
PRINT "10 / 3 as double: "; b

Running with Docker

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Pull the FreeBASIC image
docker pull primeimages/freebasic

# Run the basic variables example
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc variables.bas && ./variables"

# Run the suffix-style example
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc variables_suffixes.bas && ./variables_suffixes"

# Run the constants example
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc variables_constants.bas && ./variables_constants"

# Run the conversions example
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc variables_conversions.bas && ./variables_conversions"

Expected Output

Running variables.bas:

Integer:   25
Double:    9.99
String:    Alice
Boolean:  true

Running variables_suffixes.bas:

Language birthplace:  Dartmouth
Year created:         1964
Single precision:     3.14
Double precision:     2.71828182845905

Running variables_constants.bas:

Max score:   100
Pi:          3.14159265358979
Greeting:    Hello from BASIC!
Version:     1
Circle area: 78.5398163397448

Running variables_conversions.bas:

String to integer:  42
Double to string: [ 3.14159]
INT(7.9) =  7
CINT(7.9) =  8
10 / 3 as double:  3.33333333333333

Key Concepts

  • DIM declares variables explicitly with a type — this is the modern FreeBASIC style and avoids surprises from implicit creation.
  • Type suffixes ($, %, !, #) are the classic BASIC convention still recognized by FreeBASIC; you will see them in vintage and ported code.
  • CONST creates named constants — unchangeable values that improve readability and prevent accidental modification.
  • VAL() converts a string to a number; STR() converts a number to a string — these are the workhorses of BASIC type conversion.
  • INT() truncates toward negative infinity, while CINT() rounds to the nearest integer — an important distinction when processing numeric data.
  • CDBL(), CINT(), CSNG() are explicit cast functions for converting between numeric types cleanly.
  • Strings are zero-allocation in FreeBASIC — you don’t need to manage memory; string variables grow automatically.
  • BASIC’s type system is permissive — the compiler performs many implicit conversions, but being explicit with DIM and cast functions leads to more predictable programs.

Running Today

All examples can be run using Docker:

docker pull primeimages/freebasic
Last updated:

Comments

Loading comments...

Leave a Comment

2000 characters remaining