inputtypesintegerfreepascal

Why does read(x) set -31073 to x instead of 99999 when entering 99999 in the terminal


This is a program written in Free Pascal:

program readTest;
var
    x: integer;
begin
    read(x);
    writeln(x)
end.

This is the execution script of this program:

$ echo 99999 | ./readTest
-31073
$

Why does x contain -31073 instead of 99999?


Solution

  • From Free Pascal documentation:

    The integer type is an alias to the smallint type in the default Free Pascal mode. It is an alias for the longint type in either Delphi or ObjFPC mode.

    Smallint -32768 .. 32767

    99999 is out of valid range for x variable, that's why you got this "shifted" value.