Chapter 9: Pascal Standard Libraries
Pascal comes with several standard libraries, also called units, that cover a wide range of tasks, from basic input/output to mathematical computation and string manipulation. Knowing them saves you a lot of work and lets your programs do more. This chapter covers some of the most commonly used ones: crt, SysUtils, and Math.
The CRT unit
The crt unit has functions and procedures for console input and output and for managing the screen.
Including the CRT unit
To use the crt unit, include it in the uses clause of your program.
uses crt;
Clearing the screen
begin
ClrScr;
writeln('Screen cleared!');
end.
Moving the cursor
begin
GotoXY(10, 5);
writeln('Cursor moved to position (10, 5)');
end.
Changing text color
begin
TextColor(Red);
writeln('This text is red.');
TextColor(White);
end.
Pausing the program
begin
writeln('Press any key to continue...');
ReadKey;
end.
The SysUtils unit
The SysUtils unit has system-related utilities for string manipulation, file handling, date and time operations, error handling, and more.
Including the SysUtils unit
Add the SysUtils unit to the uses clause of your program to use it.
uses SysUtils;
String manipulation
Converting strings to numbers
var
str: string;
num: integer;
begin
str := '123';
num := StrToInt(str);
writeln('Converted string to integer: ', num);
end.
Converting numbers to strings
var
num: integer;
str: string;
begin
num := 456;
str := IntToStr(num);
writeln('Converted integer to string: ', str);
end.
File handling
Checking if a file exists
var
fileName: string;
begin
fileName := 'test.txt';
if FileExists(fileName) then
writeln('File exists.')
else
writeln('File does not exist.');
end.
Deleting a file
var
fileName: string;
begin
fileName := 'test.txt';
if DeleteFile(fileName) then
writeln('File deleted.')
else
writeln('Failed to delete file.');
end.
Date and time operations
Getting the current date and time
var
currentTime: TDateTime;
begin
currentTime := Now;
writeln('Current date and time: ', DateTimeToStr(currentTime));
end.
Don’t name the variable now. Pascal ignores case, so a variable called now hides the Now function, and now := Now would copy the uninitialized variable into itself instead of reading the clock.
Formatting date and time
var
currentTime: TDateTime;
begin
currentTime := Now;
writeln('Formatted date: ', FormatDateTime('yyyy-mm-dd', currentTime));
writeln('Formatted time: ', FormatDateTime('hh:nn:ss', currentTime));
end.
The Math unit
The Math unit has mathematical functions and constants.
Including the Math unit
As with the other units, list Math in the uses clause of your program to use it.
uses Math;
Common mathematical functions
Calculating the square root
var
x, result: real;
begin
x := 16.0;
result := Sqrt(x);
writeln('Square root of ', x:0:2, ' is ', result:0:2);
end.
Sqrt is built in: it comes from the System unit, which every program uses automatically, so this example works without uses Math.
Calculating the power of a number
var
base, exponent, result: real;
begin
base := 2.0;
exponent := 3.0;
result := Power(base, exponent);
writeln(base:0:2, ' raised to the power of ', exponent:0:2, ' is ', result:0:2);
end.
Constants
Mathematical constants
begin
writeln('Value of Pi: ', Pi:0:10);
end.
Like Sqrt, Pi is built into the System unit. The Math unit adds constants such as Infinity, NaN, and MaxFloat.