Pascal - Data Types
In Pascal there are six different kinds of types.
- The simple types are used to store numeric value as well as the boolean type. We will look at the simple types in more detail below.
- The string types are used to store textual data. We will look at the simple types in more detail below.
- There are several different types of structured types. These include things like records, objects, and classes as well as arrays. We are only going to examine arrays here. The other types will have their own sections.
- Pointer types are used to declare variables that refer to other data. The "value" of this variable is the memory location of the data that it is "pointing" to. Pointers will have their own section.
- Procedure types are used to declare a special kind of pointer that can refer to a function or a procedure. This will be covered elsewhere.
- Type identifiers are used to give alternative names to data types.
The simple types are used to store numeric values. These are either an ordinal or real type as described in the following diagram.
Ordinal types have the following characteristics (from the FPC documentation):
- Ordinal types are countable and ordered (e.g. 1, 2, 3, etc)
- Ordinal values have a smallest possible value. (e.g. for a Byte the smallest value is 0)
- Ordinal values have a largest possible value. (e.g. for a Byte the largest value is 255)
In Pascal there are two different kinds of simple ordinal types: Integers and Boolean. While there are different boolean types, mostly the Boolean itself is sufficient. Each Boolean variable can store either a True or False value. There are a number of different integer (i.e. whole number) types. The integer types that you will mostly work with are shown in the following list.
- Range -32768 .. 32767
- The majority of the time an integer will be sufficient
- Range -2147483648 .. 2147483647
- Range -9223372036854775808 .. 9223372036854775807
Note: The Int64 and QWord types are not ordinal, though they are integer types.
The real types are used to store numeric values, and can contain fractional parts. For example 1.0, 3.1415, 0.123 are all real values. There are a number of different real number types in Pascal. The following list describes the real types that you will use most of the time.
- Range 1.5E-45 .. 3.4E38
- Significant digits 7-8
- Range 5.0E-324 .. 1.7E308
- Significant digits 15-16
String types are used to store textual information. There are two basic string types that you can use in Pascal: char and string.
The char data type is used to store a single character such as 'a', 'b', '+', '-', etc. There are two ways of coding characters literals in Pascal. Firstly you can place the character within single quotes, for example, 'a' and 'A' are both character literals. Alternatively you can use the character's ASCII code preceeded by a #, for example #65 is the same as 'A'. Read about the ascii character set on wikipedia, also check out code page 437 which shows the characters that are printable in a console window (see the ascii printer sample).
The string type is used to store a number of characters such as 'Hello World'. Strings in Pascal can have any length, and the memory used by the string is managed by Pascal.
The following syntax diagram illustrates the different structured types available in Pascal. These are described in the following syntax diagram.
We are only going to examine the array type here.
An array type is used to declare a varaible which contains multiple values, where each value can be accessed by an index. The array type's syntax is shown in the following syntax diagram.
The following are all valid array declarations.
var x: Array[0..9] of Integer; //10 integers var y: Array of Integer; //a variable length array of integers