Pascal
Online Pascal Compiler
main.pas is compiled with Free Pascal 3.2 (fpc -O2) and run as a native program. Output isn’t buffered here, so Write('Prompt: ') shows before ReadLn waits for your input.
Try it aboveHow Pascal runs here
- Versions
- Free Pascal 3.2
- Build
fpc -O2 -o a.out main.pas- Run
./a.out- Limits per run
- 256 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A Pascal program that reads input
This main.pas reads a name and some numbers, and prints the average with two decimals:
program Main; var name: string; n, i, x: Integer; sum: LongInt; begin Write('Your name: '); ReadLn(name); Write('How many numbers? '); ReadLn(n); sum := 0; for i := 1 to n do begin Write('Number ', i, ': '); ReadLn(x); sum := sum + x; end; WriteLn('Thanks, ', name, '. Sum: ', sum, ', average: ', sum / n :0:2); end.
$ fpc -O2 -o a.out main.pas && ./a.out Your name: Ada How many numbers? 4 Number 1: 12 Number 2: 7 Number 3: 30 Number 4: 5 Thanks, Ada. Sum: 54, average: 13.50
Reading input in Pascal
- ReadLn(x) reads a value and moves past the end of the line. ReadLn(a, b) reads two numbers typed on one line.
- Read(x) stays on the same line, so a ReadLn(name) right after it gets an empty string. Use ReadLn unless you mean to read more from that line.
- In Free Pascal’s default mode, Integer is 16-bit (−32768 to 32767) and string holds up to 255 characters. Add {$mode objfpc}{$H+} at the top for a 32-bit Integer and long strings, or use LongInt as the sum above does.
What isn’t available here
- Lazarus forms and dialogs (the LCL) and the BGI Graph unit: the program runs in a terminal and can’t open a window.
- Range checking is off, so an array index out of bounds doesn’t stop the program. Add {$R+} to make it fail with a runtime error instead.
- DOS-only Turbo Pascal code, such as interrupts and direct screen memory.
Errors you’ll see
- “Error: Identifier not found "total"” A typo, or a variable missing from the var section.
- A Fatal: Syntax error, ";" expected message: a missing semicolon at the end of the line before the one reported.
- “Runtime error 106” Invalid numeric format: ReadLn into an Integer got text that isn’t a number.
Questions
Which Pascal compiler does this use?
Free Pascal 3.2, compiling main.pas with fpc -O2.
Does Turbo Pascal code work?
Most console programs do: Free Pascal’s default mode is close to Turbo Pascal, down to the 16-bit Integer. Code that relies on DOS, interrupts or the Graph unit doesn’t.
How do I read input in Pascal?
ReadLn(x) for one value per line, or ReadLn(a, b) for two on one line. Type them in the terminal while the program runs.