Creating and Running .pas Files
To write a Pascal program, you save your source code in a text file with the .pas extension, then compile it with the fpc command to produce an executable. The .pas extension is the standard convention for Pascal source files. A Pascal program begins with a program declaration and wraps its body between begin and end.. You can chain compilation and execution with && to run both steps in a single command.
Syntax
{ -----------------------------------------------
Basic structure of a .pas file
----------------------------------------------- }
{ The program declaration defines the program's name }
program ProgramName;
{ The uses clause loads libraries (optional) }
uses
SysUtils; { Standard library for string operations and more }
{ The const section declares constants (optional) }
const
CONSTANT_NAME = value;
{ The var section declares variables (optional) }
var
variable_name : TypeName;
{ begin...end. is the program body }
begin
{ Write your statements here }
end.
{ -----------------------------------------------
Compiling and running (terminal commands)
----------------------------------------------- }
{ Compile the source file to produce an executable }
fpc filename.pas
{ Run the executable in the current directory }
./filename
{ Compile and run in a single line }
fpc filename.pas && ./filename
Syntax reference
| Syntax / Command | Description |
|---|---|
program Name; | Declares the name of the program. By convention the name matches the filename, and the declaration ends with a semicolon. |
uses LibraryName; | Loads external libraries or standard units. Common examples include SysUtils and Math. |
const CONSTANT_NAME = value; | Declares a read-only constant. The const section must appear before the var section. |
var variable_name : TypeName; | Declares a variable. In Pascal, all variables must be declared in the var section before begin. |
begin | Marks the start of the program body where executable statements are written. |
end. | Marks the end of the program. The final end requires a dot (.), not a semicolon — semicolons are used for inner end keywords. |
fpc file.pas | Compiles file.pas and produces an executable named file. |
./file | Runs the executable file located in the current directory. |
fpc file.pas && ./file | Runs the executable only if compilation succeeds. If there is a compiler error, the run step is skipped. |
Sample code
sg_hello.pas
{ sg_hello.pas — A sample demonstrating the basic structure of a .pas file, }
{ compilation, and execution using Steins;Gate characters. }
{ Declares string, integer, and real variables, then prints them with WriteLn. }
{
Compile and run:
fpc sg_hello.pas && ./sg_hello
}
program sg_hello;
var
{ -----------------------------------------------
Variable declarations (all must appear before begin)
----------------------------------------------- }
member_name : string; { Lab member name }
lab_no : integer; { Lab member number }
divergence : real; { World line divergence rate (%) }
begin
{ -----------------------------------------------
Assignment (Pascal uses := for assignment)
----------------------------------------------- }
member_name := 'Okabe Rintaro';
lab_no := 1; { Lab member 001 }
divergence := 1.048596; { Attractor field convergence value }
{ -----------------------------------------------
Output (WriteLn appends a newline at the end)
----------------------------------------------- }
WriteLn('===== Future Gadget Lab =====');
WriteLn('Lab No. : ', lab_no);
WriteLn('Name : ', member_name);
WriteLn('Divergence : ', divergence:10:6, ' %'); { Formatted: width 10, 6 decimal places }
WriteLn('El Psy Kongroo');
WriteLn('=============================');
end.
Compile and run the program to see the output:
fpc sg_hello.pas && ./sg_hello Free Pascal Compiler version ... Linking ./sg_hello ===== Future Gadget Lab ===== Lab No. : 1 Name : Okabe Rintaro Divergence : 1.048596 % El Psy Kongroo =============================
sg_members.pas
{ sg_members.pas — A sample managing data for multiple characters using arrays. }
{ Uses data from five Steins;Gate lab members to demonstrate }
{ constants, arrays, and for loops in a .pas file. }
{
Compile and run:
fpc sg_members.pas && ./sg_members
}
program sg_members;
const
NUM_MEMBERS = 5; { Number of lab members (read-only constant) }
var
{ Array declarations (1-based indexing is the Pascal convention) }
names : array[1..NUM_MEMBERS] of string; { Lab member names }
lab_nos : array[1..NUM_MEMBERS] of integer; { Lab member numbers }
divergences: array[1..NUM_MEMBERS] of real; { Associated world line divergence rates (%) }
i : integer; { Loop counter }
begin
{ -----------------------------------------------
Data initialization
----------------------------------------------- }
names[1] := 'Okabe Rintaro'; lab_nos[1] := 1; divergences[1] := 1.048596;
names[2] := 'Makise Kurisu'; lab_nos[2] := 4; divergences[2] := 1.130205;
names[3] := 'Hashida Itaru'; lab_nos[3] := 3; divergences[3] := 0.571024;
names[4] := 'Shiina Mayuri'; lab_nos[4] := 2; divergences[4] := 0.000000;
names[5] := 'Amane Suzuha'; lab_nos[5] := 8; divergences[5] := 1.048596;
{ -----------------------------------------------
Display the lab member list
----------------------------------------------- }
WriteLn('===== Lab Members =====');
WriteLn('No. Name Divergence');
WriteLn('------------------------------------');
for i := 1 to NUM_MEMBERS do begin
{ Write outputs without a newline; WriteLn appends a newline }
Write(lab_nos[i]:3, ' ');
Write(names[i]);
WriteLn(' ', divergences[i]:10:6, ' %');
end;
WriteLn('------------------------------------');
WriteLn('Total members: ', NUM_MEMBERS);
WriteLn('=======================');
end.
Compile and run the program to see the output:
fpc sg_members.pas && ./sg_members Free Pascal Compiler version ... Linking ./sg_members ===== Lab Members ===== No. Name Divergence ------------------------------------ 1 Okabe Rintaro 1.048596 % 4 Makise Kurisu 1.130205 % 3 Hashida Itaru 0.571024 % 2 Shiina Mayuri 0.000000 % 8 Amane Suzuha 1.048596 % ------------------------------------ Total members: 5 =======================
Overview
Pascal source files are saved with the .pas extension by convention. A program begins with a program Name; declaration, followed by an optional constant section (const) and variable section (var), and ends with an executable block enclosed in begin and end.. One important characteristic of Pascal is that all variables must be declared in the var section before begin — you cannot declare variables inline. Note also that the final end takes a dot (.), not a semicolon, which is used only for inner block terminators. The assignment operator is :=; the equals sign = is reserved for comparisons and constant definitions. To compile, run fpc filename.pas, which produces an executable with the same name as the source file. For compiler options, see Setup. For the program structure itself, see PROGRAM / BEGIN / END structure.
If you find any errors or copyright issues, please contact us.