Type in Pascal examples. Abstract: Data types in Pascal

In order for a machine to be able 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 is specified directly one-dimensional array Y, which is 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.

In conclusion

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.

Most common in mathematics numeric types- This whole numbers that represent an infinite number of discrete values, and valid numbers that represent an unlimited continuum of values.

Description of Pascal's numeric data types (integers)

Within the same language, different subsets of the set of integers can be implemented. The range of possible values ​​of integer numeric types depends on their internal representation, which can be one, two, or four bytes. Thus, in Pascal 7.0 the following integer numeric data types are used:

With whole numeric data types Pascal can perform the following operations:

  • Arithmetic:
    addition(+);
    subtraction(-);
    multiplication(*);
    remainder of division (mod);
    exponentiation;
    unary plus (+);
    unary minus (-).
  • Relation operations:
    equality relation (=);
    inequality relation (<>);
    ratio is less (<);
    ratio greater than (>);
    relation not less than (>=);
    attitude no more (<=).

When acting with integer numeric data types the type of the result will correspond to the type of the operands, and if the operands are of different integer types, to the type of the operand that has the maximum power (maximum range of values). Possible overflow of the result is not controlled in any way (this is important!) , which may lead to errors.

Particular attention should be paid to the division operation of integer numeric data types. Pascal allows two division operations, which are denoted accordingly "/" And div. You need to know that the result of division "/" is not an integer, but real number(this is true even if you divide 8 by 2, i.e. 8/2=4.0). The division div is integer division, i.e. result type is integer.

Description of Pascal's numeric data types (reals)

The real numeric data type refers to a subset of real numbers that can be represented in the so-called floating point format with a fixed number of digits. With floating point, each numeric data type is represented as two groups of digits. The first group of numbers is called the mantissa, the second is the exponent. In general, a numeric data type in floating point form can be represented as follows: X= (+|-)MP (+ | -) r, where M is the mantissa of the number; r – number order (r – integer); P – base of the number system. For example, for the decimal base, the representation 2E-1 (here E is the base of the decimal number system) will look like: 2*10 -1 =0.2, and the representation 1.234E5 will correspond to: 1.234*10 5 =123400.0.

Pascal uses the following types of real numbers, which define an arbitrary number only with some finite precision depending on the internal format of the real number:

When describing a real variable of type real, a variable of 4 bytes will be created in the computer memory. In this case, 3 bytes will be given for the mantissa, and one byte for the order.

The following operations can be performed on real numeric data types:

  • Arithmetic:
    addition (+);
    subtraction(-);
    multiplication(*);
    division(/);
    exponentiation;
    unary plus (+);
    unary minus (-).
  • Relation operations:
    inequality relation (<>);
    ratio is less (<);
    ratio greater than (>);
    relation not less than (>=);
    attitude no more (<=).

As you can see, Pascal is characterized by a rich range of real types, but access to numeric data types single, double And extended only possible under special compilation modes. These numeric data types are designed for hardware support for floating-point arithmetic, and to use them effectively, your PC must include a math coprocessor.

The numeric data type occupies a special position in Pascal. comp, which is treated as a real number without exponential and fractional parts. Actually, comp is a “large” signed integer that retains 19..20 significant decimal digits. At the same time, the numeric data type comp in expressions it is fully compatible with other real types: all real operations are defined on it, it can be used as an argument to mathematical functions, etc.

About converting Pascal's numeric data types

In Pascal, implicit (automatic) conversions of numeric data types are almost impossible. An exception is made only for the type integer, which is allowed to be used in expressions like real. For example, if the variables are declared like this:

Var X: integer; Y: real;

That's the operator

will be syntactically correct, although there is an integer expression to the right of the assignment sign and a real variable to the left, the compiler will convert numeric data types automatically. The reverse conversion is automatically type real in type integer impossible in Pascal. Let's remember how many bytes are allocated for type variables integer And real: under integer type data integer 2 bytes of memory are allocated, and for real - 6 bytes. To convert real V integer There are two built-in functions: round(x) rounds real x to the nearest integer, trunc(x) truncates a real number by discarding the fractional part.

The most important elements of a program are variables. They influence the course of events in the program during its execution. For example, if we had not specified the value of the Name variable in , who would the greeting output from the program be addressed to?

Variables can contain completely different data. For example, one variable might store someone’s name, another might store their year of birth, a third might store their height, etc. Such different data is represented by the computer in different ways. The name is a string of characters, the year of birth is an integer, and the height is a real number (for example, height is 1.72 m).

The way a computer represents data is determined by its type. In addition, the data type determines what actions are allowed to be performed on that data.

The main standard data types of the Turbo Pascal language are listed below:

  1. INTEGER– integer data in the range from –32768 to 32767, occupying two bytes in memory;
  2. REAL– real numbers in the range from 2.9´10 -39 (2.9E-39) to 1.7´10 38 (1.7E38), occupy six bytes;
  3. CHAR– separate character, one byte;
  4. STRING– a string of characters, the number of characters in the line (string length) is limited by the number N in square brackets, occupies N+1 bytes (if the number N is not specified, then the maximum length of the line is 255 characters);
  5. BOOLEAN– logical type, has two values: FALSE (false) and TRUE (true), one byte.

Note that the types INTEGER, CHAR, and BOOLEAN are classified as ordinal ordinal types.

As you probably remember, when describing a variable, a colon is placed after its name, and then the type is indicated. If several variables have the same type, their names can be separated by commas.

An example of describing variables of various types:

Delphi/Pascal

var a, b, c: integer; sum: real; Alpha, Beta: char; S:string; S_1: string; t: boolean;

a, b, c: integer;

sum: real;

Alpha, Beta: char;

S: string[25];

S_1: string;

t: boolean;

Note that the variable S_1 is a character string, but its declaration does not specify a length. In this case, the compiler itself sets the maximum possible length - 255 characters.

There are other predefined data types for storing integers and real numbers. Their characteristics are given in the tables below. Compare these types with the INTEGER and REAL types also shown in the tables.

Range

Size in bytes

SHORTINT
INTEGER
LONGINT

2147483648 .. 2147483647

BYTE
WORD

Real data types

Range

Number of significant figures

Size in bytes

REAL

2.9´10 -39 .. 1.7´10 3 8

SINGLE

1.5´10 – 45 .. 3.4´10 3 8

DOUBLE

5.0´10 -3 24 .. 1.7´10 3 08

EXTENDED

3.4´10 -4932 .. 1.1´10 49 32

COMP

2 63 +1 .. 2 63 -1

What data type to use

There are so many different types, you might say, so which one should you use?
It depends on the task assigned to you. For example, you need a variable in which you will store the height of a certain person (real value): in this case, it is enough to use the SINGLE type. If you use some variable to count the number of certain objects (a positive integer value), then figure out whether this number can be greater than 255, if not, use BYTE, if it can, you cannot do without WORD, and in some cases LONGINT may also be needed.

To learn more about the different types, press Shift+F1 in Turbo Pascal (a help index window will appear), and then select the object you are interested in (for example, type 'type' or 'real').

Any data - constants, variables, function values ​​- are characterized in Pascal by a data type.

Let's define the concept data type. As is already known, all program objects (variables, constants, etc.) must be described.

Descriptions inform the translator, firstly, about the existence of the variables and other objects used, and secondly, they indicate the properties of these objects. For example, a description of a variable whose value is a number indicates the properties of numbers. Formally, numbers can be integer and real (fractional). In Pascal, as in other programming languages, numbers are divided into two types: whole(reserved word integer) and real(reserved word real).

The separation of integers into a separate type is explained by the fact that in a computer, integer and real numbers are represented differently: an integer can be represented absolutely accurately, but a real number inevitably has some finite error, which is determined by the properties of the translator.

For example, let the variable x be of type real and its value equal to one: x=1 . The corresponding value in computer memory can be 0.999999999, 1.000000001, or 1.000000000. But if the variable x is declared as a variable of integer type, then the unit in the computer will be represented absolutely precisely and the variable x will not be able to take real (fractional) values ​​- after all, it was described as a variable of the integer type.

So the data type defines:

  • internal representation of data in computer memory;
  • the set of values ​​that quantities of this type can take;
  • operations that can be performed on values ​​of this type.

Introduction of data types is one of the basic concepts of the Pascal language, which is that when performing an operation of assigning a variable to the value of an expression, the variable and the expression must be of the same type. This check is performed by the compiler, which greatly simplifies the search for errors and leads to increased program reliability.

The many data types of the Turbo Pascal language can be divided into two groups:

  • standard (predefined) types ;
  • user-defined types (user-defined types) .

Standard Turbo Pascal types include:

  • integer type – integer;
  • real type – real;
  • character type – char;
  • boolean type – boolean;
  • string type – string ;
  • pointer type – pointer;
  • text type – text .

Custom data types are various combinations of standard types.

Custom types include:

  • enumerated type;
  • interval type;
  • pointer type;
  • structured types;
  • procedural type.

Comment. Another classification of data types is possible, according to which types are divided into simple and complex.

Simple types include: integer type, real type, character type, logical type, enumerated type and interval type.

Complex type represents various combinations simple types(arrays, records, sets, files, etc.)

Standard types

The standard data type is defined by the Pascal language itself. When using standard types in a program, it is enough to indicate the subsections of the required types (const, var) and then describe the constants and variables used in the program. There is no need to use the Type subsection.

For example, if the program uses only variables:

i,j – integer (integers);

x,y - real (real);

t,s - char (character);

a,b – boolean (logical),

then only a subsection of variables is needed - Var. Therefore, in the descriptive part of the program, variable declarations are written as follows:

Integer types

Data of this type can only accept integer values. In a computer, values ​​of an integer type are represented absolutely precisely. If the variable is negative, then it must be preceded by a “–” sign; if the variable is positive, then the “+” sign can be omitted. This type is necessary in the case when some quantity cannot be represented approximately as a real number. For example, the number of people, animals, etc.

Examples of writing integer values: 17, 0, 44789, -4, -127.

The range of variation of data of an integer type is determined by five standard types of integers and is presented in the table:

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

The last two types represent only positive numbers, and the first three represent both positive and negative numbers.

In the text of the program or when entering data of an integer type, the values ​​are written without decimal point . Actual variable values must not exceed permissible values the type (Shortint, Integer, Longint, Byte, Word) that was used to describe the variable. Possible excesses during calculations are not controlled in any way, which will lead to incorrect operation of the program.

An example of using an integer variable

Var a:integer; b:word; c:byte; Begin a:=300; (a is assigned the value 300) b:=300; (b set to 300) c:=200; (c is set to 200) a:=b+c; (a is set to 500) c:=b; (Error! Variable c can only take values ​​of 255. Here variable c is set to 500, which will cause the result to overflow.) End.

Real types

Values ​​of real types are represented approximately in a computer. The range of variation of real type data is determined by five standard types: real (Real), single precision (Single), double precision (Double), extended precision (Extended), complex (Comp) and is presented in the table:

Type Range Number of significant figures Size in bytes
Real 2.9E-39...1.7E+38 11-12 6
Single 1.5E-45...3.4E+38 >7-8 4
Double 5E-324...1.7E+308 15-16 8
Extended 3.4E-4951...1.1E+4932 19-20 10
Comp -2E+63+1...+2E+63-1 19-20 8

Real numbers can be represented in two formats: fixed point and floating point.

The format for writing a fixed-point number is the same as regular mathematical notation decimal number With fractional part. The fractional part is separated from the whole part using a dot, for example

34.5, -4.0, 77.001, 100.56

The floating point notation format is used when writing very large or very small numbers. In this format, the number before the "E" is multiplied by the number 10 to the power after the "E".

1E-4 1*10-4
3.4574E+3 3.4574*10+3
4.51E+1 4.51*10+1

Examples of floating point numbers:

Number Recording in Pascal
0,0001 1E-4
3457,4 34574E-1
45,1 451E-1
40000 4E+4
124 0.124E+3
124 1.24E+2
124 12.4E+1
124 1240E-1
124 12400E-2

The table from 5 to 9 lines shows a recording of the same number 124. By changing the position of the decimal point in the mantissa (the point “floats”, hence the name “recording a floating point number”) and at the same time changing the order value, you can select the most suitable recording numbers.

An example of describing variables of real type.

Character type

Character values ​​are characters that can be typed on a computer keyboard. This allows you to present text in the program and perform various operations on it: insert, delete individual letters and words, format, etc.

A character type is designated by the reserved word Char and is designed to store a single character. Character data takes up one byte in memory.

Symbolic variable declaration format:

<имя переменной>: Char;

When defining the value of a character variable, the character is written in apostrophes. In addition, you can set the required symbol by specifying it directly numerical value ASCII code. In this case, you must precede the number indicating the ASCII code of the required character with a # sign.

An example of using character type variables:

Var c:char; (c is a character type variable) Begin c:=’A’; (variable c is assigned the character 'A') c:=#65; (variable c is also assigned the character A. Its ASCII code is 65) c:=’5’; (variable c is assigned the symbol 5, End. Here 5 is no longer a number)

Boolean type

The logical data type is called Boolean after the English mathematician George Boole, the creator of the field of mathematics - mathematical logic.

Format for declaring a Boolean type variable:

<имя переменной>: boolean;

Data of this type can take only two values:

  • True - truth;
  • False is a lie.

Logical data is widely used in checking the validity of certain conditions and in comparing quantities. The result may be true or false.

To compare data, the following relational operations are provided:

An example of using relational operations:

relation 5>3, result true;

relation 5=3, result false.

An example of using Boolean type variables.

Var a,b:boolean; (a,b are variables of logical type) Begin a:=True; (variable a is set to true) b:=false; (variable b is set to false) End.

Constants

Integers, real numbers, characters, character strings, and logical constants can be used as constants.

A constant must be declared in its descriptive part using the reserved word const.

Constant Declaration Format

Const<имя константы>= <значение>;

If a program uses several constants, only one is allowed. keyword Const , the description of each constant ends with a semicolon. A constant block ends with the declaration of another section or the declaration of a block of executable statements.

Const (constant section declaration) year=2003; (constant of integer type, because there is no decimal point in the record) time=14.05; (real type constant) N=24; (constant of integer type, because there is no decimal point in the notation) P=3.14; (real type constant) A=true; (boolean constant) str1=’7’; (character type constant) str2=’A’; (character type constant) str3=’Turbo’; (string type constant) Var (variable section declaration) X,y:integer; (integer type variables)

Custom types

Of the set of user types, we will consider only

  • enumerated type;
  • interval type.

We will need these two types when studying arrays.

Enum type

An enumerated data type describes new data types whose values ​​are defined by the programmer. An enumerated type is specified by an enumeration of the values ​​it can receive. Each value is named by some identifier and is located in a list surrounded by parentheses. An enumeration type is a user-defined data type, so its type declaration begins with the reserved word TYPE.

Enum type format:

<имя типа>= (constant1, constant2,..., constantN);

Where
constant1 , constant2 ,..., constantN – an ordered set of identifier values ​​treated as constants.

An example of an enumerated type description:

Type ball=(one, two, three, four, five); var t:ball;

Here ball is the name of the enumerated type; one, two, three, four, five – constants; t is a variable that can take any constant value.

In an enumerated type, a constant is an identifier, so it is not quoted and cannot be a number. Thus, in an enumerated type, a constant is a special kind of constant that cannot be:

  • constants of numeric type: 1, 2, 3, 4, etc.;
  • constants of character type: "a", "s", "1", "3", etc.;
  • constants of string type: "first", "second", etc.

In addition, arithmetic operations and standard input and output procedures Read and Write are not applicable to values ​​of this type.

An example of using enumerated variables:

Type days = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); Var day: days; begin if day = Sunday then writeln(‘Today is Sunday!’); End.

The elements included in the definition of an enumerated type are considered to be ordered in the order in which they are enumerated. Numbering starts from zero. Therefore, in the example given, the days of the week have the following serial numbers

To programmatically determine the ordinal number, the Ord() function is used.

In our example, the sequence numbers are equal:

Ord(Monday) = 0;

Ord(Saturday) = 5;

Ord(Sunday) = 6.

Interval type

If a variable does not accept all values ​​of its type, but only values ​​contained in a certain range, then this data type is called an interval type. The interval type is often called the limited type and the range type. An interval type is defined by the boundaries of its values:

<минимальное значение>..<максимальное значение>

  • two ".." characters are treated as one character, so spaces between them are not allowed;
  • the left border of the range should not exceed its right border.

The interval type is a user-defined data type, so the declaration of this type begins with function word TYPE.

An example of an interval type description:

Type digit = 1..10; month = 1..31; lat = 'A'..'Z';

A data type defines a set of valid values ​​and a set of valid operations.

Simple types.

Simple types are divided into ORDINAL and REAL.

1. ORDERAL TYPES , in turn, there are:

a) whole

Pascal defines 5 integer types, which are defined depending on the sign and value that the variable will take.

Type name

Length (in bytes)

Range of values

32 768...+32 767

2 147 483 648...+2 147 483 647

b) logical

The name of this type is BOOLEAN. Boolean values ​​can be one of the logical constants: TRUE (true) or FALSE (false).

c) symbolic

The name of this type is CHAR - occupies 1 byte. The value of a character type is the set of all PC characters. Each character is assigned an integer in the range 0…255. This number serves as a code for the internal representation of the symbol.

2. REAL TYPES .

Unlike ordinal types, whose values ​​are always mapped to a series of integers and are therefore represented absolutely exactly in PC, the values ​​of real types define an arbitrary number only with some finite precision depending on the internal format of the real number.

Length of numeric data type, bytes

Numeric data type name

Number of significant digits of a numeric data type

Decimal order range of a numeric data type

2*1063 +1..+2*1063 -1

STRUCTURED TYPES

Structured data types define an ordered collection of scalar variables and are characterized by the type of their components.

Structured data types, unlike simple ones, define many complex values ​​with one common name. We can say that structural types determine a certain way of forming new types from existing ones.

There are several structuring methods. According to the method of organization and type of components in complex data types, the following varieties are distinguished: regular type (arrays); combined type (records); filetype(files); multiple type(s); string type(strings); in the Turbo Pascal language version 6.0 and older, an object type (objects) was introduced.

Unlike simple data types, structured type data is characterized by the multiplicity of elements that form this type, i.e. a variable or constant of a structured type always has multiple components. Each component, in turn, can belong to a structured type, i.e. nesting of types is possible.

1. Arrays

Arrays in Turbo Pascal are in many ways similar to similar data types in other programming languages. A distinctive feature of arrays is that all their components are data of the same type (possibly structured). These components can be easily organized and any one of them can be accessed simply by specifying a serial number.

The array description is specified as follows:

<имя типа>= array [<сп.инд.типов>] of<тип>

Here<имя типа>- correct identifier;

Array, of – reserved words (array, from);

<сп.инд.типов>- a list of one or more index types, separated by commas; square brackets framing the list are a syntax requirement;

<тип>- any type of Turbo Pascal.

Any ordinal types can be used as index types in Turbo Pascal, except LongInt and range types with the base type LongInt.

The depth of nesting of structured types in general, and therefore of arrays, is arbitrary, so the number of elements in the list of type indexes (array size) is not limited, however, the total length of the internal representation of any array cannot be more than 65520 bytes.

2. Records

A record is a data structure consisting of a fixed number of components called record fields. Unlike an array, the components (fields) of a record can be various types. To make it possible to refer to one or another component of a record, the fields are named.

The structure of a post type declaration is:

< Nametype>=RECORD< joint venture. fields>END

Here<имя типа>- correct identifier;

RECORD, END – reserved words (record, end);

<сп.полей>- list of fields; is a sequence of sections of a record separated by a semicolon.

3. Sets

Sets are a set of objects of the same type that are logically connected to each other. The nature of the connections between objects is only implied by the programmer and is in no way controlled by Turbo Pascal. the number of elements included in a set can vary from 0 to 256 (a set that does not contain elements is called empty). It is the inconstancy of the number of its elements that sets differ from arrays and records.

Two sets are considered equivalent if and only if all their elements are the same, and the order of the elements of the set is indifferent. If all the elements of one set are also included in another, the first set is said to be included in the second.

The description of the set type is:

< Nametype>=SET OF< bases. type>

Here<имя типа>- correct identifier;

SET, OF – reserved words (set, of);

<баз.тип>- the base type of set elements, which can be any ordinal type except WORD, INTEGER and LONGINT.

To define a set, the so-called set constructor is used: a list of specifications of the elements of the set, separated by commas; the list is surrounded by square brackets. Element specifications can be constants or expressions of a base type, as well as a range type of the same base type.

4. Files

A file refers to either a named area external memory A PC or logical device is a potential source or receiver of information.

Any file has three characteristic features

    it has a name, which allows the program to work with several files simultaneously.

    it contains components of the same type. The component type can be any Turbo Pascal type, except files. In other words, you cannot create a “file of files.”

    the length of the newly created file is not specified in any way when it is declared and is limited only by the capacity of external memory devices.

A file type or file type variable can be specified in one of three ways:

< Name>= FILE OF< type>;

< Name>=TEXT;

<имя>= FILE;

Here<имя>- file type name (correct identifier);

FILE, OF – reserved words (file, from);

TEXT – name standard type text files;

<тип>- any type of Turbo Pascal, except files.

Depending on the method of declaration, three types of files can be distinguished:

· typed files (set by the FILE OF... clause);

· text files(defined by type TEXT);

· untyped files (defined by the FILE type).

About converting Pascal's numeric data types

In Pascal, implicit (automatic) conversions of numeric data types are almost impossible. An exception is made only for the integer type, which is allowed to be used in expressions of type real. For example, if the variables are declared like this:

Var X: integer; Y: real;

then the operator

will be syntactically correct, although there is an integer expression to the right of the assignment sign and a real variable to the left, the compiler will convert numeric data types automatically. The reverse conversion automatically from the real type to the integer type is impossible in Pascal. Let's remember how many bytes are allocated for variables of type integer and real: 2 bytes of memory are allocated for the integer data type integer, and 6 bytes for real. There are two built-in functions for converting real to integer: round(x) rounds a real x to the nearest integer, trunc(x) truncates a real by discarding the fractional part.

Share