ZetCode

Pascal variables and declarations

last modified November 18, 2026 Free Pascal 3.2.2

A variable is a named storage location with a type. This article shows how variables are declared and initialised in Pascal, how identifiers are written, how constants differ from variables, how scope decides which declaration a name refers to, and why the {$H+} directive changes the size of a string.

Declaring variables

A variable is declared in a var section with a name and a type. Several names of the same type can share one declaration, and a declaration may include an initial value. The type comes after a colon, and the declaration ends with a semicolon.

declarations.pas
program declarations;
{$mode objfpc}{$H+}

var
  age: Integer;
  height, weight: Double;
  isStudent: Boolean;
  name: string;
  x: Integer = 10;
  y: Integer = 20;

begin
  age := 28;
  height := 1.82;
  weight := 74.5;
  isStudent := True;
  name := 'Alice';
  WriteLn('Name:    ', name);
  WriteLn('Age:     ', age);
  WriteLn('Height:  ', height:0:2);
  WriteLn('Weight:  ', weight:0:1);
  WriteLn('Student: ', isStudent);
  WriteLn('x + y =  ', x + y);
end.

The program declares an integer, two doubles, a Boolean, a string, and two integers with initial values. The assignments use :=, and a Boolean value is written True or False. The 0:2 after height means "use the default width and show two decimal places". A string is printed without padding, unlike a Fortran character variable, because an AnsiString stores its current length.

$ fpc -gl declarations.pas
$ ./declarations
Name:    Alice
Age:     28
Height:  1.82
Weight:  74.5
Student: TRUE
x + y =  30

Identifiers

An identifier starts with a letter or an underscore and continues with letters, digits, or underscores. Pascal identifiers are case-insensitive, so TotalCount, totalcount, and TOTALCOUNT are the same name. The compiler preserves the spelling in error messages, but it does not distinguish the names. Free Pascal accepts identifiers up to 127 characters long; the ISO standard guarantees only a much shorter length.

A reserved word such as begin, end, or record cannot be used as an identifier. The same is true of the names of the types and functions that the System unit declares. The usual Pascal style is CamelCase for types and mixed case for variables and procedures, as in TStudent and studentCount.

Initial values and zeroing

A global variable that is declared without an initial value is zeroed when the program starts. For an integer that means zero, for a Boolean False, for a string an empty string, and for a pointer nil. A local variable inside a procedure is different: it is not zeroed, and its value is whatever the stack happens to contain.

globalzero.pas
program globalzero;
{$mode objfpc}{$H+}

var
  g: Integer;

begin
  WriteLn('global g = ', g);
end.

The variable g is declared at the top level and never assigned, so the run-time library has already set it to zero before the main block runs. A local variable would not be safe to read without an assignment, and a compiler warning is not guaranteed. The safest habit is to assign every local variable before using it.

$ fpc -gl globalzero.pas
$ ./globalzero
global g = 0

Assignment and swapping

The assignment operator copies the value of the right-hand side into the variable on the left. It is not a comparison; the comparison operator is =. Exchanging two values needs a third variable, because the language has no multiple assignment.

swap.pas
program swap;
{$mode objfpc}{$H+}

var
  a, b, tmp: Integer;

begin
  a := 3;
  b := 8;
  tmp := a;
  a := b;
  b := tmp;
  WriteLn('a = ', a, ', b = ', b);
end.

The program saves a in tmp, overwrites a with b, and copies the saved value into b. After the three assignments the variables have exchanged their values. A procedure can do the same with var parameters, which the procedures article shows.

$ fpc -gl swap.pas
$ ./swap
a = 8, b = 3

Constants

A const section defines names whose values cannot change. A constant without a type, such as Pi = 3.14159265, is replaced by its value wherever it is used and takes the type that fits the context. A typed constant, such as Limit: Integer = 100, has a fixed type and occupies storage. In Free Pascal a typed constant is writable by default, which is a legacy behaviour inherited from Turbo Pascal.

constants.pas
program constants;
{$mode objfpc}{$H+}

const
  Pi = 3.14159265;
  Limit: Integer = 100;

begin
  WriteLn('Pi = ', Pi:0:8);
  WriteLn('Limit = ', Limit);
  Limit := Limit + 1;
  WriteLn('Limit = ', Limit);
end.

The program prints Pi with eight decimals and then changes Limit. The assignment compiles because Limit is a typed constant, not an untyped one. This is rarely what a programmer wants: use an untyped constant for a value that must never change, and a variable when the value is meant to be modified.

$ fpc -gl constants.pas
$ ./constants
Pi = 3.14159265
Limit = 100
Limit = 101

Read-only typed constants

The compiler directive {$J-} disables the writable-typed-constant behaviour. With the directive active, an assignment to a typed constant is a compile-time error. The program below is the previous example with {$J-} added; it does not produce an executable.

readconst.pas
program readconst;
{$mode objfpc}{$H+}
{$J-}

const
  Limit: Integer = 100;

begin
  WriteLn(Limit);
  Limit := 101;
  WriteLn(Limit);
end.

The compiler reports the assignment on line 8 and stops. The exact line number depends on the file, but the message is always the same: a value cannot be assigned to a constant variable. Turning the directive back on with {$J+} restores the old behaviour.

$ fpc -gl readconst.pas
readconst.pas(8,3) Error: Can't assign values to const variable
readconst.pas(11) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted

Scope

The scope of a declaration is the part of the program where its name is visible. A variable declared at the top level is global: every procedure after the declaration can use it, and so can the main block. A variable declared inside a procedure is local: it exists only while the procedure runs, and it hides a global variable with the same name.

scope.pas
program scope;
{$mode objfpc}{$H+}

var
  value: Integer = 10;

procedure Show;
var
  value: Integer;
begin
  value := 20;
  WriteLn('inside:  ', value);
end;

begin
  WriteLn('outside: ', value);
  Show;
  WriteLn('outside: ', value);
end.

The procedure declares its own value, so the assignment inside Show changes the local variable, not the global one. The two outside lines print 10, and only the inside line prints 20. Without the local declaration, the procedure would modify the global variable.

$ fpc -gl scope.pas
$ ./scope
outside: 10
inside:  20
outside: 10

Absolute variables

The absolute clause places a variable at the same address as another variable. It is an advanced feature that reinterprets the bytes of one variable through another type. The next program stores a 32-bit integer and reads its first byte through a Byte variable.

absolute.pas
program absolute;
{$mode objfpc}{$H+}

uses SysUtils;

var
  number: Integer;
  firstByte: Byte absolute number;

begin
  number := $12345678;
  WriteLn('number = $', IntToHex(number, 8));
  WriteLn('first byte = $', IntToHex(firstByte, 2));
end.

On a little-endian x86-64 machine the least significant byte comes first, so the first byte of $12345678 is $78. On a big-endian machine the same program would print $12. The clause does not allocate new storage and does not convert values; it only gives another name to the same memory, so it should be used with care.

$ fpc -gl absolute.pas
$ ./absolute
number = $12345678
first byte = $78

Why H+ matters

Without {$H+}, ObjFPC mode reads string as ShortString: a fixed 256-byte record with a length byte and at most 255 characters. With the directive, string becomes AnsiString: an eight-byte pointer to heap data that stores the length, the code page, and a reference count. The difference is easy to see with SizeOf.

sizes.pas
program sizes;
{$mode objfpc}{$H+}

var
  a: AnsiString;
  s: ShortString;
  u: UnicodeString;

begin
  WriteLn('AnsiString:    ', SizeOf(a));
  WriteLn('ShortString:   ', SizeOf(s));
  WriteLn('UnicodeString: ', SizeOf(u));
end.

AnsiString and UnicodeString are both eight-byte references on this machine; the characters themselves live on the heap. ShortString is stored inline, which is why its size is 256 bytes whatever the current text is. The data types article compares the three string types and explains their encodings.

$ fpc -gl sizes.pas
$ ./sizes
AnsiString:    8
ShortString:   256
UnicodeString: 8

Next steps

The data types article lists the integer and real types with their sizes and ranges, the Boolean and character types, and the string types. The literals and constants article shows how values are written, and the operators article starts using variables in expressions.

Source

Free Pascal Reference guide

Free Pascal: variables

Pascal on Wikipedia

In this article we have declared and initialised Pascal variables, looked at identifiers, constants and typed constants, scope, absolute variables, and the difference between short and ANSI strings. The series continues with Pascal data types.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.