How to set up smartphones and PCs. Informational portal

In pascal, the real data type is. Address data types

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

Description of numeric data types (integers) Pascal

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. So, Pascal 7.0 uses the following integer numeric data types:

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

  • Arithmetic:
    addition(+);
    subtraction(-);
    multiplication(*);
    remainder of division (mod);
    exponentiation;
    unary plus (+);
    unary minus (-).
  • Relationship operations:
    equality relation (=);
    inequality relation (<>);
    ratio less (<);
    ratio greater than (>);
    ratio not less than (>=);
    ratio 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 cardinality (maximum range of values). Possible result overflow is not controlled in any way (it is important!) , which can lead to errors.

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

Description of numeric data types (real) Pascal

The real numeric data type refers to a subset of real numbers that can be represented in a 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 digits is called the mantissa, the second - the order. 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 is the order of the number (r is an integer); P is the base of the number system. For example, for a decimal base, the representation of 2E-1 (here E is the base of the decimal number system) will look like: 2*10 -1 =0.2, and the representation of 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 the real type, a 4-byte variable will be created in the computer's memory. In this case, 3 bytes will be given under the mantissa, and one - under the order.

You can perform the following operations on real numeric data types:

  • Arithmetic:
    addition (+);
    subtraction(-);
    multiplication(*);
    division(/);
    exponentiation;
    unary plus (+);
    unary minus (-).
  • Relationship operations:
    inequality relation (<>);
    ratio less (<);
    ratio greater than (>);
    ratio not less than (>=);
    ratio 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 numerical data types are designed for hardware support of floating point arithmetic and for their effective use, the PC must include a mathematical coprocessor.

A special position in Pascal is occupied by a numeric data type. comp, which is treated as a real number without exponential and fractional parts. Actually, comp is a signed "big" integer that stores 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 of mathematical functions, etc.

About converting numeric data types in Pascal

Implicit (automatic) conversions of numeric data types are almost impossible in Pascal. 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 as follows:

VarX: integer; Y: real

Then the operator

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

In Pascal variables are characterized by their type. A type is a property of a variable, according to which a variable can take on a set of values ​​allowed by this type, and participate in a set of operations allowed on this type.

A type defines the set of valid values ​​that a variable of a given type can take. It also defines the set of allowed operations from a variable of this type and defines the representation of data in the computer's RAM.

For example:

n:integer;

Pascal is a static language, which means that the type of a variable is determined when it is declared and cannot be changed. The Pascal language has a developed system of types - all data must belong to a previously known data type (either a standard type created during the development of the language or a user-defined type that the programmer defines). The programmer can create his types with an arbitrary complexity structure based on standard types, or types already defined by the user. The number of created types is unlimited. Custom types in the program are declared in the TYPE section by the format:

[name] = [type]

The standard type system has a branched, hierarchical structure.

Primary in the hierarchy are simple types. Such types are present in most programming languages ​​and are called simple, but in Pascal they have a more complex structure.

Structured types are built according to certain rules from simple types.

Pointers are formed from simple views and are used in programs to set addresses.

Procedural types are an innovation of the Turbo Pascal language, and they allow you to refer to subroutines as if they were variables.

Objects are also new, and they are intended to be used as an object-oriented language.

There are 5 types of integer types in Pascal. Each of them characterizes the range of accepted values ​​and their place in memory.

When using integer numbers, one should be guided by the nesting of types, i.e. smaller range types can be nested within larger range types. The Byte type can be nested in all types that occupy 2 and 4 bytes. At the same time, the Short Int type, which occupies 1 byte, cannot be nested in the Word type, since it does not have negative values.

There are 5 real types:

Integer types are exactly represented in a computer. Unlike integer types, the value of real types defines an arbitrary number only with some finite precision, depending on the format of the number. Real numbers are represented in a computer with fixed or floating point.

2358.8395

0.23588395*10 4

0.23588395*E 4

A special position in Pascal is occupied by the Comp type, in fact, it is a signed large integer. This type is compatible with all real types and can be used for large integers. When representing real floating-point numbers, the decimal point is always implied before the left or highest mantissa, but when operating on a number, it is shifted to the left or right.

Ordinal types

Ordinal types combine several simple types. These include:

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

Common features for ordinal types are: each type has a finite number of possible values; the value of these types can be ordered in a certain way and with each number a certain number, which is a serial number, can be compared; adjacent values ​​of ordinal types differ by one.

For ordinal type values, the ODD(x) function can be applied, which returns the ordinal number of the x argument.

Function PRED(x) - returns the previous value of the ordinal type. PRED(A) = 5.

SUCC(x) Function - Returns the next value of an ordinal type. SUCC(A) = 5.

Character type

Character type values ​​are 256 characters out of the set allowed by the code table of the computer being used. The initial area of ​​this set, i.e. the range from 0 to 127, corresponds to the set of ASCII codes where the characters of the alphabet, Arabic numbers and special characters are loaded. The start area characters are always present on the PC keyboard. The older area is called alternative, it contains characters of national alphabets and various special characters, and pseudographic characters that do not correspond to the ASCII code.

A character type value occupies one byte in RAM. In the program, values ​​are enclosed in apostrophes. Also, values ​​can be specified in the form of its ASCII code. In this case, the number with the character code must be preceded by a # sign.

C:= 'A'

Boolean (boolean) type

There are two values ​​of Boolean type: True (True) and False (False). Variables of this type are specified by the service word BOOLEAN. A Boolean value takes up one byte in RAM. The True and False values ​​correspond to the numeric values ​​1 and 0.

Type-range

There is a subset of its base type, which can be any ordinal type. The range type is defined by the boundaries within the base type.

[min-value]…[max-value]

The range type can be specified in the Type section as a specific type, or directly in the Var section.

When defining a range-type, one should be guided by:

  • the left border should not exceed the right border;
  • a range type inherits all the properties of the base type, but with limitations related to its lower cardinality.

Enumerated type

This type refers to ordinal types and is specified by listing those values ​​that it can enumerate. Each value is named by some identifier and is located in the list framed in parentheses. The enumerated type is specified in Type:

peoples = (men, women);

The first value is 0, the second value is 1, and so on.

Maximum power 65535 values.

string type

The string type belongs to the group of structured types and consists of the base type Char. The string type is not an ordinal type. It defines a set of character strings of arbitrary length up to 255 characters.

In the program, the string type is declared by the word String. Since String is a base type, it is declared in the language and the declaration of a variable of type String is done in Var. When declaring a string type variable behind String, it is advisable to indicate the length of the string in square brackets. Specifies an integer between 0 and 255.

Fam: String;

Specifying the string length allows the compiler to allocate the specified number of bytes in RAM for the given variable. If the string length is not specified, then the compiler will allocate the maximum possible number of bytes (255) for the value of this variable.

Federal Agency for Education

abstract

"DATA TYPES IN PASCAL"

1. Data types

Any data, i.e. constants, variables, properties, function values ​​or expressions are characterized by their types. A type defines the set of valid values ​​that an object can have, as well as the set of valid operations that can be applied to it. In addition, the type also determines the format of the internal representation of data in the PC memory.

In general, the Object Pascal language is characterized by a branched structure of data types (Fig. 1.1). The language provides a mechanism for creating new types, so that the total number of types used in the program can be arbitrarily large.

The data processed in the program is divided into variables, constants and literals:

Constants are data whose values ​​are set in the constant declaration section and do not change during program execution.

Variables are declared in the variable declaration section, but unlike constants, they get their values ​​already during the execution of the program, and these values ​​can be changed. Constants and variables can be referred to by name.

Literal has no identifier and is represented directly by the value in the program text.

Type of defines the set of values ​​that data elements can take, and the set of operations allowed on them.

This and the four following chapters provide a detailed description of each type.

1.1 Simple types

Simple types include ordinal, real, and date-time types.

Ordinal types differ in that each of them has a finite number of possible values. These values ​​can be ordered in a certain way (hence the name of the types) and, therefore, each of them can be associated with some integer - the ordinal number of the value.

Real types, strictly speaking, also have a finite number of values, which is determined by the format of the internal representation of a real number. However, the number of possible values ​​of real types is so large that it is not possible to associate an integer (its number) with each of them.

date-time type designed to store date and time. In fact, it uses the real format for these purposes.

1.1.1 Ordinal types

Ordinal types include (see Figure 1.1) integer, boolean, character, enum, and range. For any of them, the Ord(x) function is applicable, which returns the ordinal number of the value of the expression X.


Rice. 1.1 - Structure of data types

For whole types, the function ord(x) returns the value of x itself, that is, Ord(X) = x for x belonging to any whole type. Applying Ord(x) to logical , symbolic and enumerated types gives a positive integer in the range 0 to 1 ( boolean type), from 0 to 255 ( symbolic), from 0 to 65535 ( enumerable). Type-range preserves all the properties of the underlying ordinal type, so the result of applying the ord(x) function to it depends on the properties of that type.

Functions can also be applied to ordinal types:

predict(x)- returns the previous value of the ordinal type (the value that corresponds to the ordinal ord (x) -1, i.e. ord(pred(x)) = ord(x) - 1;

succ(x)- returns the next value of the ordinal type that corresponds to the ordinal ord(x) + 1, i.e. ord(Succ(x)) = ord(x) + 1.

For example, if the program defines a variable

then the PRED(c) function will return the character "4" and the SUCC(c) function will return the character "6".

If we imagine any ordinal type as an ordered set of values ​​increasing from left to right and occupying a certain segment on the number axis, then the function pred (x) is not defined for the left end, and succ (x) - for the right end of this segment.

Integer types . The range of possible values ​​for integer types depends on their internal representation, which can be one, two, four, or eight bytes. In table. 1.1 lists the names of integer types, the length of their internal representation in bytes, and the range of possible values.

Table 1.1 - Integer types

Name Length, bytes Value range
cardinal 4 0. .. 2 147 483 647
bytes 1 0...255
Shortint 1 -128...+127
Smallint 2 -32 768...+32 767
Word 2 0...65 535
Integer 4
Longint 4 -2 147 483 648...+2 147 483 647
int64 8 -9*1018...+9*1018
long word 4 0. . .4 294 967 295

Types long word and int64 first introduced in version 4, and types Smallint and cardinal missing in Delphi 1. Type integer for this version, it takes 2 bytes and has a range of values ​​from -32768 to +32767, i.e., the same as Smallint .

When using procedures and functions with integer parameters, one should be guided by the “nesting” of types, i.e. wherever it can be used word, it is allowed to use bytes(but not vice versa) Longint“enters” Smallint, which in turn includes Shortint .

The list of procedures and functions applicable to integer types is given in Table. 1.2. Letters b, s, w, i, l denote expressions, respectively, of the type bytes , Shortint, Word, Integer and Longint ,

x is an expression of any of these types; the letters vb, vs, vw, vi, vl, vx denote variables of the respective types. An optional parameter is indicated in square brackets.

Table 1.2 - Standard Procedures and Functions Applicable to Integer Types

Appeal Result type Action
abs(x) x Returns module x
chr(b) Char Returns a character by its code
dec(vx[, i]) - Decreases the value of vx by i, and in the absence of i - by 1
inc(vx[,i]) - Increases the value of vx by i, and in the absence of i - by 1
Hi(w) bytes Returns the highest bow of the argument
Hi(I) Same Returns the third byte
Lo(i) Returns the low byte of the argument
Lo(w) Same
odd(l) Boolean Returns True if the argument is an odd number
Random(w) As parameter Returns a pseudo-random number evenly distributed in the range 0...(w-l)
sqr(x) X Returns the square of the argument
swap(i) Integer Swap bytes in a word
swap(w) Word Too

When operating on integers, the type of the result will correspond to the type of the operands, and if the operands are of different integer types, to a common type that includes both operands. For example, when acting with shortint and word common will be the type integer. By default, the Delphi compiler does not generate code that controls whether a value is out of range, which can lead to confusion.

Boolean types . Boolean types are Boolean, ByteBool, Bool, wordBool and longbool. In standard Pascal, only the type is defined Boolean, other Boolean types are introduced in Object Pascal for compatibility with Windows: types Boolean and ByteBool occupy one byte each, Bool and wordbool- 2 bytes, longbool- 4 bytes. Boolean values ​​can be one of the pre-declared constants False (false) or True (true).

Since the Boolean type is an ordinal type, it can be used in a counting type loop statement. In Delphi 32 for Boolean meaning

Ord (True) = +1, while for other types ( bool, wordbool etc.)

Ord (True) = -1, so these kinds of operators should be used with care! For example, for version Delphi 6, the executable showMessage (" ---") statement in the following loop for will never be executed:

for L:= False to True do

ShowMessage("--);

If we replace the loop parameter type L in the previous example with Boolean, the loop will run and the message will appear twice on the screen. [For Delphi version 1 and 2 ord (True) =+1 for any boolean type.]

Character type . Character type values ​​are the set of all PK characters. Each character is assigned an integer in the range 0...255. This number serves as the code for the internal representation of the character, and is returned by the ord function.

Windows encoding uses ANSI code (named for the American National Standard Institute, the American standards institute that proposed this code). The first half of PC characters with codes 0...127 corresponds to Table 1.3. The second half of characters with codes 128...255 changes for different fonts. The standard Windows fonts Arial Cyr, Courier New Cyr and Times New Roman to represent Cyrillic characters (without the letters “ё” and “Ё”) use the last 64 codes (from 192 to 256): “А”... “Я” are encoded values ​​192..223, “a”... “i” - 224...255. The characters "Ё" and "ё" have codes 168 and 184, respectively.

Table 1.3 - Character encoding according to the ANSI standard

The code Symbol The code. Symbol The code. Symbol The code Symbol
0 NUL 32 BL 64 @ 96 "
1 ZON 33 ! 65 BUT 97 a
2 STX 34 66 AT 98 b
3 ETX 35 # 67 FROM 99 With
4 EOT 36 $ 68 D 100 d
5 ENQ 37 % 69 E 101 e
6 ACK 38 & 70 F 102 f
7 BEL 39 " 71 G 103 d
8" BS 40 ( 72 H 104 h
9 HT 41 ) 73 I 105 i
10 LF 42 * 74 J 106 j
11 VT 43 + 75 To 107 k
12 FF 44 F 76 L 108 1
13 CR 45 - 77 M 109 m
14 SO 46 78 N 110 n
15 SI 47 / 79 0 111 about
16 DEL 48 0 80 R 112 P
17 DC1 49 1 81 Q 113 q
18 DC2 50 2 82 R 114 r
19 DC3 51 3 83 S 115 s
20 DC 4 52 4 84 T 116 t
21 NAK 53 5 85 U 117 u
22 SYN 54 6 86 V 118 v
23 ETB 55 7 87 W 119 W
24 CAN 56 8 88 X 120 x
25 EM 57 9 89 Y 121 At
26 SUB 58 : 90 Z .122 z
27 ESC 59 ; 91 t 123 {
28 FS 60 < 92 \ 124 1
29 GS 61 = 93 ] 125 }
30 RS 62 > 94 L 126 ~
31 US 63 F 95 127 r

Symbols with codes 0...31 refer to service codes. If these codes are used in the symbolic text of a program, they are considered spaces.

Pascal Data Types

Any data (constants, variables, function values ​​or expressions) in Turbo Pascal are characterized by their types. A type defines the set of valid values ​​that an object can have, as well as the set of valid operations that can be applied to it. The type also determines the format of the internal representation of data in the computer's memory.

The following data types exist in Turbo Pascal.

1) Simple types:

- real;

- symbolic;

- boolean (logical);

- listed;

– limited (range).

2) Composite (structured) types:

– regular (arrays);

– combined (records);

- file;

- multiple;

- string;

- objects.

3) Reference types (typed and untyped pointers).

4) Procedural types.

Turbo Pascal provides a mechanism for creating new data types, so that the total number of types used in the program can be arbitrarily large.

integer type. Integer type values ​​are elements of a subset of integers. There are five integer types in Turbo Pascal. Their names, range of values, length of representation in bytes are given in Table. 6.

Table 6

Integer data types

Integer variables are declared using the above reserved words:

i, j, k: integer;

Integer type data is stored exactly in memory. For example, variables of type integer occupy 2 bytes (16 bits) in memory, which are distributed as follows: 1 bit is reserved for storing the sign of the number (0 if the number is positive, and 1 if the number is negative) and 15 bits for storing the number in binary system reckoning. The maximum decimal number that can be written as 15 bit binary is 32767.

When using procedures and functions with integer parameters, one should be guided by the “nesting” of types, i.e. wherever a word is used, a byte is allowed (but not vice versa), a longint "includes" an integer, which, in turn, includes a shortint.

The integer type defines five basic operations that also result in an integer: +, -,*, div, mod (addition, subtraction, multiplication, integer division, and integer division remainder). In arithmetic expressions, the operations *, div, mod have a higher precedence than the operations +, -. Examples of writing expressions:

The list of procedures and functions applicable to integer types is given in Table. 7. The letters b, s, w, i, l denote expressions of type byte, shortint, word, integer, and longint, respectively; x is an expression of any of these types; identifiers vb, vs, vw, vi, vl, vx denote variables of the corresponding types. An optional parameter is indicated in square brackets.

Table 7

Standard Procedures and Functions Applicable to Integer Types

Appeal Result type Action
Abs(x) x Returns module x
Chr(b) Char Returns a character by its code
Dec(vx[, i]) - Decreases the value of vx by i, and in the absence of i - by 1
Inc(vx[,i]) - Increases the value of vx by i, and in the absence of i - by 1
Hi(i) bytes Returns the high byte of the argument
Hi(i) bytes Same
Lo(i) bytes Returns the low byte of the argument
lo(w) bytes Same
Odd(l) bytes Returns true if the argument is an odd number
Random(w) As parameter Returns a pseudo-random number uniformly distributed in the range 0…(w-1)
Square(x) x Returns the square of the argument
Swap(i) Integer
Swap (w) Word Swap bytes in a word
Succ(x) As parameter Returns the next integer value, i.e. x+1
pred(x) As parameter Returns the previous integer value, i.e. x-1

When operating on integers, the type of the result will correspond to the type of the operand, and if the operands are of different integer types, to the type of the operand that has the maximum range of values. Possible overflow of the result is not controlled, which can lead to errors in the program.

Real type. Values ​​of real types define an arbitrary number with some finite precision depending on the internal format of the real number. There are five real types in Turbo Pascal (Table 8).

Table 8

Real data types

Real variables are declared using the above reserved words:

A real number in computer memory consists of 3 parts:

The sign digit of the number;

Exponential part;

The mantissa of the number.

The mantissa has a length of 23 (Single) to 63 (Extended) binary digits, which provides an accuracy of 7-8 for Single and 19-20 for Extended decimal digits. The decimal point (comma) is implied before the left (most significant) digit of the mantissa, but when operating with a number, its position is shifted to the left or right in accordance with the binary order of the number stored in the exponential part, therefore operations on real numbers are called floating point (comma) arithmetic .

The Single, Double, and Extended types are accessed only under special compilation modes. To enable these modes, select the menu item Options, Compiler... and enable the option 8087/80287 in a group Numeric processing.

A special position in Turbo Pascal is occupied by the Comp type, which is treated as a real number without exponential and fractional parts. In fact, Comp is a signed large integer that stores 19 to 20 significant decimal digits. At the same time, Comp is fully compatible with any other real types in expressions: all real operations are defined on it, it can be used as an argument of mathematical operations, etc.



Real numbers are given in decimal notation in one of two forms.

AT fixed point form the record consists of an integer and a fractional part, separated from each other by a dot, for example:

0.087 4.0 23.5 0.6

AT floating point form the entry contains the letter E, which means "multiply by ten to the power", and the degree is an integer, for example:

7E3 6.9E-8 0.98E-02 45E+04

The following operations are defined on objects of real type: +, -, *, /.

The "*" and "/" operators have higher precedence than the "+" and "-" operators.

If at least one operand is real, then the operations +, -, *, / lead to a real result. The division operation / leads to a real result in the case of two integer operands, for example: 9/3 = 3.0.

To work with real data, the standard mathematical functions presented in Table 1 can be used. 9. The result of these functions is also real.

Table 9

Math functions that work with real data

Variables and constants of REAL type are not allowed to be used:

– in functions pred(x), succ(x), ord(x);

– as indexes of arrays;

– as labels in control transfer statements;

– as control variables (cycle parameters).

To convert a real number to an integer, you can use the following functions:

trunc(x) is the integer part of x (x is real);

round(x) – rounding to the nearest integer (x is real).

character type. Character variables are declared using the reserved word char:

Values ​​of this type are selected from an ordered set of characters (from the ASCII set) consisting of 256 characters. Each character is assigned an integer from the range 0..255. For example, uppercase letters of the Latin alphabet A..Z have codes 65..90, and lowercase letters have codes 97..122.

The value of a character type variable is a single character enclosed in apostrophes, for example:

'F' '8' '*'

Character variables can be compared with each other by comparing character codes.

There are functions that establish a correspondence between a character and its code:

ord(с) - gives the number of the symbol from;

chr(i) - returns the character with number i.

These functions are inverse of each other.

boolean type. Boolean variables are declared using the reserved word boolean:

p1, p2: boolean;

Boolean variables take two values: true(true), false(False).

These values ​​are ordered as follows: false< true. false имеет порядковый номер 0, true имеет порядковый номер 1.

Boolean variables can either be assigned a value directly, or a boolean expression can be used. For example,

a, d, g, b: boolean;

Relational operations (<, <=, >, >=, =, <>) applied to integer, real, and character variables produce a boolean result.

Logical operations on operands of a boolean type also give a logical result (the operations are listed in descending order of priority) (for details, see Tables 3 and 5):

not – negation (NOT operation);

and - logical multiplication (operation AND);

or – logical addition (OR operation);

xor - exclusive OR.

The expression (not a) has the opposite meaning of a.

The expression (a and b) evaluates to true only if both a and b are true, otherwise the value of this expression is false.

The expression (a or b) evaluates to false only if both a and b are false, in all other cases the result is true.

Enumerated type. A non-standard enumerated type is specified by the enumeration as the names of the values ​​that the variable can take. Each value is named by some identifier and is located in a list enclosed in parentheses. The general form of the enumerated type description:

x = (w1, w2, …, wn);

where x is the name of the type, w1, w2,…, wn are the values ​​that a variable of type x can take.

These values ​​are ordered w1

The following standard functions apply to an enumerated type argument w:

succ(w), pred(w), ord(w).

color=(red, black, yellow, green)

ww=(left, up, right, down);

f: array of ww;

succ(d) = yellow;

Variables a and b are of type w. they can take one of three values, and on

Relational operations are applicable to values ​​of enumerated type: =,<>, <=, >=, <, >.

It is allowed to specify enumerated type constants directly in the section var without using section type, for example

c,d: (red, black, yellow, green);

Range (limited) type. When defining a bounded type, you specify the start and end values ​​that a range type variable can take. The values ​​are separated by two dots.

The description of a restricted type is

Here a is the name of the type, min, max are constants.

When specifying a restricted type, the following rules must be followed:

– both boundary constants min and max must be of the same type;

– A restricted type is created from the base type data, which can be an integer, character, or enumerated type. For example:

col = red..yellow;

letter = 'a'..'f';

- Variables of a restricted type can be declared in the var section without referring to the type section:

– a restricted type inherits all the properties of the base type from which it is created;

– the min limit must always be less than the max limit.

Arrays. An array is a complex type, which is a structure consisting of a fixed number of components of the same type. The type of the component is called the base type. All array components can be easily ordered and any one of them can be accessed simply by specifying its ordinal number. Description of the array in the section var looks like:

a: array of t2;

where a is the name of the array, array, of– service words (meaning “array of…”), t1 – index type; t2 – component type (base type).

The number of indexes determines the dimension of the array. Indexes can be of integer (except longint), character, boolean, enumerated, and range types. Indexes are separated by commas and enclosed in square brackets. Array components can be of any type except file.

Example 1 Consider a one-dimensional array C, whose values ​​are five real numbers:

4.6 6.23 12 -4.8 0.7

The description of this array is as follows:

c: array of real;

By a specific index value, you can select a specific array component (for example, C means the third element of array C, i.e. the number 12).

Example 2 Consider a two-dimensional array B (matrix B) whose value is a table of integers:

The description of this array is as follows:

b of integer;

Here b is the name of the array, the first index is the row number and takes values ​​from 1 to 2, the second is the column number and takes values ​​from 1 to 4. By specific index values, you can select a specific array component (for example, b means a table element in first row and third column, i.e. the number -4).

Indexes can be arbitrary expressions, corresponding to the type of indexes from the array declaration:

a: array of real;

a[(i+1)*2] := 24;

The set of operations on array elements is completely determined by the type of these elements.

string type. String type - a set of character strings of arbitrary length (from zero to a given number). Variables of string type are described using a service word string:

b: string ;

Peculiarities:

– the value of a string variable can be entered using the keyboard, assigned in an assignment statement, read from a file. In this case, the length of the entered string can be any (less than the specified size, equal to the size or more, in the latter case, extra characters are discarded); a:= 'Results';

– it is allowed to use the concatenation operation in the assignment operator, since strings can dynamically change their length: a:= a + ‘calculations’;

– the maximum length of a string variable is 255 characters, this length indication can be omitted:

a: string;

a1: string ;

Variables a and a1 are the same (equivalent description).

- memory for string type variables is allocated to the maximum, but only a part of the memory that is actually occupied by the characters of the string at the moment is used. To describe a string variable of length n, n + 1 bytes of memory are used: n bytes - to store the characters of the string, n + 1 -th byte - to store the current length.

– comparison operations are defined on values ​​of string types:< <= > >= = <>. A short string is always less than a long one. If the strings are of the same length, then the character codes are compared.

– access to individual elements of a string is possible in the same way as access to array elements: a, a. Square brackets indicate the number of the element of the line.

Procedures and functions focused on working with strings.

concat (s1, s2,…)- string merge function, s1, s2,…- lines, the number of lines can be arbitrary. The result of the function is a string. If the resulting string is longer than 255 characters, then the string is truncated to 255 characters.

copy(s, index, count)– function of extracting a string from the source string s long count characters starting with character number index.

delete(s, index, count) is the procedure for removing from the string s a substring of length count characters, starting with a character with a number index.

insert (s1, s2, index)- line insertion procedure s1 into a string s2, starting with a character with a number index.

length(s)– function for determining the current length of the string, returns a number equal to the current length of the string.

pos(s1, s2)- search function in a string s2 substrings s1. returns the position number of the first character of a substring s1 in line s2(or 0 if this line does not exist).

val(st, x, code)– procedure for converting the string s into an integer or real variable x. Parameter code contains 0 if the conversion was successful (and in x the result of the conversion is placed), or the position number of the string where the erroneous character was found (in this case, the value x does not change).

Compatibility and type conversion. Turbo Pascal is a typed language. It is built on the basis of strict adherence to the concept of types, according to which all operations used in the language are defined only on operands of compatible types.

Two types are considered compatible if:

- they are both of the same type;

are both real;

- both are intact

– one type is a range type of the second type;

– both are range types of the same underlying type;

– both are sets composed of elements of the same basic type;

– both are packed strings (defined with a preceding packed word) of the same maximum length;

- one is a type-string, and the other is a type-string or character;

- one type is any pointer, and the other is a pointer to a related object;

– both are procedural types with the same result type (for a function type), the number of parameters, and the type of mutually corresponding parameters.

Type compatibility takes on special significance in assignment statements. Let t1 be the type of the variable and t2 be the type of the expression, that is, the assignment is t1:=t2. This assignment is possible in the following cases:

– t1 and t2 are of the same type, and this type does not apply to files, arrays of files, records containing file fields, or arrays of such records;

– t1 and t2 are compatible ordinal types and the value of t2 lies in the range of possible values ​​of t1;

– t1 and t2 are real types, and the value of t2 lies in the range of possible values ​​of t1;

– t1 is a real type and t2 is an integer type;

– t1 – string and t2 – symbol;

– t1 is a string and t2 is a packed string;

– t1 and t2 are compatible packed strings;

– t1 and t2 are compatible sets and all members of t2 belong to the set of possible values ​​of t1;

– t1 and t2 are compatible pointers;

– t1 and t2 are compatible procedural types;

– t1 is an object and t2 is its child.

In a program, data of one type can be converted to data of another type. Such a conversion may be explicit or implicit.

Explicit type conversion calls special conversion functions whose arguments are of one type and whose values ​​are of another type. An example is the already considered functions ord, trunc, round, chr.

Implicit conversion is only possible in two cases:

- in expressions made up of real and integer variables, the latter are automatically converted to a real type, and the entire expression as a whole acquires a real type;

- the same memory area is alternately treated as containing data of one or another type (combination of data of different types in memory).

The concept of type is one of the fundamental concepts of any programming language. Objects (constants, variables, functions, expressions) that the program operates on are of a certain type.

Type of is the set of values ​​that program objects can take, and the set of operations allowed on these values.

For example, the values ​​1 and 2 are of integer type, they can be added, multiplied, and other arithmetic operations can be performed. The meanings "monitor" and "Pascal" are linguistic in nature, they have their own set of valid operations. In most commonly used languages, only well-defined, known types can be used. Pascal, along with the standard types found in other high-level languages, allows the programmer to create their own types.

All types allowed in the Pascal language are divided into two large groups: simple and complex (structured).

Type of Range Mantissa, signs Required memory (bytes)
REAL 2.9*10E-39..1.7*10E38 11-12
SINGLE 1.5*10E-45..3.4*10E38 7-8
DOUBLE 5.0*10E-324..1.7*10E308 15-16
EXTENDED 1.9*10E-4951..1.1*10E4932 19-20
COMP -2Е+63+1..2Е+63-1 10-20

Effective use of SINGLE, DOUBLE, EXTEND, COMP types is possible only when the directive ($N+) is included. By default, it is in the off state. To solve engineering and economic problems, values ​​of the REAL type are sufficient.

Example

Var Res, Summa, Itog: real;

Bulevsky the data type is described by the identifier BOOLEAN. Variables and constants of this type can take only one of two values: TRUE (true) or FALSE (false).

Example

Var Sel1, Sel2: boolean;

A,B,C,D: boolean;

Boolean type expressions occupy 1 byte of memory and are used in logical and relational expressions, as well as to control the execution order of program statements.

Literal (character) the type is described by the standard identifier CHAR. Constants and variables of this type can take one of the values ​​of the ASCII code table. The value of a constant or variable of this type is enclosed in apostrophes.

For example, Var Bukva, Znak, Symbol: char;

Letter:='A'; Sign:='+'; Symbol:='!'

Variables of character type occupy 1 byte in memory. The use of data of type char in arithmetic expressions is prohibited. Comparison operations can be applied to literal values, the result of which depends on the number of the literal variable or constant in the code table.

In addition to standard data types, Pascal supports scalar types, user defined. These include enumerable and interval types . These data types occupy 1 byte of memory, so any user-defined type cannot contain more than 255 elements. Their use significantly improves the visibility of the program, makes it easier to find errors and saves memory.

Enumerated type is specified directly by listing all the values ​​that a variable of a given type can take. Individual values ​​are separated by commas, and the entire list is enclosed in parentheses.

Format

type<имя типа>=(<значение1, значение2, ..., значениеN>);

Var<идентификатор, ...>: < имя типа>;

Example

Type Season =(Spring, Summer, Autumn, Winter);

Var S1, S2: Season;

Autumn: (September, October, Nowember);

In this example, the explicitly declared data type of the Season user is shown. Their meanings are determined - the designations of the seasons. Variables S1 and S2 can take only one of the listed values. Attempting to assign any other value to them will cause a software interrupt. The third type of enumeration is anonymous (has no name) and is specified by enumeration of values ​​in the Var section. Autumn is a variable of this type and can take the values ​​September, October, Nowember. Thus, any type can be given, but this is not always acceptable. The first way is certainly more understandable and more in line with the nature of the Pascal language.

interval type allows you to set two constants that define the boundaries of the range of values ​​for this variable. Whenever an operation is performed on an interval type variable, the compiler generates check routines to determine whether the variable's value remains within its specified range. Both constants must belong to one of the standard types, except real. The value of the first constant must necessarily be less than the value of the second.

Format

type<имя типа> = <константа1> .. <константа2>;

Var<идентификатор>: < имя типа>;

Example

Type Days = 1.. 31;

Var Work_d, Free_d: Days;

In this example, the variables Work_d, Free_d are of type days and can take any value from the range 1 . . 31.

Out of range triggers a software interrupt.

You can define an interval type by specifying the range limits not by constant values, but by their names:

Const Min = 1; max=31;

Type Days = Min .. Max;

Var Work_d, Free_d: Days;

Structured types data are based on scalar types and may contain various combinations of them. They define an ordered set of scalar elements and are characterized by the type of their components. Pascal provides the following structured data types:

line - a sequence of characters enclosed in apostrophes;

array - structured data type consisting of a fixed number of elements of the same type, accessed by index ;

lots of - a set of objects selected according to some attribute or group of attributes, which can be considered as a whole;

record - a set of a fixed number of components of different types;

file- a sequence of components of the same type and the same length.

Two more structured types - procedural and type object (objective) - are difficult to associate with data in a conventional representation.

Picture 1- A set of basic types of the Pascal language

Top Related Articles