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:
| |
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:
| |
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:
| |
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:
| |
Running with Docker
| |
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
DIMdeclares 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. CONSTcreates 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, whileCINT()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
DIMand cast functions leads to more predictable programs.
Running Today
All examples can be run using Docker:
docker pull primeimages/freebasic
Comments
Loading comments...
Leave a Comment