Previous Table of Contents Next


5.2.4. Data Types

This section describes most of the data types usable in Turbo Pascal. They include types to handle numerical data; types to handle character and boolean data; types to handle grouped data such as arrays, strings, records, and sets; as well as a description of enumerated types, constants, and typed data.

5.2.4.1. Numeric Data

Numeric data can be stored in a variety of ways. The most common types are integer, which represents a signed number between –32,767 and 32,768, and real, which represents a scientific number of 11 significant figures. The real numbers may be set in terms of scientific numbers or as decimal numbers. A complete listing of numeric data types and their ranges can be found in the online help or manuals. The following are other common types of numeric representative data types:

  byte—regular storage for a byte of data: 0 to 255; 1 byte storage
  word—regular storage for a word of data: 0 to 65,535; 2 bytes storage
  integer—signed numerical storage: –32,767 to 32,768; 2 bytes storage
  longint—signed numerical storage with an even larger range than integer: 4 bytes storage
  real—scientific number, 11 digits precision, storage from about 10-30 to 1030, 6 bytes storage

Some of the numeric types involve use of the numeric co-processor or runtime emulation routines that are inherent to Turbo Pascal. What you may want to use in this case is something referred to as a compiler directive to determine which way TP handles these kinds of numeric types. Compiler directives are commented codes that appear in a source program, generally at the beginning. Relevant compiler directives in this case are $N and $E. The defaults are $N- and $E+, which uses emulation by default. The + condition on each of these compiler directives signals to use either the numeric co-processor (N) or the emulation (E), whereas the - condition disables use of either.

The real data in Listing 5.4 for division resulted in output of the number in a scientific number format. In most cases of output, you may not want to output numbers for users in that format. Therefore, there is another formatting code with write and writeln that enables formatting. It is a second number, after the spacing format code, that specifies the number of digits when showing the number as a decimal. When spacing is not desired, a 0 can be used as the first character, but both formatting codes must still be used. For example, a real number can be written as a decimal number like the following:

  { write the real variable result, to 3 decimal places }
  writeln(result:0:3);

5.2.4.2. Character or Boolean Data

A byte of data, translated to a character in the ASCII chart is referred to as a data type char. A character can be assigned to a char variable, such as ‘A’ or an equivalent ASCII number in decimal as #65 or hexadecimal such as $41. In Listing 5.12, note that even though byte and char are entirely equivalent in storage (1 byte, same value in memory), the compiler interprets these types differently. To check this fact, information is shown later (Listing 5.24) to enable you to test the statement made.

Listing 5.12. An illustration of byte versus char.

  program fig12;
    var
      character: char;
      byteinfo: byte;
    begin
      character := #65; { or character := ‘A’ would be equivalent }
      byteinfo := 65;
      writeln(character);
      writeln(byteinfo);
    end.

Another type of data called boolean data is stored as a byte, $00 or $FF. This type is referred to as either true or false and is assigned as such in a boolean variable. These variables are good for status flags generally.


Previous Table of Contents Next