Pascal. Data types. Standard data types. Ordinal types

Data types. Standard data types. Integer types. Ordinal types. Functions Ord, Pred, Succ. Function SizeOf


Contents


Search other resources:

1. The concept of data type. Groups of data types

In a program, data is characterized by a so-called type defined for each variable declared in the program. In other words, each variable has its own specific type. The type of a variable is a characteristic that determines the set of values that a variable can take. Based on the type of the variable, a corresponding memory size is allocated for each variable.

The Pascal programming language allows you to use a set of types, which are divided into two groups:

  • standard (predefined) types;
  • custom types. These are user-defined (programmer) types.

Standard types have well-defined names and are available anywhere in the program. All standard types are described in the System module. To use standard types, the program does not need to include the System unit in the uses clause, since this unit is included by default. Standard types are available in any module of the program.

It is allowed to override the names of standard types in the program. Also the type can be accessed by using the module name System as shown below

System.LongInt
System.Real
System.Boolean
System.Char

User-defined types are programmer-defined types. These types are necessary to provide a more preferable solution to the task at hand. With the help of user-defined types in the program, you can more accurately describe the data required to solve the problem.

In Pascal, the following user-defined types are distinguished:

  • enumerated type;
  • interval type;
  • pointer types (except for the standard Pointer type);
  • structured types;
  • object types.

 

2. Classification of standard types

The names and the number of types change in different versions of Pascal. There are basic Pascal types and additional types added in new subsequent versions and varieties of this language: Delphi (Object Pascal), Lazarus, PascalABC.

The standard types of the Pascal language are:

  • integers ShortInt, Integer, LongInt, Byte, Word;
  • real types Single, Real, Double, Extended, Comp;
  • boolean types Boolean, ByteBool, WordBool, LongBool;
  • character type Char;
  • string types String, PChar;
  • pointer type Pointer;
  • text type named Text.

New standard types have been added to the Free Pascal language:

  • integers SmallInt, Cardinal, LongWord, Int64, QWord;
  • real type Currency;
  • character type WideChar.

The following types have been added to the Delphi language:

  • integers SmallInt, Cardinal, LongWord, Int64, UInt64, NativeInt, NativeUint;
  • real types Real48, Currency.

 

3. Ordinal types. Concepts. Characteristics of ordinal types. Ord, Pred, Succ functions

Ordinal data types include the following standard types:

  • integers ShortInt, Integer, LongInt, Byte, Word, SmallInt, Cardinal, LongWord, Int64, QWord;
  • boolean types Boolean, ByteBool, WordBool, LongBool;
  • character types Char, WideChar.

For ordinal types, you can define the following properties.

  • for each ordinal type, a set of admissible values is defined, which are an ordered sequence. Each element of the ordinal type has its own ordinal number, represented by an integer. The first value in ordinal types Byte, Word, Boolean, ByteBool, WordBool, LongBool has ordinal 0, followed by ordinal 1, etc. In ordinal types ShortInt, Integer, LongInt, the ordinal is equal to the value of these types;
  • for any ordinal type, the standard Ord function is applicable, which determines the ordinal number of this value;
  • for any ordinal type, the standard Pred function is applicable, which returns the previous value relative to a given value. If the Pred function is applied to the first valid value of an ordinal type (other than boolean types), then the ordinal of the last value is returned;
  • any ordinal type can be used in the standard Succ function that returns the value that follows the specified one. If this function is applied to the last valid value of an ordinal type (other than boolean types), then the ordinal of the first value is returned.

 

4. An example demonstrating the use of ordinal types in combination with the Pred, Succ functions

The example shows the use of the Pred, Succ functions for the standard types of classical Pascal. These functions can also be used for new types of Free Pascal language.

program TestOrdinalTypes;

uses
  crt;

var
  // Declaring variables of ordinal types
  // 1. Group of integer types
  i, pred_i, succ_i : Integer;
  bt, pred_bt, succ_bt : Byte;
  si, pred_si, succ_si : ShortInt;
  li, pred_li, succ_li : LongInt;
  w, pred_w, succ_w : Word;

  // 2. Boolean types
  b, pred_b, succ_b : Boolean;
  bb, pred_bb, succ_bb : ByteBool;
  wb, pred_wb, succ_wb : WordBool;
  lb, pred_lb, succ_lb : LongBool;

  // 3. Char type
  c, pred_c, succ_c : Char;

begin
  // 1. Group of integer types
  // 1.1. Тип Integer
  i:=25;
  pred_i := Pred(i); // 24
  succ_i := Succ(i); // 26
  Writeln('Integer: ', pred_i:6, i:6, succ_i:6);

  // 1.2. Byte type
  bt:=0;
  pred_bt := Pred(bt); // 255
  succ_bt := Succ(bt); // 1
  Writeln('Byte:   ', pred_bt:6, bt:6, succ_bt:6);

  // 1.3. ShortInt type
  si:=-1;
  pred_si := Pred(si); // -2
  succ_si := Succ(si); // 0
  Writeln('ShortInt:', pred_si:6, si:6, succ_si:6);

  // 1.4. LongInt type
  li:=0;
  pred_li := Pred(li);
  succ_li := Succ(li);
  Writeln('LongInt: ', pred_li:6, li:6, succ_li:6);

  // 1.5. Word type
  w := 0;
  pred_w := Pred(w); // 65535
  succ_w := Succ(w); // 1
  Writeln('Word:   ', pred_w:6, w:6, succ_w:6);

  // 2. Boolean types
  // 2.1. The Boolean type
  b:=True;
  pred_b := Pred(b); // False
  succ_b := Succ(b); // True
  Writeln('Boolean: ', pred_b:6, b:6, succ_b:6);

  // 2.2. WordBool type
  wb:=False;
  pred_wb := Pred(wb); // True
  succ_wb := Succ(wb); // True
  Writeln('WordBool:', pred_wb:6, wb:6, succ_wb:6);

  // 2.3. ByteBool type
  bb:=True;
  pred_bb := Pred(bb); // True
  succ_bb := Succ(bb); // False
  Writeln('ByteBool:', pred_bb:6, bb:6, succ_bb:6);

  // 2.4. The LongBool type
  lb:=False;
  pred_lb := Pred(lb); // True
  succ_lb := Succ(lb); // True
  Writeln('LongBool:', pred_lb:6, lb:6, succ_lb:6);

  // 3. Char type
  c:='D';
  pred_c := Pred(c);
  succ_c := Succ(c);
  Writeln('Char:   ', pred_c:6, c:6, succ_c:6);  
end.

Program result

Integer:     24   25     26
Byte:       255     0     1
ShortInt:   -2   -1     0
LongInt:     -1     0     1
Word:     65535     0     1
Boolean: FALSE TRUE   TRUE
WordBool: TRUE FALSE TRUE
ByteBool: TRUE TRUE FALSE
LongBool: TRUE FALSE TRUE
Char:         C     D     E

 

5. Integer types. Characteristics of integer Types in standard Pascal and Free Pascal

Table 1 displays the characteristics of integer types implemented in standard Pascal.

 

Type description

 

Designation in the program Range of numbers

Memory size

Signed short integer ShortInt -128 .. 127 1 byte
Signed integer Integer -32768 .. 32767 2 bytes
Signed long integer LongInt -2147483648..2147483647 4 bytes
Unsigned short integer Byte 0 .. 255 1 byte
Unsigned integer Word 0 .. 65535 2 bytes

Figure 1. Characteristics of integer types in Pascal

Important characteristics of a type include memory size in bytes and range of numbers. Memory size determines how many bytes will be allocated to store one variable or constant in memory. If you declare a variable of type LongInt, then 4 bytes of memory will be allocated to store it.

In Free Pascal, the list of integer types has been changed (see table 2), namely:

  • added additional types SmallInt, Cardinal, LongWord, Int64, QWord;
  • the Integer type can occupy memory of 2 or 4 bytes in order to ensure compatibility between 16-bit words and 32-bit words implemented in different generations of processors.

Table 2 contains additional integer types added in Free Pascal.

Designation in the program Range of numbers Memory size, bytes
SmallInt -32768 .. 32767 2
Cardinal

0..4294967295 same as LongWord

4
LongWord 0..4294967295 4
Int64 -9223372036854775808 .. 9223372036854775807 8
QWord or UInt64 0 .. 65535 8

Delphi adds the following integer types: SmallInt, Cardinal, LongWord, Int64, UInt64, NativeInt, NativeUint. These types have the same characteristics as the types of the same name from Table 2. The NativeInt type has the same characteristics as the LongInt type. The NativeUint type corresponds to the LongWord type.

 

6. An example of declaring and using variables of integer types

Below is a snippet of the declaration of variables and constants of standard Pascal integer types.

program TestOrdinalTypes;

uses
  crt;

// Declaring constants of integer types
const
  Max = 300;
  Min = 0;
  NumberDays = 7;

// Declaring variables of integer types
var
  s:ShortInt;
  i:Integer;
  l:LongInt;
  b:Byte;
  w:Word;

begin
  // Assigning values to variables of types ShortInt, Integer, LongInt
  s := -35;
  Writeln(s);

  i := 2021;
  Writeln(i);
  l := 3000000;
  Writeln(l);

  // An attempt was made to assign a value to a variable that exceeds its valid bounds.
  b:=2000; // b = 208 - overflow
  Writeln(b);

  // Assigning a value to a variable of type Word
  w := 65000;
  Writeln(w);

end.

Program result

-35
2021
3000000
208
65000

 

7. Determination of the minimum and maximum allowable values of integer types programmatically. Example

For some types, there are predefined constants that determine the minimum or maximum allowable value that a variable of a certain type can receive. The list of these constants may differ in different languages (Free Pascal, Delphi, PascalABC).

Below is an example of using the MaxInt and MaxLongInt constants in Delphi and Lazarus systems

// Print the maximum value of types Integer and LongInt
Writeln(System.MaxInt);     // 2147483647
Writeln(System.MaxLongint); // 2147483647

 

8. Determining the size of the type programmatically. Function SizeOf. Example

To get the size of a type occupied by a variable, use the SizeOf function. In general, a function call looks like this:

SizeOf(variableName_or_constantName);

or

SizeOf(typeName);

here

  • variableName_or_constantName – the name of a variable or constant for which you need to determine the size of the memory allocated for it;
  • typeName – the name of the type for which the size is determined.

Example.

program TestTypes;
uses
  crt;

// Declaring constants
const
  Pi = 3.1415;

// Declaring variables
var
  i:Integer;
  d:Double;
  qw:QWord;
  size:Integer;

begin
  // SizeOf function - determine the size of a variable or type
  // 1. Get the size of the variable i
  size := SizeOf(i); // size = 4
  Writeln('SizeOf(i) = ', size);

  // 2. Get the size of the variable qw
  size := SizeOf(qw); // size = 8
  Writeln('SizeOf(qw) = ', size);

  // 3. Get the size of type LongInt
  size := SizeOf(LongInt); // size = 4
  Writeln('SizeOf(LongInt) = ', size);

  // 4. Display dimensions of other types
  Writeln('SizeOf(Currency) = ', SizeOf(Currency)); // 8
  Writeln('SizeOf(SmallInt) = ', SizeOf(SmallInt)); // 2

  // 5. Get the size of the constant Pi
  Writeln('SizeOf(Pi) = ', SizeOf(Pi)); // 8

end.

Program result

SizeOf(i) = 4
SizeOf(qw) = 8
SizeOf(LongInt) = 4
SizeOf(Currency) = 8
SizeOf(SmallInt) = 2
SizeOf(Pi) = 8

 


Related topics