Modula-2 data types
last modified November 26, 2026 Modula-2
Every variable and constant in Modula-2 has a type, and the compiler checks
that values of different types are not mixed by accident. This article
covers the basic types: whole numbers, real numbers, booleans, characters and
bit sets. It shows the range of each type with MIN and
MAX and its size in bytes with SIZE and
TSIZE.
The basic types
PIM Modula-2 defines the following basic types. GNU Modula-2 adds short and long variants of some of them.
INTEGER— signed whole numbers;CARDINAL— unsigned whole numbers, zero and up;LONGINTandLONGCARD— wider whole numbers;REALandLONGREAL— floating-point numbers;BOOLEAN— the valuesTRUEandFALSE;CHAR— single characters;BITSET— a set of small whole numbers, stored as the bits of a word.
Arrays, records, enumerations, subranges, sets and pointers are built from these types; each has an article of its own.
Integers
INTEGER holds signed whole numbers. The standard functions
MIN and MAX take a type and return its smallest
and largest value.
MODULE integers;
FROM InOut IMPORT WriteString, WriteLn, WriteInt;
VAR
temperature: INTEGER;
BEGIN
temperature := -18;
WriteString("Temperature: ");
WriteInt(temperature, 0);
WriteLn;
WriteString("MIN(INTEGER): ");
WriteInt(MIN(INTEGER), 0);
WriteLn;
WriteString("MAX(INTEGER): ");
WriteInt(MAX(INTEGER), 0);
WriteLn
END integers.
On GNU Modula-2 for x86-64 an INTEGER is 32 bits wide, so its
range is from -2147483648 to 2147483647. The language does not fix the size;
compilers for 16-bit machines used 16 bits.
$ gm2 integers.mod -o integers $ ./integers Temperature: -18 MIN(INTEGER): -2147483648 MAX(INTEGER): 2147483647
Cardinals
CARDINAL holds whole numbers from zero up. It uses the same
number of bits as INTEGER but spends none of them on a sign, so
its maximum is twice as large. Cardinals suit counts, sizes and indexes,
which are never negative.
MODULE cardinals;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
pages, words: CARDINAL;
BEGIN
pages := 320;
words := pages * 350;
WriteString("Pages: ");
WriteCard(pages, 0);
WriteLn;
WriteString("Words: ");
WriteCard(words, 0);
WriteLn;
WriteString("MIN(CARDINAL): ");
WriteCard(MIN(CARDINAL), 0);
WriteLn;
WriteString("MAX(CARDINAL): ");
WriteCard(MAX(CARDINAL), 0);
WriteLn
END cardinals.
INTEGER and CARDINAL are distinct types, and an
expression may not mix variables of the two. The
operators article shows the error that
results, and the type conversions
article shows how to convert between them.
$ gm2 cardinals.mod -o cardinals $ ./cardinals Pages: 320 Words: 112000 MIN(CARDINAL): 0 MAX(CARDINAL): 4294967295
Long integers
LONGINT and LONGCARD are 64 bits wide in GNU
Modula-2. InOut has no procedures for them, so the program
imports WriteLongInt from the Logitech LongIO module
and WriteLongCardinal from CardinalIO.
MODULE longs;
FROM InOut IMPORT WriteString, WriteLn;
FROM LongIO IMPORT WriteLongInt;
FROM CardinalIO IMPORT WriteLongCardinal;
VAR
distance: LONGINT;
bytes: LONGCARD;
BEGIN
distance := 149597870700;
WriteString("Earth to Sun in metres: ");
WriteLongInt(distance, 0);
WriteLn;
bytes := 1024 * 1024 * 1024;
bytes := bytes * 1024 * 16;
WriteString("Bytes in 16 TiB: ");
WriteLongCardinal(bytes, 0);
WriteLn;
WriteString("MAX(LONGINT): ");
WriteLongInt(MAX(LONGINT), 0);
WriteLn;
WriteString("MAX(LONGCARD): ");
WriteLongCardinal(MAX(LONGCARD), 0);
WriteLn
END longs.
The distance does not fit in an INTEGER. The second
multiplication is done in LONGCARD arithmetic because
bytes already has that type.
$ gm2 longs.mod -o longs $ ./longs Earth to Sun in metres: 149597870700 Bytes in 16 TiB: 17592186044416 MAX(LONGINT): 9223372036854775807 MAX(LONGCARD): 18446744073709551615
Real numbers
REAL and LONGREAL hold floating-point numbers.
GNU Modula-2 also has SHORTREAL. RealInOut prints
six decimal places by default; SetNoOfDecimalPlaces changes the
number, which shows how precise each type is.
MODULE reals;
FROM InOut IMPORT WriteString, WriteLn;
FROM RealInOut IMPORT WriteReal, WriteLongReal, WriteShortReal,
SetNoOfDecimalPlaces;
VAR
s: SHORTREAL;
r: REAL;
l: LONGREAL;
BEGIN
s := 1.0 / 3.0;
r := 1.0 / 3.0;
l := 1.0 / 3.0;
SetNoOfDecimalPlaces(20);
WriteString("SHORTREAL: ");
WriteShortReal(s, 0);
WriteLn;
WriteString("REAL: ");
WriteReal(r, 0);
WriteLn;
WriteString("LONGREAL: ");
WriteLongReal(l, 0);
WriteLn
END reals.
Each variable holds the nearest value to one third that its type can
represent. A SHORTREAL is correct to about 7 digits, a
REAL to about 16, and a LONGREAL to about 19. In GNU
Modula-2 on x86-64, REAL is the C double and
LONGREAL the C long double.
$ gm2 reals.mod -o reals $ ./reals SHORTREAL: 0.33333334326744079590 REAL: 0.33333333333333331483 LONGREAL: 0.33333333333333333334
Booleans
BOOLEAN has two values, TRUE and
FALSE. Comparisons produce booleans, and AND,
OR and NOT combine them. InOut has no
procedure that prints a boolean, so the program prints its ordinal number:
0 for FALSE and 1 for TRUE.
MODULE booleans;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
age: INTEGER;
adult, senior, working: BOOLEAN;
BEGIN
age := 42;
adult := age >= 18;
senior := age >= 65;
working := adult AND NOT senior;
WriteString("adult: "); WriteCard(ORD(adult), 0); WriteLn;
WriteString("senior: "); WriteCard(ORD(senior), 0); WriteLn;
WriteString("working: "); WriteCard(ORD(working), 0); WriteLn;
WriteString("MIN(BOOLEAN): "); WriteCard(ORD(MIN(BOOLEAN)), 0); WriteLn;
WriteString("MAX(BOOLEAN): "); WriteCard(ORD(MAX(BOOLEAN)), 0); WriteLn
END booleans.
ORD returns the position of a value in its type.
BOOLEAN behaves as an enumeration whose first value is
FALSE, so MIN(BOOLEAN) is FALSE. The
conditionals article uses booleans to
choose between statements.
$ gm2 booleans.mod -o booleans $ ./booleans adult: 1 senior: 0 working: 1 MIN(BOOLEAN): 0 MAX(BOOLEAN): 1
Characters
CHAR holds one character. ORD returns its code and
CHR turns a code back into a character. In GNU Modula-2 a
character is one byte, so the codes run from 0 to 255.
MODULE characters;
FROM InOut IMPORT WriteString, WriteLn, WriteCard, Write;
VAR
letter: CHAR;
BEGIN
letter := 'M';
WriteString("Letter: ");
Write(letter);
WriteLn;
WriteString("Code: ");
WriteCard(ORD(letter), 0);
WriteLn;
WriteString("Next letter: ");
Write(CHR(ORD(letter) + 1));
WriteLn;
WriteString("Codes: ");
WriteCard(ORD(MIN(CHAR)), 0);
WriteString(" to ");
WriteCard(ORD(MAX(CHAR)), 0);
WriteLn
END characters.
The expression CHR(ORD(letter) + 1) computes the character
after M. Characters cannot be used in arithmetic directly; they
are converted to numbers and back. The
character handling article goes
further.
$ gm2 characters.mod -o characters $ ./characters Letter: M Code: 77 Next letter: N Codes: 0 to 255
Bit sets
BITSET is a set of small cardinals. Each possible member is one
bit of a machine word, so a BITSET in GNU Modula-2 holds the
numbers 0 to 31. A set value is written in braces, and IN tests
whether a number is a member.
MODULE bitsets;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
VAR
weekend, workdays: BITSET;
BEGIN
weekend := {5, 6};
workdays := {0..4};
WriteString("6 IN weekend: ");
WriteCard(ORD(6 IN weekend), 0);
WriteLn;
WriteString("6 IN workdays: ");
WriteCard(ORD(6 IN workdays), 0);
WriteLn;
WriteString("2 IN workdays: ");
WriteCard(ORD(2 IN workdays), 0);
WriteLn;
WriteString("Largest member: ");
WriteCard(MAX(BITSET), 0);
WriteLn
END bitsets.
{0..4} is a range of members. MAX(BITSET) returns
the largest number a bit set can hold, not a set. The
sets article covers set operations and sets of
other types.
$ gm2 bitsets.mod -o bitsets $ ./bitsets 6 IN weekend: 1 6 IN workdays: 0 2 IN workdays: 1 Largest member: 31
Sizes of types
SIZE returns the number of bytes a variable or type occupies.
TSIZE takes only a type and must be imported from the
SYSTEM module.
MODULE sizes;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
FROM SYSTEM IMPORT TSIZE;
VAR
count: INTEGER;
ratio: LONGREAL;
BEGIN
WriteString("INTEGER "); WriteCard(SIZE(INTEGER), 2); WriteLn;
WriteString("CARDINAL "); WriteCard(SIZE(CARDINAL), 2); WriteLn;
WriteString("LONGINT "); WriteCard(SIZE(LONGINT), 2); WriteLn;
WriteString("SHORTREAL "); WriteCard(TSIZE(SHORTREAL), 2); WriteLn;
WriteString("REAL "); WriteCard(TSIZE(REAL), 2); WriteLn;
WriteString("LONGREAL "); WriteCard(TSIZE(LONGREAL), 2); WriteLn;
WriteString("BOOLEAN "); WriteCard(TSIZE(BOOLEAN), 2); WriteLn;
WriteString("CHAR "); WriteCard(TSIZE(CHAR), 2); WriteLn;
WriteString("BITSET "); WriteCard(TSIZE(BITSET), 2); WriteLn;
WriteString("count "); WriteCard(SIZE(count), 2); WriteLn;
WriteString("ratio "); WriteCard(SIZE(ratio), 2); WriteLn
END sizes.
TSIZE dates from the early editions of PIM, in which
SIZE took only a variable; GNU Modula-2 accepts a type in
SIZE as well.
A LONGREAL occupies 16 bytes, although only 10 of them hold the
x87 extended-precision number; the rest is padding for alignment.
$ gm2 sizes.mod -o sizes $ ./sizes INTEGER 4 CARDINAL 4 LONGINT 8 SHORTREAL 4 REAL 8 LONGREAL 16 BOOLEAN 1 CHAR 1 BITSET 4 count 4 ratio 16
Next steps
The constants and literals article shows
how values of these types are written, including octal, hexadecimal and
character codes. The type
conversions article explains ORD, CHR,
VAL and the other conversions in detail.
Source
Modula-2 language reference at modula2.org
In this article we have covered the basic types of Modula-2: integers, cardinals, long integers, reals, booleans, characters and bit sets, their ranges and their sizes. The basics article shows them in simple programs, and the program structure article shows where their declarations go.
Author
List all Modula-2 tutorials.