The type of a data item determines the set of values that it can hold and the operations that can be performed on it. Pascal is a 'strongly typed' programming language, which means that clear and sharp distinctions are drawn between data items of different types and it is not always possible to switch freely between types.
There are three main data types:
Of these the numeric type provides the greatest range of types, character types have a limited range and Boolean types are exactly as they appear here.
Numeric types divide into two main groups, the integers and the real numbers.
If a variable is declared as being of integer type then it can store only whole numbers. Integer types are used where a number has to be represented exactly and where fractional parts are not required. As we shall see, a for loop must have an integer type as its counter. The range of numbers that can be stored depends on the number of bits assigned to it by the language. In Borland Object Pascal the integer types available are as follows:
| Type | Numeric Range | Format |
| Integer | –2147483648..2147483647 | signed 32-bit |
| Cardinal | 0..4294967295 | unsigned 32-bit |
| Byte | 0..255 | unsigned 8-bit |
| Word | 0..65535 | unsigned 16-bit |
| Longword | 0..4294967295 | unsigned 32-bit |
| Shortint | –128..127 | signed 8-bit |
| Smallint | –32768..32767 | signed 16-bit |
| Longint | –2147483648..2147483647 | signed 32-bit |
| Int64 | –2^63..2^63–1 | signed 64-bit |
The format of a number relates to the number of bits it has (binary digits, the fundamental numeric unit inside a computer). If a number is unsigned it can store numbers up to the maximum possible for that number of bits but only as positive integers, negative numbers cannot be stored. If a number is signed then its left-most bit is used as a sign bit so it can store negative values but only up to half the range of an unsigned number.
For example, a variable of type Byte can store numbers in the range 0..255
while one of type Shortint can store numbers in the range -128..127. The number
of values that each of these types can store is 256 but the range differs
according to whether negative values are available. The same principle applies
to the other types, Word-Smallint and Longword-Longint. The Int64 type is
available for situations where very large integer values are required.
Choosing the correct type can be important in a program. If memory is limited
the precise specification may save precious bytes while the range of values
required by a number should always be considered carefully when choosing a data
type (an Ariane rocket is thought to have exploded on the launch pad due to an
old section of code having insufficient space for an updated value). In many
cases the programmer will use the simple integer type rather than one of
the more specific choices but this is not to be recommended.
Functions that operate on integer types include:
A real type defines a set of numbers that can be represented using floating-point notation. Floating point numbers are used where a fractional part is required (1.5, etc.) and/or where the range of a number exceeds that of the integer types. The range of the single real type, for example, is much greater than that of the equivalent integer type (integer or Longint) but the degree of accuracy is not at all the same. Integer numbers are precise within their range while real types are not precise but have a wider range.
Where a real type is required the programmer can use the basic real type in a declaration or one of the additional variants provided by Object Pascal:
| Type | Numeric Range | Significant digits | Size in bytes |
| Single | 1.5 x 10^–45 .. 3.4 x 10^38 | 7–8 | 4 |
| Double | 5.0 x 10^–324 .. 1.7 x 10^308 | 15–16 | 8 |
| Extended | 3.6 x 10^–4951 .. 1.1 x 10^4932 | 19–20 | 10 |
| Comp | –2^63+1 .. 2^63 –1 | 19–20 | 8 |
| Currency | –922337203685477.5808.. 922337203685477.5807 | 19–20 | 8 |
| Real48 | 2.9 x 10^–39 .. 1.7 x 10^38 | 11–12 | 6 |
The storage format for real numbers (mantissa and exponent) is too complex to go into at this stage but you should be clear that real numbers are very distinct from integers.
The simplest character type is char, which defines a single character. A string is a group or array of characters (see the section on arrays). The declaration:
var mychar:char;
allows any single character to be held in mychar, as in
mychar:='z';
While Object Pascal accepts variables of type char the preferred type declarations are now AnsiChar and WideChar. AnsiChar types are 8 bits wide (one byte) and have 256 character; they are usually set to the order of the locale character set (the Windows locale). WideChar types are 16 bits wide and follow the order of the Unicode character set, with the first 256 characters corresponding to the ANSI character set.
The symbol of a given character value can be obtained from the chr() function:
mychar:=chr(122);
The numeric value of a character in its character set can be obtained from the ord function:
mycharvalue:=ord('z')
Boolean types are named after George Boole, the 19th century mathematician who devised Boolean logic and algebra, which is based on outcomes of true or false. A Boolean variable in Object Pascal can take on the results true or false.
A Boolean expression typically compares one value with another and returns a Boolean result of true or false.
Boolean operators include: =, >, <, >=, <=, <> and not. Boolean expressions appear in if statements and while loops (see programming structures) and Boolean variables are often used to set conditions inside sections of code that are tested later. One commonly used technique, for example, is to set a Boolean variable found to false and then execute some code that will try to find some object; if the object is found the Boolean variable is set to true and its value is examined when the search has finished, as in:
if found then showmessage(...
An ordinal type is one that has a set of values that appear in a distinct order; each value has a predecessor (except the first) and a successor (except the last). Ordinal types include integer, character, Boolean, enumerated and sub-range types (see below). Ordinal types use the following fucntions:
We have seen what types data items can take, now we can consider how they are used in programs. Object Pascal provides the categories of const, type and var within a program, always in that order.
Const is short for 'constant' and is used to declare an object whose value will not vary in the course of a program. This is used for objects such as pi or the size limit of an array.
Type is more complex and includes structured types such as arrays, records, files and classes but it is also of some relevance to simple, unstructured types. In particular it includes the sub-range type and the enumerated type, both of which can be declared by the user and both of which are ordinal in nature.
A sub-range type can be declared as any sub-range of an existing ordinal type, for example:
type my_integer_range=0..10;
type my_letters='A'..'Z';
An enumerated type is one declared by the programmer, for example:
type mycolours=(Red, Blue, Green, Yellow);
The programmer can use an enumerated type as if it were any other ordinal type, for example:
for colour:=red to yellow do...
Var provides a section in a program (or unit or procedure or function) where variables are declared. A variable is a program object whose value can vary during execution (unlike a const whose value stays the same). Variables can be any of the types considered above. Variables have their values assigned by the assignment operator ':=', as in x:=10;.