Reading input in Pascal
last modified November 18, 2026 Free Pascal 3.2.2
A command-line Pascal program reads from standard input with
Read and ReadLn. The same statements work whether the
input is typed at a terminal, piped from another program, or redirected from a
file. This article shows how to prompt for values, read several numbers from
one line, parse a line into a number without an exception, stop at a sentinel,
and process input until end of file.
Reading a line
ReadLn(s) reads one line into a string variable and discards the
line ending. A prompt is written with Write rather than
WriteLn, so the cursor stays on the same line. When the input is
piped, the prompt and the result both appear in the captured output.
program greet;
{$mode objfpc}{$H+}
var
name: string;
begin
Write('Name: ');
ReadLn(name);
WriteLn('Hello, ', name, '!');
end.
The program reads the whole line, including spaces, so a name such as
Mary Ann arrives intact. The newline is consumed, which matters
when the next statement reads another line. If the input has already reached
end of file, ReadLn returns an empty string and sets the
Eof flag.
$ printf 'Alice\n' | ./greet Name: Hello, Alice!
Reading several values
ReadLn(a, b) reads values into several variables. Numeric reads
skip leading spaces and line breaks, so two numbers can be separated by one
space, several spaces, or a newline. The line is consumed after the last
variable has been read.
program sum;
{$mode objfpc}{$H+}
var
a, b: Integer;
begin
Write('Two numbers: ');
ReadLn(a, b);
WriteLn(a, ' + ', b, ' = ', a + b);
end.
The input line 7 5 fills a with 7 and
b with 5. If the line contains only one number,
ReadLn continues reading the next line, because it has not found
the second value yet. A non-numeric character stops the program with runtime
error 106, the invalid numeric format error, so this form is safe only when the
input is known to be well formed.
$ printf '7 5\n' | ./sum Two numbers: 7 + 5 = 12
Read and ReadLn
Read reads the requested values and leaves the rest of the line
in the input buffer. ReadLn reads the values and then skips to the
next line. The difference matters when a number is followed by text on the same
line. The next program reads a count and then one character.
program readchars;
{$mode objfpc}{$H+}
var
c: Char;
n: Integer;
begin
Write('Count: ');
ReadLn(n);
Write('Characters: ');
ReadLn(c);
WriteLn('n = ', n, ', c = ', c);
end.
The first ReadLn consumes the line that holds the number, so the
second ReadLn starts at the beginning of the next line and reads
its first character. If the first call were Read(n), the newline
would remain in the buffer, and the next character read would be that newline
rather than the intended letter. Mixing the two forms is a common source of
skipped input.
$ printf '3\nX\n' | ./readchars Count: Characters: n = 3, c = X
Parsing a line safely
Reading a whole line and converting it with TryStrToInt keeps the
program in control. A malformed value returns False instead of
raising an exception, and the original text is still available for the error
message. The program below accepts either a number or a word.
program parseline;
{$mode objfpc}{$H+}
uses SysUtils;
var
line: string;
n: Integer;
begin
Write('Number: ');
ReadLn(line);
if TryStrToInt(line, n) then
WriteLn('twice = ', n * 2)
else
WriteLn('not a number: ', line);
end.
The first input produces twice = 42; the second reaches the
else branch and echoes the text unchanged. This pattern is the
basis of a menu or a settings reader: read a line, trim it, validate it, and
only then convert it. The strings article
describes the conversion routines in more detail.
$ printf '21\n' | ./parseline Number: twice = 42 $ printf 'abc\n' | ./parseline Number: not a number: abc
A sentinel loop
A sentinel is a special value that ends a sequence. The loop reads values until it sees the sentinel, which is not processed. The next program adds numbers until it reads zero, then prints the count and the total.
program sentinel;
{$mode objfpc}{$H+}
var
n, total, count: Integer;
begin
total := 0;
count := 0;
repeat
Read(n);
if n <> 0 then
begin
total := total + n;
Inc(count);
end;
until n = 0;
WriteLn('count = ', count);
WriteLn('total = ', total);
end.
Read(n) skips whitespace, so the numbers can be separated by
spaces or newlines. The sentinel is read before the condition is tested, which
is why a repeat loop fits better than a while loop
here. The program expects at least one number and a sentinel; a production
version would also test Eof and stop when the input runs out.
$ printf '3 4 5 0\n' | ./sentinel count = 3 total = 12
Reading until end of file
Eof is true when no more input is available. A
while not Eof loop processes every line and stops cleanly when the
input ends, which is the natural form for a filter that reads standard input
and writes standard output.
program eofloop;
{$mode objfpc}{$H+}
var
line: string;
count: Integer;
begin
count := 0;
while not Eof do
begin
ReadLn(line);
Inc(count);
WriteLn(count, ': ', line);
end;
WriteLn('lines = ', count);
end.
The loop reads one line per iteration and numbers it. The final
WriteLn runs after the last line has been processed. Redirecting a
file into the program works in exactly the same way as piping the output of
another command, because both connect the file to standard input.
$ printf 'alpha\nbeta\ngamma\n' | ./eofloop 1: alpha 2: beta 3: gamma lines = 3
Prompts and terminal input
When the input is a terminal, the user types a line and presses Enter. Pascal does not provide line editing, history, or tab completion; those come from the terminal or from a library such as readline. The prompt should end without a newline, and the program should not assume that the user types valid data. Reading a line and validating it is more robust than reading a number directly, because the program can report the invalid text and ask again.
For a repeated prompt, wrap the read in a loop and exit when the conversion succeeds or when the user enters an empty line. A later article covers the other way of passing data to a program, which avoids interactive input entirely.
Next steps
The records article stores the parsed values in a typed structure, and a later article reads parameters without a prompt. Another later article shows how to catch the exceptions that a failed conversion can raise.
Source
In this article we have read strings and numbers with ReadLn,
distinguished Read from ReadLn, parsed a line with
TryStrToInt, used a sentinel loop, and processed input until end
of file. The series continues with
Pascal records.
Author
List all Pascal tutorials.