Pascal - Data Types

From SwinBrain

In Pascal there are six different kinds of types.


ref28x.png

  • 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.

Contents

Simple Type

The simple types are used to store numeric values. These are either an ordinal or real type as described in the following diagram.


ref29x.png

Ordinal type

Ordinal types have the following characteristics (from the FPC documentation):

  1. Ordinal types are countable and ordered (e.g. 1, 2, 3, etc)
  2. Ordinal values have a smallest possible value. (e.g. for a Byte the smallest value is 0)
  3. 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.

  • Smallint
    • Range -32768 .. 32767
  • Integer
    • The majority of the time an integer will be sufficient
    • Range -2147483648 .. 2147483647
  • Int64
    • Range -9223372036854775808 .. 9223372036854775807

Note: The Int64 and QWord types are not ordinal, though they are integer types.

Real Type

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.

  • Single
    • Range 1.5E-45 .. 3.4E38
    • Significant digits 7-8
  • Double
    • Range 5.0E-324 .. 1.7E308
    • Significant digits 15-16

String Type

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.

Structured Type

The following syntax diagram illustrates the different structured types available in Pascal. These are described in the following syntax diagram.


ref36x.png

We are only going to examine the array type here.

Array Type

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.


ref37x.png

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 

[edit]Pascal: Programs | Functions and Procedures | Statements | Data Types | Identifiers | ...
Links: Topic Brains
Return to Swin Brain