ZetCode

Pascal data types

last modified November 18, 2026 Free Pascal 3.2.2

Every Pascal variable, constant, parameter, and function result has a type. The type decides which values the variable can hold, how much memory it occupies, and which operations are allowed. This article describes the integer and real types, Boolean and character values, the string types, type aliases, subranges, and the conversions between them.

Integer types

Pascal has several integer types. They differ in size and signedness, and the size is fixed by the compiler, not by the machine's native word size. On the x86-64 target used in this series, Integer is 32 bits and Int64 is 64 bits. The signed types can hold negative values; the unsigned types start at zero and reach twice as far for the same number of bits.

TypeBytesRange
Byte10 .. 255
ShortInt1-128 .. 127
Word20 .. 65535
SmallInt2-32768 .. 32767
Cardinal40 .. 4294967295
Integer4-2147483648 .. 2147483647
Int648-9223372036854775808 .. 9223372036854775807
QWord80 .. 18446744073709551615

The functions Low and High return the smallest and largest value of a type, and SizeOf returns its size in bytes. The next program prints all three values for the common integer types. It does not need any unit beyond the System unit.

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

begin
  WriteLn('Byte     ', SizeOf(Byte):2, '  ', Low(Byte), ' .. ', High(Byte));
  WriteLn('ShortInt ', SizeOf(ShortInt):2, '  ', Low(ShortInt), ' .. ', High(ShortInt));
  WriteLn('Word     ', SizeOf(Word):2, '  ', Low(Word), ' .. ', High(Word));
  WriteLn('SmallInt ', SizeOf(SmallInt):2, '  ', Low(SmallInt), ' .. ', High(SmallInt));
  WriteLn('Cardinal ', SizeOf(Cardinal):2, '  ', Low(Cardinal), ' .. ', High(Cardinal));
  WriteLn('Integer  ', SizeOf(Integer):2, '  ', Low(Integer), ' .. ', High(Integer));
  WriteLn('Int64    ', SizeOf(Int64):2, '  ', Low(Int64), ' .. ', High(Int64));
  WriteLn('QWord    ', SizeOf(QWord):2, '  ', Low(QWord), ' .. ', High(QWord));
end.

The :2 after SizeOf right-aligns the number in two characters, which keeps the columns straight. The ranges confirm the table: a Byte stops at 255, an Integer at about two thousand million, and an Int64 at about nine quintillion. The unsigned QWord reaches twice as far as Int64 because it has no sign bit.

$ fpc -gl inttypes.pas
$ ./inttypes
Byte      1  0 .. 255
ShortInt  1  -128 .. 127
Word      2  0 .. 65535
SmallInt  2  -32768 .. 32767
Cardinal  4  0 .. 4294967295
Integer   4  -2147483648 .. 2147483647
Int64     8  -9223372036854775808 .. 9223372036854775807
QWord     8  0 .. 18446744073709551615

Real types

Pascal has three floating-point types. Single is a 32-bit IEEE single-precision number, Double is 64-bit double precision, and Extended is the widest type the platform supports. On x86-64 Linux, Extended occupies ten bytes and carries about nineteen significant decimal digits; on some other processors it is stored as a Double.

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

var
  s: Single;
  d: Double;
  e: Extended;

begin
  s := 1.2345678;
  d := 1.2345678;
  e := 1.2345678;
  WriteLn('Single:   ', SizeOf(s), ' bytes  ', s);
  WriteLn('Double:   ', SizeOf(d), ' bytes  ', d);
  WriteLn('Extended: ', SizeOf(e), ' bytes  ', e);
  WriteLn('Single:   ', s:0:7);
  WriteLn('Double:   ', d:0:7);
end.

The default output uses scientific notation and prints enough digits to show the exact stored value. Single cannot represent 1.2345678 exactly, so the printed value ends in 761; Double is much closer. With :0:7 both values are rounded to seven decimal places and look the same, which is why formatting hides small differences rather than proving that two values are equal.

$ fpc -gl floattypes.pas
$ ./floattypes
Single:   4 bytes   1.234567761E+00
Double:   8 bytes   1.2345678000000000E+000
Extended: 10 bytes   1.23456780000000000002E+0000
Single:   1.2345678
Double:   1.2345678

Boolean and character types

Boolean has two values, False and True, stored as 0 and 1. The language also has ByteBool, WordBool, and LongBool for compatibility with other APIs, but Boolean is the type to use in ordinary code. Char is a single byte and holds one character of the current code page; WideChar is two bytes and holds one UTF-16 code unit.

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

var
  b: Boolean;
  c: Char;

begin
  b := True;
  WriteLn('Boolean size: ', SizeOf(b));
  WriteLn('True:  ', Ord(True));
  WriteLn('False: ', Ord(False));
  c := 'A';
  WriteLn('Char size: ', SizeOf(c));
  WriteLn('Code: ', Ord(c));
  WriteLn('Succ: ', Succ(c));
  WriteLn('Pred: ', Pred(c));
  WriteLn('Digit: ', Ord('7') - Ord('0'));
end.

Ord returns the ordinal value of an enumeration, a character, or a Boolean. Succ and Pred return the next and previous value in that order. For a character, the code is the value from the character set: Ord('A') is 65 in ASCII and in UTF-8, which is why Succ('A') is B and Pred('A') is @. The last line converts a digit character to its numeric value by subtracting the code of '0'.

$ fpc -gl boolchar.pas
$ ./boolchar
Boolean size: 1
True:  1
False: 0
Char size: 1
Code: 65
Succ: B
Pred: @
Digit: 7

String types

Pascal has three string families. AnsiString stores bytes in the current code page, UnicodeString stores UTF-16 code units, and ShortString stores up to 255 characters inline. With {$H+}, the name string means AnsiString. All three types are managed by the compiler: they are reference counted or copied automatically, and they need no manual memory management.

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

uses SysUtils;

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

begin
  a := 'Pascal';
  u := 'Pascal';
  s := 'Pascal';
  text := UTF8Decode('Příliš žluťoučký kůň');
  WriteLn('AnsiString:    ', SizeOf(a), ' bytes, length ', Length(a));
  WriteLn('UnicodeString: ', SizeOf(u), ' bytes, length ', Length(u));
  WriteLn('ShortString:   ', SizeOf(s), ' bytes, length ', Length(s));
  WriteLn('text length (UTF-16): ', Length(text));
end.

The first three variables hold the same ASCII text, so all three lengths are six. The sizes differ: AnsiString and UnicodeString are eight-byte references, while ShortString is a 256-byte record stored in the variable itself. The Czech text has 20 characters and 29 bytes in UTF-8; UTF8Decode converts it to UTF-16, where Length counts 20 code units. Length on an AnsiString counts bytes, not characters.

$ fpc -gl strtypes.pas
$ ./strtypes
AnsiString:    8 bytes, length 6
UnicodeString: 8 bytes, length 6
ShortString:   256 bytes, length 6
text length (UTF-16): 20

Type aliases and subranges

The type section can give a new name to an existing type or define a type with a smaller range. An alias such as TAge = Integer is the same type under another name, and values are freely assignable between the two. A subrange such as TDigit = 0..9 restricts the values but usually occupies the smallest integer size that can hold the range.

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

type
  TAge = Integer;
  TDigit = 0..9;

var
  age: TAge;
  digit: TDigit;
  n: Integer;

begin
  age := 42;
  digit := 7;
  WriteLn('age: ', age, '  size: ', SizeOf(age));
  WriteLn('digit: ', digit, '  size: ', SizeOf(digit));
  n := 12;
  digit := n;
  WriteLn('digit: ', digit);
end.

TDigit occupies one byte, because the values 0 to 9 fit in a byte. The assignment digit := n stores 12 even though 12 is outside the declared range: range checks are off by default, so the program prints 12 and carries on. Compiled with -Cr, the same assignment stops with runtime error 201 at that line. The checking option is described in the introduction.

$ fpc -gl aliases.pas
$ ./aliases
age: 42  size: 4
digit: 7  size: 1
digit: 12

Conversions

Pascal converts between numeric types implicitly when the conversion is safe, and requires an explicit function when it is not. Assigning an integer to a real is automatic; assigning a real to an integer needs Round or Trunc. Strings and numbers are converted with the routines from SysUtils, and a failed conversion raises an exception rather than returning a sentinel value.

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

uses SysUtils;

var
  i: Integer;
  d: Double;

begin
  i := StrToInt('123');
  WriteLn('StrToInt:   ', i);
  WriteLn('IntToStr:   ', IntToStr(456));
  d := StrToFloat('3.75');
  WriteLn('StrToFloat: ', d:0:2);
  WriteLn('FloatToStr: ', FloatToStr(2.5));
  WriteLn('Trunc:      ', Trunc(2.9));
  WriteLn('Round:      ', Round(2.5), ' ', Round(3.5));
  WriteLn('Ord/Chr:    ', Ord('A'), ' ', Chr(66));
end.

StrToInt and StrToFloat parse the whole string and raise EConvertError if it does not contain a valid number. IntToStr and FloatToStr go the other way. Trunc drops the fractional part, while Round rounds to the nearest integer; Pascal uses banker's rounding, so 2.5 becomes 2 and 3.5 becomes 4. Ord and Chr convert between a character and its numeric code.

$ fpc -gl conversions.pas
$ ./conversions
StrToInt:   123
IntToStr:   456
StrToFloat: 3.75
FloatToStr: 2.5
Trunc:      2
Round:      2 4
Ord/Chr:    65 B

Next steps

The literals and constants article shows how values of these types are written in the source, including hexadecimal, binary, and octal integers, character codes, and string escapes. The operators article describes the operations that combine them, and the numeric functions article collects the routines that work on reals.

Source

Free Pascal Reference guide

Free Pascal: types

Pascal on Wikipedia

In this article we have described the Pascal integer and real types, Boolean and character values, the three string types, type aliases, subranges, and conversions. The series continues with Pascal literals and constants.

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.