Pascal data types. Data types in pascal

The lesson discusses the main standard data types in Pascal, the concept of a variable and a constant; explains how to work with arithmetic operations

Pascal is a typed programming language. This means that the variables that store data are of a specific data type. Those. the program must directly indicate what data can be stored in a particular variable: text data, numeric data, if numeric, then integer or fractional, etc. This is necessary in the first place so that the computer "knows" what operations can be performed with these variables and how to perform them correctly.

For example, the addition of textual data, or as it is correctly called in programming - concatenation - is the usual merging of strings, while the addition of numeric data occurs bit by bit, in addition, fractional and integer numbers are also added in different ways. The same applies to other operations.

Consider the most common data types in Pascal.

Integer Data Types in Pascal

Type Range Required memory (bytes)
byte 0..255 1
shortint -128..127 1
integer -32768.. 32767 2
word 0..65535 2
longint -2147483648..2147483647 4

It must be borne in mind that when writing programs in Pascal integer(translated from English as an integer) is the most commonly used, since the range of values ​​is the most in demand. If a wider range is needed, use longint(long integer, translated from English long integer). Type byte in Pascal it is used when there is no need to work with negative values, the same goes for the type word(only the range of values ​​here is much larger).

Examples of how variables are described (declared) in Pascal:

program a1; varx,y:integer; (integer type) myname:string; (string type) begin x:=1; y:=x+16; myname:="Peter"; writeln ("name: ",myname, ", age: ", y) end.

Result:
name: Peter, age: 17

Comments in Pascal

Pay attention to how Pascal comments are used. In the example, comments, i.e. service text that is “not visible” to the compiler is enclosed in curly braces. Typically, comments are made by programmers to clarify code snippets.

Task 3. The population of Moscow is a = 9,000,000 inhabitants. The population of New Vasyukov is equal to b=1000 inhabitants. Write a program that determines the difference in the number of inhabitants between two cities. Use Variables

Real Data Types in Pascal

Real numbers in Pascal and in general in programming is the name of fractional numbers.

Type Range Required memory (bytes)
real 2.9*10E-39..1.7*10E38 6
single 1.5 * 10 E-45 .. 3.4 * 10E38 4
double 5*10E-324..1.7*10E308 8
extended 1.9 * 10E-4951 .. 1.1 * 10E4932 10

The real type in Pascal is the most commonly used of the real types.

Above were presented simple data types in Pascal, which include:

  • Ordinal
  • whole
  • brain teaser
  • Symbolic
  • enumerated
  • Interval
  • Real

To output the values ​​of variables of a real type, formatted output is usually used:

  • the format uses either one number, which means the number of positions assigned to this number in exponential form;
  • p:=1234.6789; WriteIn(p:6:2); (1234.68)

    Along with simple types, the language also uses structured data types and pointers, which will be the subject of subsequent Pascal lessons.

    Constants in Pascal

    Often, the program knows in advance that the variable will take on a particular value and not change it throughout the execution of the entire program. In this case, you must use a constant.

    The declaration of a constant in Pascal occurs before the declaration of variables (before the service word var) and looks like this:

    An example of a constant description in Pascal:

    1 2 3 4 5 6 const x= 17 ; var myname: string ; begin myname: = "Peter" ; writeln ("name: " , myname, ", age: " , x) end .

    const x=17; varmyname:string; begin myname:="Peter"; writeln ("name: ", myname, ", age: ", x) end.

    "Beautiful" output of integers and real numbers

    In order to leave indents after the output of the values ​​of the variables, so that the values ​​do not “merge” with each other, it is customary to indicate through a colon how many characters need to be provided for the output of the value:


    Arithmetic operations in Pascal

    Order of Operations

    1. evaluation of expressions in brackets;
    2. multiplication, division, div, mod from left to right;
    3. addition and subtraction from left to right.

    Pascal Standard Arithmetic Procedures and Functions

    Here it is worth dwelling in more detail on some arithmetic operations.

    • The inc operator in Pascal, pronounced as increment, is the Pascal standard procedure, which means increment by one.
    • Inc operation example:

      x:=1; inc(x); (Increments x by 1, i.e. x=2) writeln (x)

      A more complex use of the inc procedure:
      Inc(x,n) where x is an ordinal type, n is an integer type; the procedure inc increments x by n.

    • The Dec procedure in Pascal works similarly: Dec(x) - decreases x by 1 (decrement) or Dec(x,n) - decreases x by n.
    • The abs operator represents the modulus of a number. Works like this:
    • a: =- 9 ; b:=abs(a); (b=9)

      a:=-9; b:=abs(a); (b=9)

    • The div operator in Pascal is a commonly used one, since a number of tasks are associated with the division integer action.
    • The remainder of the division or the mod operator in pascal is also indispensable for solving a number of problems.
    • Noteworthy is Pascal's standard odd function, which determines whether an integer is odd. That is, it returns true (true) for odd numbers, false (false) for even numbers.
    • An example of using the odd function:

      varx:integer; beginx:=3; writeln(sqr(x)); (answer 9) end.

    • Operation exponentiation in Pascal missing as such. But in order to raise a number to a power, you can use the exp function.
    • The formula is: exp(ln(a)*n) , where a is a number, n is a power (a>0).

      However, in the pascal abc compiler, exponentiation is much easier:

      varx:integer; beginx:=9; writeln(sqrt(x)); (answer 3) end.

    Task 4. The dimensions of a matchbox are known: height - 12.41 cm, width - 8 cm, thickness - 5 cm. Calculate the base area of ​​the box and its volume
    (S=width * thickness, V=area*height)

    Task 5. There are three elephants and quite a lot of rabbits in the zoo, and the number of rabbits changes frequently. An elephant is supposed to eat a hundred carrots a day, and a rabbit two. Every morning the zookeeper reports the number of rabbits to the computer. The computer, in response, should tell the attendant the total number of carrots that today need to be fed to rabbits and elephants.

    Task 6. It is known that x kg of sweets a rubles. Determine how much y kg of these sweets, as well as how many kilograms of sweets can be bought on k rubles. All values ​​are entered by the user.

    The concept of data is one of the key concepts in programming, and in general in computer science. Roughly speaking, data in computer science is information that is in a state of storage, processing or transmission, at some point in time. In Turing machines, information has a type, which, in turn, depends on the type of information.

    Data types in Pascal define the possible values ​​of variables, constants, expressions and functions. They are built-in and custom. Built-in types are native to the programming language, while custom types are created by the programmer.

    According to the way of representation and processing, data types are:

    • simple
    • structured
    • pointers
    • objects
    • procedures

    In this article, only the most simple data types will be considered, since at the initial stages of training, it will be easier for your program to do without, for example, files and records than without integer or string variables.

    integer type

    This includes several integer types, which differ in the range of values, the number of bytes allocated for their storage, and the word with which the type is declared.

    Type Range Size in bytes
    shortint -128…127 1
    integer -32 768…32 767 2
    longint -2 147 483 648…2 147 483 647 4
    byte 0…255 1
    word 0…65 535 2

    You can declare an integer variable in the Var section, for example:

    All arithmetic and logical operations can be performed on variables of this category, with the exception of division (/), which requires a real type. Some standard functions and procedures can also be applied.

    Real type

    Pascal has the following real data types:

    Type Range Memory, bytes Number of digits
    Real 2.9e-39 … 1.7e38 6 11-12
    Single 1.5e-45 … 3.4e38 4 7-8
    Double 5.0e-324 …1.7e308 8 15-16
    Extended 3.4e-4932 … 1.1e493 10 19-20
    Comp -9.2e63 … (9.2e63)-1 8 19-20

    More operations and functions can be performed on them than on integers. For example, these functions return a real result:

    sin(x) - sine;

    cos(x) - cosine;

    arctan(x) – arc tangent;

    ln(x) – natural logarithm;

    sqrt(x) is the square root;

    exp(x) is the exponent;

    boolean type

    A variable that has a boolean data type can take only two values: true (true) and false (false). Here, true corresponds to the value 1, and false is identical to zero. You can declare a boolean variable like this:

    Comparison operations and logical operations can be performed on data of this type: not , and, or, xor.

    Character type

    A character data type is a collection of characters used in a particular computer. A variable of this type takes the value of one of these symbols and occupies 1 byte in the computer's memory. Word Char defines a value of this type. There are several ways to write a character variable (or constant):

    1. as a single character enclosed in apostrophes: 'W', 'V', 'n';
    2. by specifying the character code, the value of which must be in the range from 0 to 255.
    3. using the ^K construction, where K is the control character code. The value of K must be 64 greater than the code of the corresponding control character.

    The relational operators and the following functions apply to values ​​of the character data type:

    Succ(x)- returns the next character;

    pred(x)- returns the previous character;

    Ord(x)- returns the value of the character code;

    Chr(x)- returns the value of a symbol by its code;

    upcase(x)- converts characters from the interval 'a'..'z' to upper case.

    For fruitful work with the character type, I recommend using .

    string type

    A string in Pascal is a sequence of characters enclosed in apostrophes, and is denoted by the word String. The number of characters (string length) must not exceed 255. If the string length is not specified, then it will automatically be determined as 255 characters. The general form of a string variable declaration looks like this:

    Var<имя_переменной>:string[<длина строки>];

    Each character in a string has its own index (number). The index of the first byte is 0, but it does not store the first character, but the length of the entire string, which means that a variable of this type will take 1 byte more than the number of variables in it. The number of the first character is 1, for example, if we have the string S='stroka', then S=s;. In one of the following lessons, the string data type will be discussed in more detail.

    enumerated data type

    An enumerated data type represents some limited number of identifiers. These identifiers are enclosed in parentheses and separated from each other by commas.

    Type Day=(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

    Variable A can only take values ​​defined in the Type section. You can also declare an enumerated type variable in the Var section:

    Var A: (Monday, Tuesday);

    Relational operations are applicable to this type, while it is predetermined that Monday

    interval data type

    When it is necessary to specify a range of values, then in such situations, the interval data type is used. The construction is used to declare m..n, where m is the minimum (initial) value, and n– maximum (final); here m and n are constants, which can be integer, character, enum, or boolean. Values ​​of an interval type can be described both in the types section and in the variable declaration section.

    General form:

    TYPE<имя_типа> = <мин. значение>..<макс. значение>;

    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 word is used, byte is allowed (but not vice versa), longint "includes" integer, which, in turn, includes 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 (highest) 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(Lying).

    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).

    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 ones, are called ordinal types. For values ​​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
    bytes 0 .. 255 unsigned 1
    Word 0 .. 65535 unsigned 2

    3.2.2. boolean type

    The standard logical type Boolean (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 true:
    False Ord (False) = 0
    Order (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 are introduced for unification with other programming languages ​​and with the Windows environment. Their difference from the standard Boolean type lies in the actual value of the parameter of this type, which corresponds to the True value. For all Boolean types, the False value corresponds to the number 0 written in the corresponding number of bytes. On the other hand, a True value for the Boolean type corresponds to the number 1 written into its byte, and for other types, the True value 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 the full set of ASCII characters. The Ord function of a Char value gives the code of the corresponding character. Values ​​of character type are compared by their codes.

    3.2.4. Enumerated type

    An enumerated type is non-standard and is defined by a set of identifiers that the parameter values ​​can match. The list of identifiers is indicated in parentheses, the 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 a serial number of 0, the second - 1, and so on. The same identifier can be used in the definition of only one enumerated type. The Ord function of 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, it is possible to distinguish a subset of values, determined by the minimum and maximum values, which includes all values ​​of the original type that are within these boundaries, including the boundaries themselves. Such a 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 must not be greater than the maximum.

    Example. Definition of range types.

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

    3.2.6. Real types

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

    Type Range Number of significant digits 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

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

    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 subsections 17.5.8 and 17.7.1).

    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.