Modula-2 constants and literals
last modified November 26, 2026 Modula-2
A literal is a value written directly in the source text, such as
42, 3.5 or "text". A constant gives
a literal, or an expression computed from literals, a name. This article
covers the CONST section and every form of literal in
Modula-2: decimal, octal and hexadecimal whole numbers, character codes, real
numbers, and strings.
Constant declarations
Constants are declared in a CONST section. Each declaration
is a name, an equals sign, a value, and a semicolon. The compiler replaces
the name with its value, and an attempt to assign to a constant is an error.
MODULE constants;
FROM InOut IMPORT WriteString, WriteLn, WriteInt;
CONST
Width = 80;
Height = 25;
Offset = -4;
BEGIN
WriteString("Width: "); WriteInt(Width, 0); WriteLn;
WriteString("Height: "); WriteInt(Height, 0); WriteLn;
WriteString("Offset: "); WriteInt(Offset, 0); WriteLn
END constants.
A whole-number literal has no sign of its own; -4 is the
literal 4 with the minus operator applied. A constant has no
declared type, so Width can be passed to a procedure that
expects an INTEGER or one that expects a CARDINAL.
$ gm2 constants.mod -o constants $ ./constants Width: 80 Height: 25 Offset: -4
Octal and hexadecimal
Modula-2 writes the base of a number after its digits. A number ending in
B is octal and a number ending in H is
hexadecimal. A hexadecimal literal must start with a digit, so
0FFH is written with a leading zero.
MODULE bases;
FROM InOut IMPORT WriteString, WriteLn, WriteCard, WriteOct, WriteHex;
CONST
Mode = 755B;
Mask = 0FFH;
Colour = 0C0FFEEH;
BEGIN
WriteString("755B = "); WriteCard(Mode, 0); WriteLn;
WriteString("0FFH = "); WriteCard(Mask, 0); WriteLn;
WriteString("0C0FFEEH = "); WriteCard(Colour, 0); WriteLn;
WriteString("493 in octal: "); WriteOct(493, 0); WriteLn;
WriteString("255 in hex: "); WriteHex(255, 0); WriteLn;
WriteString("Padded hex: "); WriteHex(Colour, 8); WriteLn
END bases.
The letters of a hexadecimal number are upper case. InOut
prints numbers in octal and hexadecimal with WriteOct and
WriteHex; WriteHex uses lower-case letters and,
like the other procedures, pads on the left to the field width.
$ gm2 bases.mod -o bases $ ./bases 755B = 493 0FFH = 255 0C0FFEEH = 12648430 493 in octal: 755 255 in hex: ff Padded hex: c0ffee
Mistakes with bases
Without the leading zero, FFH is an identifier, not a number.
The compiler looks for a constant called FFH and does not find
one.
MODULE hexname; FROM InOut IMPORT WriteCard, WriteLn; CONST Mask = FFH; BEGIN WriteCard(Mask, 0); WriteLn END hexname.
The same happens with any hexadecimal number that starts with a letter. An
octal literal that contains the digit 8 or 9, such as 19B, is
rejected with a message that names the wrong digit.
$ gm2 hexname.mod -o hexname
hexname.mod:6:13: error: In program module ‘hexname’: module or symbol in qualident is not known
6 | Mask = FFH;
| ^
Character codes
A number in octal followed by C is a character literal: the
character with that code. 101C is the letter A,
whose code is 65, or 101 in octal. Character codes are the usual way to
write control characters such as the tab, the newline, or the bell.
MODULE codes;
FROM InOut IMPORT WriteString, WriteLn, WriteCard, Write;
CONST
Tab = 11C;
Letter = 101C;
Null = 0C;
BEGIN
WriteString("Letter: "); Write(Letter); WriteLn;
WriteString("Code of Letter: "); WriteCard(ORD(Letter), 0); WriteLn;
WriteString("Code of Null: "); WriteCard(ORD(Null), 0); WriteLn;
WriteString("name"); Write(Tab); WriteString("age"); WriteLn;
WriteString("Ada"); Write(Tab); WriteString("36"); WriteLn
END codes.
The constants have the type CHAR, so they are printed with
Write, and ORD returns their codes. The character
0C marks the end of a string that is shorter than its array; the
strings article explains how. Passing
Letter to WriteCard directly would fail, because a
character is not a number.
$ gm2 codes.mod -o codes $ ./codes Letter: A Code of Letter: 65 Code of Null: 0 name age Ada 36
Real literals
A real literal has a decimal point with digits on both sides of it. An
optional scale factor follows the letter E, which must be
upper case. A whole number such as 2 is not a real literal; the
real number two is written 2.0.
MODULE realnums;
FROM InOut IMPORT WriteString, WriteLn;
FROM RealInOut IMPORT WriteReal;
CONST
Gravity = 9.81;
Light = 2.99792458E8;
Tiny = 1.5E-3;
Two = 2.0;
BEGIN
WriteString("Gravity: "); WriteReal(Gravity, 0); WriteLn;
WriteString("Light: "); WriteReal(Light, 0); WriteLn;
WriteString("Tiny: "); WriteReal(Tiny, 0); WriteLn;
WriteString("Two: "); WriteReal(Two, 0); WriteLn
END realnums.
2.99792458E8 means 2.99792458 times ten to the eighth power.
WriteReal prints every value in fixed notation with six decimal
places. A literal such as 1.5e3 with a lower-case
e is a syntax error. GNU Modula-2 departs from Wirth's grammar
in two small ways: it accepts .5 without a leading digit, and it
rejects 2. with no digit after the point. Writing
0.5 and 2.0 works with every compiler.
$ gm2 realnums.mod -o realnums $ ./realnums Gravity: 9.810000 Light: 299792458.000000 Tiny: 0.001500 Two: 2.000000
String literals
A string literal is enclosed in double or single quotes. There is no escape character, so a string cannot contain the quote that encloses it; a string that contains a double quote is written in single quotes, and the other way round. A string of one character is also a character literal.
MODULE strings;
FROM InOut IMPORT WriteString, WriteLn, Write;
CONST
Said = 'He said "yes".';
Its = "It's done.";
Empty = "";
Star = "*";
BEGIN
WriteString(Said); WriteLn;
WriteString(Its); WriteLn;
WriteString("[");
WriteString(Empty);
WriteString("]"); WriteLn;
Write(Star); Write(Star); Write(Star); WriteLn
END strings.
Empty is the empty string, so nothing is printed between the
brackets. Star is a one-character string, which can be passed to
Write as a CHAR. A string may not span lines.
$ gm2 strings.mod -o strings $ ./strings He said "yes". It's done. [] ***
Joining strings
GNU Modula-2 joins string constants with the + operator, a
feature of ISO Modula-2 that the compiler also accepts in PIM mode. Joining
is useful for long strings, which cannot span lines on their own, and for
building one constant from others.
MODULE joined;
FROM InOut IMPORT WriteString, WriteLn;
CONST
Language = "Modula-2";
Author = "Niklaus Wirth";
Title = Language + " by " + Author;
Note = "A constant may be long, " +
"so it can be split over lines.";
BEGIN
WriteString(Title); WriteLn;
WriteString(Note); WriteLn
END joined.
Title is built from two other constants and a literal, and
Note continues on the next line after the +. The
operator works only on constants; strings held in variables are joined with
the procedures of the strings article.
$ gm2 joined.mod -o joined $ ./joined Modula-2 by Niklaus Wirth A constant may be long, so it can be split over lines.
Constant expressions
The value of a constant may be an expression, as long as the compiler can
compute it. It may use other constants, the arithmetic operators, and the
standard functions MIN, MAX, ORD,
CHR and SIZE.
MODULE expressions;
FROM InOut IMPORT WriteString, WriteLn, WriteInt, WriteCard, Write;
CONST
Rows = 8;
Cols = 8;
Squares = Rows * Cols;
Half = Squares DIV 2;
Limit = MAX(INTEGER) DIV 1000;
First = 'a';
Last = CHR(ORD(First) + 25);
Code = ORD(Last) - ORD(First);
Words = SIZE(INTEGER) * 1024;
BEGIN
WriteString("Squares: "); WriteInt(Squares, 0); WriteLn;
WriteString("Half: "); WriteInt(Half, 0); WriteLn;
WriteString("Limit: "); WriteInt(Limit, 0); WriteLn;
WriteString("Letters: "); Write(First); WriteString(" to ");
Write(Last); WriteLn;
WriteString("Distance: "); WriteCard(Code, 0); WriteLn;
WriteString("Bytes: "); WriteCard(Words, 0); WriteLn
END expressions.
Last is the letter 25 places after a, and
Code recovers the distance between the two letters.
$ gm2 expressions.mod -o expressions $ ./expressions Squares: 64 Half: 32 Limit: 2147483 Letters: a to z Distance: 25 Bytes: 4096
Next steps
The operators article covers the operators used in expressions and their precedence, and the type conversions article shows how values move between types.
Source
Modula-2 language reference at modula2.org
In this article we have declared constants and written whole numbers in decimal, octal and hexadecimal, characters by their codes, real numbers with scale factors, and strings in both kinds of quotes. We have joined string constants and computed constants from expressions. The data types article describes the types these values belong to.
Author
List all Modula-2 tutorials.