What is a type in Pascal. Types of variables in Pascal: description, properties, examples

Basics of programming
Every professional was once a teapot. Surely you are familiar with the state when “you don’t know how to start thinking in order to come up with such a thing.” You've probably encountered a situation where you just don't know where to start. This book is aimed precisely at such people who would like to become a programmer, but have absolutely no idea how to start this path. ...

Almost all integer data types are . These data types represent integers within a certain range. The specific names of integer types and ranges of values ​​depend on the specific programming language, the compiler, and the compilation mode. You can learn more about this in the compiler documentation.

For example, data type Integer in Delphi it has a range of -2147483648…2147483647, while in Turbo Pascal the data type Integer represents numbers in the range -35768…32767. In Free Pascal, the range of values ​​of type Integer determined by the selected mode.

Since Lazarus uses the Free Pascal compiler, everything said about data types in relation to Free Pascal is also true for Lazarus.

So, Free Pascal's integer data types are listed in Table 13.1.

Table 13.1. Free Pascal Integer Data Types (Lazarus).

Type Size, bytes Range of values
Byte 1 0…255
Shortint 1 -128…127
Smallint 2 -35768…32767
Word 2 0…65535
Integer 2 or 4 Depends on compilation mode
Cardinal 4 0…4294967295
Longint 4 -2147483648…2147483647
Longword 4 0...4294967295
Int64 8 -9223372036854775808...9223372036854775807
QWord 8 0...18446744073709551615

NOTE
Types in Free Pascal Int64 And QWord are not! This means that you cannot use them, for example, for index variables in loops. However, I presented them here so as not to describe them separately in the future and to collect all Free Pascal integer types in one place. If you don’t understand some words, don’t be alarmed. In due time I will tell you about everything in more detail.

And now a few explanations about the table.

In a collumn TYPE data type identifiers are given (keywords that indicate to the compiler what type a particular data belongs to). You will learn how to use these identifiers in the following lessons.

In a collumn SIZE indicates the size that the data type occupies in computer memory. For example, a positive integer can be represented by different types: Byte, Word, Cardinal etc. However, a number like Cardinal will occupy 4 bytes in memory, while a number like Byte– only 1 byte. Therefore, if you know for sure that the number you are working with will never take a value greater than 255, then it is better to define it as a type Byte, as this will save space in your computer's memory. Although not everything is so simple here (the nuances of the distribution of memory and other computer resources are beyond the scope).

In a collumn RANGE specifies the range of values ​​that the data type operates on. For example, a number like Byte can take values ​​from 0 to 255.

Now for practice. Let's write a program that displays the ranges of values ​​of all integer data types. The source code for this program is given below:

Listing 13.1. A program for displaying ranges of integers. program td; ($mode objfpc)($H+) uses ($IFDEF UNIX)($IFDEF UseCThreads) cthreads, ($ENDIF)($ENDIF) Classes ( you can add units after this ); begin Writeln("Byte: ", Low(Byte), "..", High(Byte)); Writeln("Shortint: ", Low(Shortint), "..", High(Shortint)); Writeln("Smallint: ", Low(Smallint), "..", High(Smallint)); Writeln("Word: ", Low(Word), "..", High(Word)); Writeln("Integer: ", Low(Integer), "..", High(Integer)); Writeln("Cardinal: ", Low(Cardinal), "..", High(Cardinal)); Writeln("Longint: ", Low(Longint), "..", High(Longint)); Writeln("Longword: ", Low(Longword), "..", High(Longword)); Writeln("Int64: ", Low(Int64), "..", High(Int64)); Writeln("QWord: ", Low(QWord), "..", High(QWord)); Readln; end.

Standard function Low defines the minimum value of the data type. Funtskia High defines the maximum value. With functions WriteLn And ReadLn you already know each other a little. We will talk in more detail about subroutines (procedures and functions) in the corresponding section.

Finally, I’ll tell you how integer data is written in the program. Yes, just like everywhere else - just write the number, without quotes or any additional symbols. For example, like this

10
178
35278

True, this applies to numbers in the decimal number system. Surely you already know that there are other systems. The most widely used number systems are binary, decimal and hexadecimal.

Free Pascal supports four integer formats:

  1. Decimal notation. Just a number, like 10.
  2. Hexadecimal notation. A number prefixed with $. For example, the hexadecimal number $10 is equal to the decimal number 16.
  3. Octal notation. A number prefixed with &. For example, octal &10 is equal to decimal 8.
  4. Binary notation. A number prefixed with %. For example, the binary number %10 is equal to the decimal number 2.

Homework:

Create a program that displays ranges of integer values ​​(Listing 13.1). Compile the program and run it. Make sure that these values ​​correspond to those shown in Table 13.1.

In the source code of the program, find the line that sets the compilation mode:

($mode objfpc)($H+)

In this line, instead of the word objfpc write the word tp. That is, the final line should look like this:

($mode tp)($H+)

Launch the program. Look at the range of type values Integer. Draw conclusions.

Learn to think like a programmer, that is, logically. No one will chew everything on you until retirement, as I do now. You have to get used to thinking for yourself. Otherwise, you will slip into the “monkey learning principle,” and then your chances of becoming a great programmer will be close to zero. To help you not slide into the “cramming” level, I will periodically leave gaps in your learning so that you try to figure out some things yourself.

It's much better if you figure it out yourself wrong decision, you will find the mistake yourself and correct it yourself, rather than always using other people’s correct solutions and stupidly copying them.

Pascal language data types: classification and descriptions. Arithmetic and ordinal data types, actions with them. Arithmetic expressions: functions, operations and order of actions. Data type compatibility and conversions.

Pascal compilers require that information about the amount of memory needed to run a program be provided before it runs. To do this, in the variable description section ( var) you need to list all the variables used in the program. In addition, you also need to tell the compiler how much memory each of these variables will occupy. It would also be nice to agree in advance on the various operations applicable to certain variables...

All this can be communicated to the program by simply indicating the type of the future variable. Having information about the type of a variable, the compiler “understands” how many bytes need to be allocated for it, what actions can be performed with it, and in what constructions it can participate.

For the convenience of programmers, Pascal has many standard data types, plus the ability to create new types.

When constructing new types of data based on existing ones (standard or again defined by the programmer himself), we must remember that any building must be built on a good foundation. Therefore, now we will talk about this “foundation”.

Based basic data types all other types of the Pascal language are built, which are called: constructed.

The division into basic and constructed data types in Pascal is shown in the table:

Programmer-constructed data types are described in section type according to the following template:

type<имя_типа> = <описание_типа>;

For example:

type Lat_Bukvy = "a" .. "z", "A" .. "Z";

The basic data types are standard, so there is no need to describe them in the section type. However, if desired, this can also be done, for example, by giving long definitions short names. Let's say by introducing a new data type

type Int = Integer;

You can shorten the program text a little.

Standard constructed types also need not be described in the section type. However, in some cases this still has to be done due to syntax requirements. For example, in parameter list procedures or functions Type constructors cannot be used (see lecture 8).

Ordinal data types

Among the basic data types, the most notable ones are ordinal types. This name can be justified in two ways:

Standard routines that process ordinal data types

Only for quantities ordinal types The following functions and procedures are defined:

  1. Function Ord(x) returns the ordinal number of the value of the variable x (relative to the type to which the variable x belongs).
  2. Function Pred(x) returns the value preceding x (not applicable to the first element of the type).
  3. Function Succ(x) returns the value following x (not applicable to the last element of the type).
  4. Procedure Inc(x) returns the value following x (for arithmetic data types this is equivalent to the operator x:= x + 1).
  5. Procedure Inc(x, k) returns the kth value following x (for arithmetic data types this is equivalent to the operator x:= x + k).
  6. Procedure Dec(x) returns the value preceding x (for arithmetic data types this is equivalent to the operator x:= x - 1).
  7. Procedure Dec(x, k) returns the k–e value preceding x (for arithmetic data types this is equivalent to the operator x:= x - k).

At first glance, it seems as if the result of applying the procedure Inc(x) completely coincides with the result of applying the function Succ(x). However, the difference between them appears at the boundaries of the permissible range. Function Succ(x) is not applicable to the maximum element of a type, but here is the procedure Inc(x) will not produce any error, but, acting according to the rules of machine addition, will add the next unit to the element number. The number, of course, will go beyond the range and, due to truncation, will turn into the number of the minimum value of the range. It turns out that the procedures Inc() And Dec() they perceive any ordinal type as if “closed in a ring”: immediately after the last value comes the first value again.

Let us explain everything that has been said with an example. For data type

type Sixteen = 0 .. 15 ;

trying to add 1 to the number 15 will result in the following result:

1 1 1 1 1 1 0 0 0 0

The initial unit will be cut off, and therefore it turns out that Inc(15)=0 .

A similar situation at the lower limit of the permissible range of an arbitrary ordinal data type is observed for the procedure Dec(x) and functions Pred(x):

Data types related to ordinal

Let us now describe ordinal data types in details.

  1. Boolean type Boolean has two values: False and True, and the following equalities hold for them:
  2. To character type Char includes 256 characters extended ASCII table(for example, "a", "b", "i", "7", "#"). Character number returned by the function Ord() , matches the number of this symbol in ASCII table.
  3. Integer data types Let's put it in a table:
  4. Listable data types are specified in the section type by explicitly listing their elements. For example:

    type Week = (sun, mon, tue, wed, thu, fri, sat); 0 1 2 3 4 5 6

    Recall that for this data type:

  5. Interval data types are specified only by the boundaries of their range. For example:

    type Month = 1 .. 12 ;
    Budni = Mon .. Fri;

  6. The programmer can create his own data types, which are a combination of several standard types. For example:

    type Valid_For_Identifiers = "a" .. "z" , "A" .. "Z" , "_" , "0" .. "9" ;

This type consists of combining several intervals, and in this case the order of Latin letters is changed: if in the standard type

In order for a machine to process any input data, it must “understand” what type the variables in which the values ​​are stored belong. In the absence of information about the data format, the computer will not be able to determine whether a particular operation is permissible in a particular case: for example, it is intuitively clear that it is impossible to raise a letter to a power or take the integral of a string. Thus, the user must determine what actions are allowed with each variable.

As in other high-level programming languages, variable types in Pascal are optimized to perform tasks of various types, have a different range of values ​​and length in bytes.

Subdivision of variable types

Variable types in Pascal are divided into simple and structured. Simple types include real and ordinal types. Structured ones include arrays, records, sets and files. Separately, pointers, objects and procedural types are distinguished.

Let's look at ordinal and real types. Ordinal types include 5 integer types, enumeration type and range type.

Ordinal types

There are 5 integer types, differing in byte length and range of values.

The length of Byte and ShortInt is 1 byte. The difference between them is that Byte stores only non-negative values, while ShortInt allows you to store negative ones (from -128 to +127). The Word and Integer types relate to each other in a similar way, with the only difference being that their size is 2 bytes.

Finally, LongInt allows you to store both negative and positive values ​​using 4 bytes - in the 16th power of numeric dimension on either side of zero. Various types of variables in Pascal contribute to the efficient solution of user tasks, since in each specific case both a small and a large range of values ​​may be required, and there may also be restrictions on the amount of memory allocated.

It is important to understand that zero takes up as much memory space as any other number. Thus, when forming a range of values, the minimum negative number in absolute value will be one greater than a positive one: for example, from -128 to +127.

Variables belonging to can take the value TRUE (true) or FALSE (false) and require 1 byte of memory.

The CHAR type allows you to store any of the many characters that exist in computer memory. At the same time, in symbolic variables in Pascal, only the code of the sign is actually stored, in accordance with which its graphic form is displayed.

Real types

Among the types of variables in Pascal, there are several numeric ones with the ability to write a fractional part. The differences between Single, Real, Double, and Extended types come down to the range of values ​​accepted, the number of significant digits after the decimal point, and the size in bytes.

According to the order presented above, a variable of each type will occupy 4, 6, 8 or 10 bytes.

Arrays

Structured data types are complex and allow you to combine a number of simple values ​​within a single variable. A prime example is an array, which can be defined as follows:

String=array of char;

Thus, we got a type called String, which allows us to define variables up to 100 characters long. The last line directly specifies a one-dimensional array Y of type String. Variables in Pascal are described by placing the identifier on the left side and the variable value on the right, after the equal sign.

The range of indices written in allows access to each specific element of the array:

In this case, we read the second element of the previously created array Y.

String variables in Pascal are also a special case of a one-dimensional array, because a string is a sequence of characters, i.e. elements of the char type.

Posts

A record consists of several fields filled with data of any type except file. In general, this type of variable is similar to a database element. For example, you can enter a person’s name and phone number:

type NTel = Record

The first line on the left indicates the name of the type, and on the right - the service word record. The second line contains a field with a name, the third - a phone number. The word “end” means that we have entered all the fields we wanted, and this completes the process of creating a record.

Finally, in the last line we set the variable One, which is of type NTel.

You can access both the record as a whole and its individual components, for example: one.NAME (i.e. variable_name. record_field_name).

Files

Pascal allows you to work with text, typed and untyped files, which are a structured sequence of components that have the same type.

When reading from or writing to a file, either the full address or its short form can be used:

‘C:\Folder\File2.txt’

The short form is used when the file is placed in the folder where the program that accesses it is stored. The full form can be used in any circumstance.

You can set a file type variable as follows:

f1: file of integer;

To work with files, various functions and procedures are used that associate a variable with a file on disk, open it for reading, writing and overwriting, closing it when finished, allowing you to create a new name and deleting the file from the computer.

Finally

Without the ability to use various types of variables in Pascal, the user will not be able to implement even the simplest task. In order for the program to execute the algorithm without errors, it is necessary to learn both function words and syntax, since the machine can “understand” commands only if they are written in the only correct way.

Any program written in any programming language is essentially designed to process data. The data can be numbers, texts, graphics, sound, etc. Some data are source data, others are the result, which is obtained by processing the source data by the program.

The data is stored in the computer's memory. The program accesses them using variable names associated with the memory locations where the data is stored.

Variables are described before the main program code. The names of the variables and the type of data stored in them are indicated here.

There are a lot of data types in the Pascal programming language. In addition, the user himself can define his own types.

The type of a variable determines what data can be stored in the memory location associated with it.

Type variables integer can only be associated with integer values ​​typically in the range -32768 to 32767. Pascal has other integer types (byte, longint).

Type variables real store real (fractional) numbers.

Variable Boolean(Boolean) type (boolean) can take only two values ​​- true(1, true) or false(0, false).

Character type (char) can take values ​​from a specific ordered sequence of characters.

Interval type defined by the user and formed only from ordinal types. Represents a subset of values ​​in a specific range.

You can create your own data type by simply listing the values ​​that a variable of that type can take. This is the so-called enumerated data type.

All of the above are simple data types. But there are also complex, structured ones, which are based on simple types.

Array is a structure that occupies a single area in memory and consists of a fixed number of components of the same type.

Strings is a sequence of characters. Moreover, the number of these characters cannot be more than 255 inclusive. This limitation is a characteristic feature of Pascal.

Record is a structure consisting of a fixed number of components called fields. Data in different fields of a record can be of different types.

Sets represent a collection of any number of elements, but of the same enumerated type.

Files for Pascal, they are sequences of the same type of data that are stored on external memory devices (for example, a hard drive).

The concept of such a data type as pointer associated with dynamic storage of data in computer memory. Often, using dynamic data types is more efficient in programming than using static data types.

3.2. Simple data types in Turbo Pascal 7

A simple type defines an ordered set of parameter values. Turbo Pascal has the following groups of simple types:

  • integer types;
  • boolean type;
  • character type;
  • enumerated type;
  • type-range;
  • real types.

All simple types, with the exception of real types, are called ordinal types. For quantities of ordinal types, standard procedures and functions are defined: Dec, Inc, Ord, Pred, Succ (see section 13.1).

3.2.1. Integer types

Unlike Pascal, which defines a single integer type, Integer, Turbo Pascal has five standard integer types: Shortint, Integer, Longint, Byte, Word. The characteristics of these types are given in table. 2.

Table 2. Integer data types

Type Range Format Size in bytes
Shortint -128 .. 127 Iconic 1
Integer -32768 .. 32767 Iconic 2
Longint -2147483648 .. 2147483647 Iconic 4
Byte 0 .. 255 Unsigned 1
Word 0 .. 65535 Unsigned 2

3.2.2. Boolean type

The standard Boolean type (size - 1 byte) is a data type, any element of which can take only two values: True and False. In this case, the following conditions are valid:
False Ord (False) = 0
Ord (True) = 1
Succ (False) = True
Pred (True) = False

Turbo Pascal 7.0 added three more logical types ByteBool (size - 1 byte), WordBool (size - 2 bytes) and LongBool (size - 4 bytes). They were introduced for unification with other programming languages ​​and with the Windows environment. Their difference from the standard Boolean type is the actual value of the parameter of this type, corresponding to the value True. For all logical types, the value False corresponds to the number 0, written in the corresponding number of bytes. The value True for the Boolean type corresponds to the number 1 written in its byte, and for other types the value True corresponds to any number other than zero (although the Ord function in this case gives the value 1).

3.2.3. Character type

The standard character type Char defines a full set of ASCII characters. The Ord function from a Char type value gives the code of the corresponding character. Values ​​of character type are compared according to their codes.

3.2.4. Enum type

An enumerated type is not standard and is defined by a set of identifiers that parameter values ​​can match. The list of identifiers is indicated in parentheses, identifiers are separated by commas:

type
= ();)

It is important in what order the identifiers are listed when defining a type, because the first identifier is assigned the serial number 0, the second - 1, etc. The same identifier can be used in the definition of only one enumerated type. The Ord function from a value of an enumerated type gives the ordinal number of its value.

Example. Enumerated type.

type Operat = (Plus, Minus, Mult, Divide);

The boolean type is a special case of the enumerated type:

type Boolean = (False, True);

3.2.5. Type-range

In any ordinal type, you can select a subset of values, defined by the minimum and maximum values, which includes all the values ​​of the original type that are within these boundaries, including the boundaries themselves. This subset defines a range type. The range type is specified by specifying the minimum and maximum values, separated by two dots:

type = . . ;

The minimum value when defining this type should not be greater than the maximum.

Example. Definition of range types.

type
Dozen = 1..12; (numbers from 1 to 12)
AddSub = Plus..Minus; (addition and subtraction operations)

3.2.6. Real types

Unlike the Pascal language standard, where only one real type Real is defined, Turbo Pascal has five standard real types: Real, Single, Double, Extended, Comp. For the characteristics of these types, see table. 3. Table 3. Real data types

Type Range Number of significant figures Size in bytes
Real 2.9*10-39..1.7*1038 11-12 6
Single 1.5*10-45..3.4*1038 7-8 4
Double 5.0*10-324.-1.7*10308 15-16 8
Extended 3.4*10-4932..1.1*104932 19-20 10
Comp -263+1..263-1 19-20 8

The Comp type is actually an extended range integer type, but is not considered an ordinal type.

The Single, Double, Extended and Comp types can be used in programs only if there is an arithmetic coprocessor or if the coprocessor emulator is enabled (see paragraphs 17.5.8 and 17.7.1).