Data Types |
2 |
![]() |
This chapter contains the following sections:
Summary of Data Format Differences
A few data formats, particularly of structured types, change when you use the Pascal compiler -calign option, when you use the -xl option, and when you use the -calign with the -xl option. This section describes the data alignments and sizes that change with these options. See the remainder of the chapter for information on types that do not change when you use these options. Default Data Alignments and Padding
Here is a summary of the default data alignments and padding. Records
The alignment of a record is always four bytes. Elements take their natural alignment, but the total size of a record is always a multiple of four bytes. Packed Records
Elements of types enumerated, subrange, integer16, and sets with a cardinal number less than 32 are bit-aligned in packed records. Variant Records
The alignment of each variant in a record is the maximum alignment of all variants. Arrays
The alignment of a array is equal to the alignment of the elements, and the size of most arrays is simply the size of each element times the number of elements. The one exception to this rule is that the arrays of aggregates always have a size that is a multiple of four bytes. Sets
Sets have an alignment of four bytes when they are longer than 16 bits; otherwise, their alignment is two bytes. The size of a set is always a multiple of two bytes. Enumerated Types
The size and alignment of enumerated types can be one byte or two, depending on the number of elements defined for the type. Subranges
The size and alignment of subrange types varies from one to four bytes, depending on the number of bits requires for its minimum and maximum values. See Table 2-7 on page 26 for examples.
Data Formats with -calign-
With the -calign option, the data formats are: Records
The alignment of a record is equal to the alignment of the largest element. Packed Records
Packed records are the same as the default, except integer elements are not bit-aligned. Arrays
The size of all arrays is the size of each element times the number of elements. Sets
Sets have an alignment of two bytes. The size is the same as the default. Data Formats with -xl-
In addition to the structured types discussed below, two simple data types change their sizes with the -xl option:
Data Formats with -calign and -xl-
When you use -xl with -calign, alignments and padding are the same as with -xl alone, with the following differences: Arrays
Arrays are the same as with -calign alone, except the size of an array of booleans is always a multiple of two. Varying Arrays
Varying arrays have an alignment of four bytes. The size is a multiple of four.
real
Pascal supports the standard predeclared real data type. As extensions to the standard, Pascal also supports:
This example declares five real variables:
var x: real; y: shortreal; z: longreal; weight: single; volume: double; |
real Initialization
To initialize a real variable when you declare it in the var declaration of your program, create an assignment statement as follows:
This example initializes the variable ph to 4.5 and y to 2.71828182845905e+00.
|
var ph: single := 4.5; y: longreal := 2.71828182845905e+00; |
You can also initialize real variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:
This example initializes the variable sum to 5.0, which has been declared as static single.
|
procedure foo (in x : single; var |
The example in the following section defines six valid real constants, two of which do not have a digit after the decimal point.
real Constants
Here is an example that of a real constant:
const n = 42.57; n2 = 4E12; n3 = 567.; n4 = 83.; n5 = cos(567.)/2; n6 = succ(sqrt(5+4)); |
Data Representation
Pascal represents real, single, shortreal, double, and longreal data types according to the IEEE standard, A Standard for Binary Floating-Point Arithmetic. Figure 2-1 shows the representation of a 32-bit floating point number; Figure 2-2 shows the representation of a 64-bit floating point number.
Figure 2-1 32-Bit Floating-Point Number
Figure 2-2 64-Bit Floating-Point Number
(-1)sign * 2 exponent-bias *1.f
f is the bits in the fraction. Extreme exponents are represented as shown in Table 2-2.
Table 2-2 Representation of Extreme Exponents
Table 2-3 Hexadecimal Representation of Selected Numbers
Integer
Pascal supports the standard predeclared integer data type. As extensions to the standard, Pascal also supports the integer16 and integer32 data types, integer initialization in the variable declaration, and integer constants in a base other than base 10. Integer Variables
Table 2-4 lists the minimum and maximum values of the integer data types.
Table 2-4 Integer Data Types
var i: integer; score: integer16; number: integer32; |
To define an unsigned integer in Pascal, use a subrange declaration. The subrange syntax indicates the lower and upper limits of the data type, as follows:
This code limits the legal values for the variable unsigned_int to 0 through 65536.
|
type unsigned_int = 0..65536; var u: unsigned_int; |
Integer Initialization
To initialize integer variables when you declare them in the var declaration part of your program, put an assignment statement in the declaration, as follows:
This example initializes the variables a and b to 50 and c to 10000.
|
var a, b: integer32 := 50; c: integer16 := 10000; |
You can also initialize integer variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:
This code initializes the variable sum to 50, which has been declared as static integer16.
|
procedure show (in x : integer16; out y: integer16); var sum: static integer16 := 50; |
Integer Constants
You define integer constants in Pascal the same as you do as in standard Pascal.
const x = 10; y = 15; n1 = sqr(x); n2 = trunc((x+y)/2); n3 = arshft(8, 1); |
maxint and minint
The value Pascal assigns to the integer constants maxint and minint is shown in Table 2-5.
maxint
32
2,147,483,647
16
32,767
minint
32
-2,147,483,648
16
-32,768
Table 2-5 Values for maxint and minint-
Without -xl
With -xl
Constant
Bits
Value
Bits
Value
In Another Base
To specify an integer constant in another base, use the following format:base#number
base is an integer from 2 to 36. number is a value in that base. To express number, use the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and then use the letters a to z. Case is insignificant; a is equivalent to A.
Data Representation
Pascal represents integer, integer16, and integer32 data types in twos complement format. Figure 2-3 shows the representation of a 16-bit integer. Similarly, Figure 2-4 shows the representation of a 32-bit integer.
Figure 2-3 16-Bit Integer
Figure 2-4 32-Bit Integer
boolean
Pascal supports the standard predeclared data type boolean. As an extension to the standard, Pascal permits you to initialize boolean variables in the variable declaration. boolean Variables
In Pascal, you declare boolean variables the same as in standard Pascal. Both of the following are valid boolean variables:
This example declares the variables cloudy and sunny as boolean.
|
var cloudy: boolean; sunny: boolean; |
boolean Initialization
To initialize a boolean variable when you declare it in the var declaration of your program, use an assignment statement, as follows:
This example initializes cloudy to true and sunny to false.
|
var cloudy: boolean := true; sunny: boolean := false; |
You can also initialize boolean variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:
This code initializes the variable rainy to false, which has been declared as static boolean.
|
function weather (x: integer): boolean; var rainy: static boolean := false; |
boolean Constants
You declare boolean constants in Pascal the same as in standard Pascal. Three valid boolean constants follow:
This example declares the constants a as true and b as false. It also declares n as the value odd(y).
|
const a = true; b = false; y = 15; n = odd(y); |
Data Representation
Pascal allocates one byte for each boolean variable. Figure 2-5 shows how Pascal internally represents a true boolean variable; Figure 2-6 shows how Pascal represents a false boolean variable.
Figure 2-5 true boolean Variable
Figure 2-6 false boolean Variable
Character
Pascal supports the standard predeclared data type char. As extensions to the standard, Pascal supports character initialization in the variable declaration and four nonstandard character constants. Character Variables
You declare character variables in Pascal the same as you do in standard Pascal. Each of the following is a valid character variable:
var current_character: char; largest: char; smallest: char; |
Character Initialization
To initialize a character variable when you declare it in the var declaration of your program, create an assignment statement, as follows:
This example initializes the variable pass to A and fail to F.
|
var pass: char := 'A'; fail: char := 'F'; |
You can also initialize character variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:
Character Constants
Pascal extends the standard definition of character constants by predeclaring the four character constants in Table 2-6.
minchar
Equal to char(0)
maxchar
Equal to char(255)
bell
Equal to char(7) (which makes your terminal beep)
tab
Equal to char(9) (which makes a tab character)
Table 2-6 Nonstandard Predeclared Character Constants
Constant
Description
Data Representation
Pascal allocates one byte for each character variable.
Enumerated Types
Pascal supports enumerated data types with extensions that allow you to input enumerated types with the read and readln procedures and output them with the write and writeln procedures. See the listings on read and write in Chapter 7, "Input and Output," for details.
Enumerated Variables
You declare enumerated data types in Pascal the same as in standard Pascal.
type continents =(North_America, South_America, Asia, Europe, Africa, Australia, Antartica); gem_cuts = (marquis, emerald, round, pear_shaped); var x: gem_cuts; index: continents; |
Data Representation
When you compile your program without the -xl option, Pascal represents enumerated types as either 8 or 16 bits, depending on the number of elements defined for that type. With -xl, Pascal represents variables of enumerated type as 16 bits. Pascal stores enumerated types as integers corresponding to their ordinal value.
Figure 2-7 16-Bit Enumerated Variable
colors = (red, green, blue, orange);
Pascal represents each value as shown in Figure 2-8.
Figure 2-8 Sample Enumerated Representation
Subrange
Pascal supports a subrange of integer, boolean, character, and enumerated data types. Subrange Variables
See "Integer Variables" on page 17 for an example of a subrange declaration.
Data Representation
The Pascal subrange takes up the number of bits required for its minimum and maximum values. Table 2-7 shows the space allocation of six subranges.
0..127
8
16
-128..127
8
16
0..255
16
16
-32768..32767
16
16
0...65536
32
32
-2,147,483,648..2,147,483,647
32
32
Table 2-7 Subrange Data Representation
Minimum/Maximum Range
Without -xl (Bits)
With -xl (Bits)
Figure 2-9 16-Bit Subrange
Figure 2-10 32-Bit Subrange
Record
Pascal supports the standard record and packed record data types. As an extension, Pascal permits you to initialize a record variable when you declare it in the variable declaration. Record Variables
You declare records in Pascal the same as in standard Pascal, as shown in the following example:
Record Initialization
To initialize a field in a record when you declare it in the var declaration of your program, use either of the following two formats:
[a := FALSE , b := TRUE ] |
[ FALSE , TRUE ] |
You can also initialize record variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static.
Initializing Record Variables (Screen 1 of 2)
Initializing Record Variables (Screen 2 of 2)
Data Representation of Unpacked Records
This section describes the data representations of unpacked fixed and variant records. Fixed Records
Pascal allocates fields in a fixed record so that they assume the natural alignment of the field type. The alignment of a record is equal to the alignment of the largest element. The size of the record is a multiple of the alignment. Variant Records
The space Pascal allocates for a variant record is the same with or without the -xl option. Data Representation of Packed Records
Table 2-8, Table 2-9, and Table 2-10 show how Pascal aligns fields in a packed record.
real
4 bytes
4 bytes
integer
2 bytes
Bit-aligned
integer16
2 bytes
2 bytes
Note - In packed records, bit-aligned fields do not cross word boundaries.
Packed Record Storage Without the -xl Option
Table 2-8 Packed Record Storage Without -xl-
Packed Record Storage with the -xl Option
Table 2-9 Packed Record Storage with -xl-
Data Type
Size
Alignment
Packed Record Storage with the -calign Option
Table 2-10 Packed Record Storage with -calign
Data Type
Size
Alignment
Table 2-11 Sample Sizes and Alignment of Packed Record
Figure 2-11 Sample Packed Record (Without -xl)
Array
Pascal supports the standard array data type. As extensions to the standard, Pascal supplies the predeclared character array types alfa, string, and varying and permits you to initialize an array variable when you declare it in the variable declaration. Array Variables
In addition to the standard array data types, this compiler supports the three data types in Table 2-12, which include a variable-length string.
Table 2-12 Array Data Types
The commands to compile and execute varying.p
|
hostname% pc varying.p hostname% a.out van Gogh and Monet . Rembrandt and Breughel . Matisse and Cezanne. |
Array Initialization
To initialize an array variable when you declare it in the var declaration of your main program, use an assignment statement after the declaration. Pascal offers you the following four different formats:
In this example, the compiler assigns the upper bound of 5 to int and of 6 to c1.
|
var i : integer; int : array[1..*] of integer := [maxint, 1, -32767, 5, 20]; c1 : array[1..*] of char := '123456'; |
This code initializes all the first 50 values of int2 to 1 and the second 50 values to 2.
|
var int2 : array[1..100] of integer := [50 of 1, 50 of 2]; |
When you initialize an array in the var declaration, the compiler sets those elements for which it doesn't find data to zero.
Packed Arrays
Although you can define an array as packed, it has no effect on how the Pascal compiler allocates the array data space. Data Representation
The elements of an array require the same space as that required by the base type of the array. However, there are two exceptions to this. With the
-calign option, the size of all arrays is the size of each element times the number of elements. When you use the -calign and -xl options together, arrays are the same as with -calign alone, except the size of an array of booleans is always a multiple of two.
Set
Pascal supports sets of elements of integer, boolean, character, and enumerated data types. As extensions to the standard, Pascal predefines a set of intset; you can then initialize a set variable when you declare it in the var declaration of your program. Set Variables
In Pascal, you declare set variables the same as you do in standard Pascal. The following is a valid set variable:
type character_set = set of char; var letters: character_set; |
Pascal predefines the set intset as the set of [0..127].
Set Initialization
To initialize a set variable when you declare it in the var declaration of your program, create an assignment statement, as follows:
This code initializes citrus to the set of orange, lemon, and lime.
|
type fruit = (orange, lemon, lime, apple, banana); var citrus: set of fruit := [orange, lemon, lime]; |
You can also initialize set variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static:
Packed Sets
Although you can define a set as packed, it has no effect on how the compiler allocates the set data space. Data Representation
Pascal implements sets as bit vectors, with one bit representing each element of a set. The maximum ordinal value of a set element is 32,768.
Table 2-13 Data Representation of Sets
var smallset: set of 2..15 := [7,4,3,2]; |
Figure 2-12 Small Set
var largeset: set of 2..255 := [7,4,3,2]; |
Figure 2-13 Large Set
File
Pascal treats files declared as file of char the same as files declared as text, except when you use the -s -s0, -s1, -V0, or -V1 options. In this case, the compiler treats the two data types differently, as required by standard Pascal.
Pointer
Pascal supports the standard Pascal pointer and the nonstandard universal pointer and procedure and function pointer. Standard Pointer
The standard pointer is the same in Pascal and standard Pascal. Universal Pointer
The universal pointer data type, univ_ptr, is compatible with any pointer type. Use univ_ptr to compare a pointer of one type to another, to assign a pointer of one type to another, or to weaken type checking when passing parameters of pointer types.
The commands to compile and execute univ_ptr.p
|
hostname% pc univ_ptr.p hostname% a.out 41200000 41200000 |
Procedure and Function Pointers
The following is an example that shows how to use procedure and function pointers in Pascal.
The commands to compile and execute pointer.p
|
hostname% pc pointer.p hostname% a.out Enter red, white, or blue: red RED |
Pointer Initialization
To initialize a pointer variable when you declare it in the var declaration of your program, use an assignment statement, as follows:
You can also initialize pointer variables in the var declaration of a procedure or function; however, when you do so, you must also declare the variable as static.
Data Representation
Pascal represents a pointer as shown in Figure 2-14.