Types of variables in Pascal: description, properties, examples

In order for the machine to be able to process any input data, it must “understand” what type the variables in which the values ​​are entered belong. In the absence of information about the data format, the computer will not be able to determine whether a particular operation is acceptable 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 can be performed with each variable.

As in other high-level programming languages, the types of variables in Pascal are optimized for performing tasks of various kinds, have a different range of values ​​and length in bytes.

Variable Type Division

Types of variables in Pascal are divided into simple and structured. Simple include real and ordinal types. Arrays, records, sets, and files are structured. Separately allocated pointers, objects and procedural types.

types of variables in pascal
Consider ordinal and real types. The ordinal numbers include 5 integer types, logical, character, enumerated, and range-type.

Ordinal types

There are 5 integer types that differ in byte length and range of values.

The length of Byte and ShortInt is 1 byte. The difference between them is that Byte only stores non-negative values, and ShortInt allows you to store negative values ​​(from -128 to +127). Word and Integer types are similarly related to each other, 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 numerical dimension it is 2 to the 16th power on both sides of zero. Various types of variables in Pascal contribute to the effective solution of user problems, since in each case both a small and a large range of values ​​may be required, and there may be some restrictions on the amount of allocated memory.

Pascal string variables
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 more than positive: for example, from -128 to +127.

Boolean variables can be TRUE or FALSE and require 1 byte of memory.

Type CHAR allows you to store any of the many characters that exist in the computer's memory. Moreover, in symbolic variables in Pascal, only the sign code is actually stored, in accordance with which its graphic form is displayed.

Real types

Among the types of variables in Pascal, there are several numerical ones with the ability to record the fractional part. The difference between the types Single, Real, Double and Extended comes down to the range of accepted values, the number of significant digits after the decimal point, and the size in bytes.

In accordance with 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 series of simple values ​​into one variable. A vivid example is an array that can be defined as follows:

Type

String = array [1..100] of char;

Var Y = String;

Thus, we got a type called String, which allows us to set variables 100 characters long. The last line contains a directly one-dimensional array Y of type String. The description of variables in Pascal is carried out by placing the identifier on the left side, and on the right, after the equal sign, the variable value.

character variables in pascal
The range of indices written in square brackets allows you to access each specific element of the array:

readln (Y [2]);

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

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

Posts

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

type NTel = Record

NAME: String [12];

NUMBER: String [10]

end;

var one: NTel;

The first line on the left indicates the type name, 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 that we wanted, and this is the end of the process of creating a record.

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

You can refer to the record as a whole, as well as to its individual components, for example: one.NAME (i.e., variable_name.name_field_name).

Files

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

description of variables in pascal

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

'File1.DAT'

'C: \ Folder \ File2.txt'

The short form is used if the file is placed in the folder where the program itself is accessed. The full form can be used in any circumstances.

You can set a file type variable as follows:

var

f1: file of integer;

kinds of variables in pascal
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, close it at the end of work, allow you to create a new name and delete the file from the computer.

Finally

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

Source: https://habr.com/ru/post/K12569/


All Articles