Variables and Types in Delphi
Learn about variables, data types, constants, and type conversions in Delphi with practical Docker-ready examples
Variables and types are the foundation of any Delphi program. Delphi inherits Pascal’s tradition of explicit, strongly-typed variable declarations — every variable must be declared with a specific type before it can be used. The compiler enforces these types strictly, catching many errors before your code ever runs.
As a statically and strongly typed language, Delphi requires you to think about types upfront. This may feel verbose compared to dynamically typed languages, but it pays off with faster executables, better tooling support, and fewer runtime surprises. Delphi’s type system also includes features like enumerated types, subrange types, and sets that give you fine-grained control over your data.
In this tutorial, you’ll learn how to declare variables, work with Delphi’s built-in data types, define constants, and perform type conversions — all compiled and run using Free Pascal in Delphi mode.
Variable Declarations
In Delphi, all variables must be declared in a var section before they can be used. You cannot introduce new variables in the middle of a begin...end block (unlike C or Python).
Create a file named variables.dpr:
program Variables;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
// Integer types
Age: Integer;
Count: LongInt;
SmallNum: ShortInt;
BigNum: Int64;
// Floating-point types
Price: Double;
Temperature: Single;
// String and character types
Name: string;
Initial: Char;
// Boolean type
IsActive: Boolean;
begin
// Integer assignments
Age := 30;
Count := 1000000;
SmallNum := -42;
BigNum := 9223372036854775807;
// Floating-point assignments
Price := 19.99;
Temperature := -3.5;
// String and character assignments
Name := 'Delphi';
Initial := 'D';
// Boolean assignment
IsActive := True;
// Display all variables
WriteLn('=== Integer Types ===');
WriteLn('Age (Integer): ', Age);
WriteLn('Count (LongInt): ', Count);
WriteLn('SmallNum (ShortInt): ', SmallNum);
WriteLn('BigNum (Int64): ', BigNum);
WriteLn;
WriteLn('=== Floating-Point Types ===');
WriteLn('Price (Double): ', Price:0:2);
WriteLn('Temperature (Single): ', Temperature:0:1);
WriteLn;
WriteLn('=== String and Character Types ===');
WriteLn('Name (string): ', Name);
WriteLn('Initial (Char): ', Initial);
WriteLn;
WriteLn('=== Boolean Type ===');
WriteLn('IsActive (Boolean): ', IsActive);
end.
Integer Type Sizes
Delphi provides integer types of various sizes, giving you control over memory usage and value ranges:
| Type | Size | Range |
|---|---|---|
ShortInt | 1 byte | -128 to 127 |
Byte | 1 byte | 0 to 255 |
SmallInt | 2 bytes | -32,768 to 32,767 |
Word | 2 bytes | 0 to 65,535 |
Integer | 4 bytes | -2,147,483,648 to 2,147,483,647 |
LongInt | 4 bytes | Same as Integer |
Cardinal | 4 bytes | 0 to 4,294,967,295 |
Int64 | 8 bytes | -9.2×10¹⁸ to 9.2×10¹⁸ |
Constants and Enumerated Types
Delphi supports true constants (evaluated at compile time) and typed constants. It also has enumerated types and sets — features inherited from Pascal that many modern languages lack.
Create a file named variables_types.dpr:
program VariablesTypes;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
Pi = 3.14159265358979;
MaxRetries = 5;
AppName = 'CodeArchaeology';
Newline = #13#10;
type
TColor = (Red, Green, Blue, Yellow, Cyan);
TColorSet = set of TColor;
TMonth = 1..12;
var
FavoriteColor: TColor;
PrimaryColors: TColorSet;
CurrentMonth: TMonth;
Area: Double;
Radius: Double;
begin
WriteLn('=== Constants ===');
WriteLn('Pi: ', Pi:0:15);
WriteLn('MaxRetries: ', MaxRetries);
WriteLn('AppName: ', AppName);
// Enumerated types
FavoriteColor := Blue;
WriteLn;
WriteLn('=== Enumerated Types ===');
WriteLn('FavoriteColor ordinal: ', Ord(FavoriteColor));
WriteLn('FavoriteColor name: Blue');
// Set types
PrimaryColors := [Red, Green, Blue];
WriteLn;
WriteLn('=== Set Types ===');
if Red in PrimaryColors then
WriteLn('Red is a primary color');
if Yellow in PrimaryColors then
WriteLn('Yellow is a primary color')
else
WriteLn('Yellow is NOT a primary color');
// Subrange types
CurrentMonth := 7;
WriteLn;
WriteLn('=== Subrange Types ===');
WriteLn('Current month: ', CurrentMonth);
// Using constants in calculations
Radius := 5.0;
Area := Pi * Radius * Radius;
WriteLn;
WriteLn('=== Using Constants ===');
WriteLn('Circle radius: ', Radius:0:1);
WriteLn('Circle area: ', Area:0:4);
end.
Type Conversions
Delphi is strict about type compatibility, but provides built-in functions and explicit casting for converting between types. Understanding these conversions is essential for working with mixed data.
Create a file named variables_convert.dpr:
program VariablesConvert;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
IntVal: Integer;
FloatVal: Double;
StrVal: string;
BoolVal: Boolean;
CharVal: Char;
ByteVal: Byte;
begin
WriteLn('=== String to Number Conversions ===');
// String to Integer
StrVal := '42';
IntVal := StrToInt(StrVal);
WriteLn('StrToInt(''42''): ', IntVal);
// String to Float
StrVal := '3.14';
FloatVal := StrToFloat(StrVal);
WriteLn('StrToFloat(''3.14''): ', FloatVal:0:2);
WriteLn;
WriteLn('=== Number to String Conversions ===');
// Integer to String
IntVal := 255;
StrVal := IntToStr(IntVal);
WriteLn('IntToStr(255): ', StrVal);
// Float to String
FloatVal := 98.6;
StrVal := FloatToStr(FloatVal);
WriteLn('FloatToStr(98.6): ', StrVal);
// Formatted float to string
StrVal := Format('%.3f', [FloatVal]);
WriteLn('Format(''%.3f'', 98.6): ', StrVal);
WriteLn;
WriteLn('=== Numeric Type Conversions ===');
// Integer to Float (implicit widening is allowed)
IntVal := 100;
FloatVal := IntVal;
WriteLn('Integer 100 as Double: ', FloatVal:0:1);
// Float to Integer (requires Trunc or Round)
FloatVal := 7.8;
WriteLn('Trunc(7.8): ', Trunc(FloatVal));
WriteLn('Round(7.8): ', Round(FloatVal));
WriteLn;
WriteLn('=== Character and Ordinal Conversions ===');
// Char to ordinal value
CharVal := 'A';
IntVal := Ord(CharVal);
WriteLn('Ord(''A''): ', IntVal);
// Ordinal to Char
IntVal := 90;
CharVal := Chr(IntVal);
WriteLn('Chr(90): ', CharVal);
// Integer to Byte (explicit cast)
IntVal := 200;
ByteVal := Byte(IntVal);
WriteLn('Byte(200): ', ByteVal);
WriteLn;
WriteLn('=== Boolean Conversions ===');
BoolVal := True;
WriteLn('Ord(True): ', Ord(BoolVal));
WriteLn('Ord(False): ', Ord(False));
// Integer to Boolean
BoolVal := Boolean(1);
WriteLn('Boolean(1): ', BoolVal);
BoolVal := Boolean(0);
WriteLn('Boolean(0): ', BoolVal);
end.
Running with Docker
| |
Expected Output
Output from variables.dpr:
=== Integer Types ===
Age (Integer): 30
Count (LongInt): 1000000
SmallNum (ShortInt): -42
BigNum (Int64): 9223372036854775807
=== Floating-Point Types ===
Price (Double): 19.99
Temperature (Single): -3.5
=== String and Character Types ===
Name (string): Delphi
Initial (Char): D
=== Boolean Type ===
IsActive (Boolean): TRUE
Output from variables_types.dpr:
=== Constants ===
Pi: 3.141592653589790
MaxRetries: 5
AppName: CodeArchaeology
=== Enumerated Types ===
FavoriteColor ordinal: 2
FavoriteColor name: Blue
=== Set Types ===
Red is a primary color
Yellow is NOT a primary color
=== Subrange Types ===
Current month: 7
=== Using Constants ===
Circle radius: 5.0
Circle area: 78.5398
Output from variables_convert.dpr:
=== String to Number Conversions ===
StrToInt('42'): 42
StrToFloat('3.14'): 3.14
=== Number to String Conversions ===
IntToStr(255): 255
FloatToStr(98.6): 98.6
Format('%.3f', 98.6): 98.600
=== Numeric Type Conversions ===
Integer 100 as Double: 100.0
Trunc(7.8): 7
Round(7.8): 8
=== Character and Ordinal Conversions ===
Ord('A'): 65
Chr(90): Z
Byte(200): 200
=== Boolean Conversions ===
Ord(True): 1
Ord(False): 0
Boolean(1): TRUE
Boolean(0): FALSE
Key Concepts
- All variables must be declared in a
varsection before use — Delphi does not allow inline declarations within code blocks - Static, strong typing means the compiler catches type mismatches at compile time, not at runtime
- Integer types come in multiple sizes (ShortInt through Int64), giving you precise control over memory and range
- Enumerated types and sets are first-class features that let you define meaningful, constrained value domains
- Subrange types (e.g.,
1..12) restrict values to a specific range, adding an extra layer of validation - Type conversions require explicit functions like
StrToInt,IntToStr,Trunc, andRound— Delphi rarely performs implicit narrowing conversions - Constants declared with
constare evaluated at compile time and cannot be reassigned - The
:=operator is used for assignment (not=, which is the equality comparison operator)
Running Today
All examples can be run using Docker:
docker pull freepascal/fpc:3.2.2-slim
Comments
Loading comments...
Leave a Comment