Pascal literals and constants
last modified November 18, 2026 Free Pascal 3.2.2
A literal is a value written directly in the source text. Pascal has literals
for integers, reals, characters, strings, Booleans, and the pointer value
nil. A constant gives a name to a value and lets the compiler
check it once instead of repeating it. This article shows how both are written
and where the compiler is stricter than a reader might expect.
Integer literals
An integer literal is a sequence of digits. Pascal also accepts hexadecimal
literals introduced by $, binary literals introduced by
%, and octal literals introduced by &. The
prefix is part of the literal, not an operator, and the value is converted to
the integer type that the context requires.
program intliterals;
{$mode objfpc}{$H+}
begin
WriteLn('decimal: ', 255);
WriteLn('hex: ', $FF);
WriteLn('binary: ', %11111111);
WriteLn('octal: ', &377);
end.
All four literals denote the same value, 255. Hexadecimal digits are the
usual 0 to 9 and A to F,
in either case. Binary literals accept only 0 and 1,
and octal literals only 0 to 7. Free Pascal does not
accept underscores inside a numeric literal, so 1_000_000 is a
syntax error; write the digits without separators.
$ fpc -gl intliterals.pas $ ./intliterals decimal: 255 hex: 255 binary: 255 octal: 255
Real literals
A real literal contains a decimal point, an exponent, or both. The exponent
is written with e or E and an optional sign, as in
1.5e3. A number without a point or exponent is an integer
literal, even when it is assigned to a real variable; the compiler converts it
in that case.
program realliterals;
{$mode objfpc}{$H+}
begin
WriteLn('plain: ', 1.5:0:2);
WriteLn('exponent:', 1.5e3:0:1);
WriteLn('small: ', 1.5e-3:0:5);
WriteLn('typed: ', 2.5);
end.
The first three lines use the width-and-precision syntax to make the values
readable. 1.5e3 is 1500 and 1.5e-3 is 0.0015. The
last line prints a real with the default formatting, which uses scientific
notation and shows the stored binary value. The
data types article explains why the default
form is so verbose.
$ fpc -gl realliterals.pas $ ./realliterals plain: 1.50 exponent:1500.0 small: 0.00150 typed: 2.500000000E+00
Character literals
A character literal is written between single quotes, as in 'A'.
The same character can be written by its numeric code with a hash sign, as in
#65, or with a caret and a letter for a control character, as in
^A. The caret form denotes the character whose code is the
letter's position in the alphabet, so ^A is code 1 and
^M is carriage return.
program charliterals;
{$mode objfpc}{$H+}
begin
WriteLn('quote: ', 'A');
WriteLn('code: ', #65);
WriteLn('control: ', Ord(^A));
WriteLn('joined: ', 'a'#32'b');
WriteLn('newline: ', 'a'#10'b');
WriteLn('apostr: ', 'It''s');
end.
#65 prints as A because 65 is the code of the upper
case letter in ASCII and UTF-8. Ord(^A) prints 1, the code of the
control character. A character code can be placed between two string parts,
so 'a'#32'b' is the three-character string a b, and
'a'#10'b' contains a line feed. The doubled quote in
'It''s' is the only escape a Pascal string needs.
$ fpc -gl charliterals.pas $ ./charliterals quote: A code: A control: 1 joined: a b newline: a b apostr: It's
String literals
A string literal is a sequence of characters between single quotes. A quote
inside the string is written twice, and a character code can be concatenated
without an operator, as the previous section showed. Two adjacent string
literals are not joined automatically: 'Hello, ' 'Pascal' is a
syntax error. Use the + operator or write one literal.
A string literal has the type AnsiString when {$H+}
is active, and the compiler stores it in the current code page. The literal
can contain any byte except an unescaped single quote, including non-ASCII
text when the file is saved as UTF-8. The next section uses a string constant
to build a longer message at compile time.
Constants
A const section gives a name to a value. The value must be known
at compile time: a literal, an expression of literals, or a call to a function
that the compiler can evaluate, such as Ord. A constant has no
storage of its own unless it is typed, and it cannot be changed at run time.
program constants;
{$mode objfpc}{$H+}
const
Pi = 3.14159265;
MaxItems = 100;
Message = 'Hello' + ', ' + 'Pascal';
Alpha = Ord('A');
resourcestring
ErrorText = 'Something went wrong';
begin
WriteLn(Message);
WriteLn('Pi = ', Pi:0:5);
WriteLn('MaxItems = ', MaxItems);
WriteLn('Ord(A) = ', Alpha);
WriteLn(ErrorText);
end.
Pi, MaxItems, Message, and
Alpha are ordinary constants. The compiler folds the string
concatenation and the Ord call, so no work is left for the
program. resourcestring declares a string that can be replaced by
a localized resource; without resource support it behaves like a constant.
The data types article explains why
Pi prints with five decimals here and how reals are stored.
$ fpc -gl constants.pas $ ./constants Hello, Pascal Pi = 3.14159 MaxItems = 100 Ord(A) = 65 Something went wrong
Booleans and nil
True and False are constants of type
Boolean, and nil is the value of a pointer or a
class reference that points to nothing. They are reserved words rather than
identifiers, so they cannot be redefined. nil is not the same as
an empty string or the number zero, although all three can look like "nothing"
in output.
The value of nil is tested with Assigned for
procedural variables and with a direct comparison for pointers and objects.
The pointers article later in the series returns to the subject.
For now it is enough to know that nil is a literal, not a
variable, and that assigning it to a pointer does not free any memory.
Next steps
The operators and expressions article shows how literals and variables are combined. The conditionals article uses Boolean literals and comparisons, and the loops article uses integer literals as bounds. The data types article, already mentioned, explains the representation behind the values.
Source
In this article we have written integer, real, character, and string literals, looked at hexadecimal, binary, and octal forms, and declared ordinary and resource constants. The series continues with Pascal operators and expressions.
Author
List all Pascal tutorials.