Back to DFS's Pascal Page


Data Types in Turbo Pascal

(Preliminary)

Follow these fast links:

Back to Top

Char

The Char data type is used for individual 8-bit codes, many of which are used to represent printable characters. Each Char requires one byte of memory and can have a value from 0 to 255 (or 0 to 11111111 in binary). This data type, along with Integer and Boolean, belongs to the class of data types known as ordinals. An ordinal is one of a set of values, each of which has both a predecessor and a successor except for the first and the last. In Pascal, a Char literal is delimited by single quotes, e.g., 'C'.

A String can be viewed as an array of Chars.

Many different encoding schemes have been used to represent characters on computers. Below are two of the most widely used, ASCII and EBCDIC.

Back to Top

ASCII

The original American Standard Code for Information Interchange (ASCII) used only 7 of the bits, with values from 010 to 12710 or 016 to 7F16. The first 32 codes and the last one are for controling communication and peripherals. The other 95 codes are for printable characters. They were intended to handle English only and they do not even do that well. Note that the hexadecimal value for a character is composed of the column value as the high-order digit and the row value as the low-order digit, e.g., the value for lowercase 'a' is 6116. The space character is 2016. Click here to see what the control code mnemonics for the codes 0016 to 1F16 stand for.

ASCII CHART
  01234567
0NULDLE 0@P`p
1SOHDC1!1AQaq
2STXDC2"2BRbr
3ETXDC3#3CScs
4EOTDC4$4DTdt
5ENQNAK%5EUeu
6ACKSYN&6FVfv
7BELETB'7GWgw
8BSCAN(8HXhx
9HTEM)9IYiy
ALFSUB*:JZjz
BVTESC+;K[k{
CFFFS,<L\l|
DCRGS-=M]m}
ESORS.>N^n~
FSIUS/?O_oDEL

Back to Top

EBCDIC

An earlier, but now rarely used, encoding system was the Extended Binary Coded Decimal Interchange Code (EBCDIC), which was used on IBM mainframes. It reflected the system of holes used on punch cards. It has fallen into disfavor partially because of the use of nonsequential codes for the alphabetic characters. Note that the hexadecimal value for a character is composed of the row value as the high-order digit and the column value as the low-order digit, e.g., the value for lowercase 'a' is 8116. The space character is 4016. The nonindented sections of the table indicate unused codes. Click here to see what the control code mnemonics for the codes 0016 to 3F16 stand for.

EBCDIC CHART
  0123456789ABCDEF
0NULSOHSTXETXPFHTLCDELSMMVTFFCRSOSI
1DLEDC1DC2DC3RESNLBSILCANEMCCIFSIGSIRSIUS
2DSSOSFSBYPLFETBESCSMENQACKBEL
3SYNPNRSUCEOTDC4NAKSUB
4 ¢.<(+|
5&!$*);¬
6-/^,%_>?
7:#@'="
8abcdefghi
9jklmnopqr
Astuvwxyz
B
C{ABCDEFGHI
D}JKLMNOPQR
E\STUVWXYZ
F0123456789

Back to Top

Boolean

A variable of the Boolean type can have only two values, TRUE and FALSE. A good compiler, therefore, can use a single bit for a boolean variable, packing up to eight such variables into a single byte of memory. Zero (0) is used to represent FALSE and one (1) is used for TRUE.

A boolean is one of the ordinal types. Internally, FALSE is treated like 0 and TRUE like 1. So TRUE is the successor of FALSE and FALSE is the predecessor of TRUE.

The result of an evaluation of a Boolean expression can be assigned to a Boolean variable, e.g.,

boo : Boolean;

boo := 5 < 4;
writeln(boo);

will result in the word FALSE being printed.

Booleans are typically used as flags to store what a current condition is. This can be seen in a reworking of the program on the User-Controlled Repetition with the Repeat-Until Loop page.

QuitProgram : boolean; {Loop control variable}

Repeat
   .
   .
   .
   {-------------------User Control------------------}
   write('Do you want to do another? (Y/N) ');
   readln(response);
   if (upcase(response) = 'N') then
      QuitProgram := TRUE
Until QuitProgram

Back to Top

Integer

An Integer is a two-byte ordinal data type. The range possible is from -32768 to 32767. The largest value is assigned to the Pascal constant MaxInt. In the binary representation, the highest-order bit of the 16-bit value is known as the sign bit -- if it is a one (1), the value is negative.

A error which frequently occurs in programming is the choice of a data type which is too small to hold a particular value. Consider the following code, for example.

x : Integer;

x := MaxInt;
x := x + 1;
writeln(x)

This code will print the value -32768. This is known as overflow. Examine the following table.

Integer Range of Values
 Smallest ValueLargest Value
Decimal-3276832767
Binary1000 0000 0000 00000111 1111 1111 1111
Hexadecimal80007FFF

The following binary addition demonstrates how the value stored in x became negative.

 0111 1111 1111 1111
                  +1
--------------------
 1000 0000 0000 0000

The result of the addition is negative, as can be seen by the highest-order bit being a 1. The resultant value is the binary representation of the smallest value that can be stored in an Integer variable.

Back to Top

Longint

An LongInt is a four-byte ordinal data type, making it twice as long as an Integer in terms of bits. The range possible is from -2147483648 to 2147483647. The largest value is assigned to the Pascal constant MaxLongInt. In the binary representation, the highest-order bit of the 32-bit value is known as the sign bit -- if it is a one (1), the value is negative.

x : LongInt;

x := MaxLongInt;
x := x + 1;
writeln(x)

This code will print the value -2147483648. This is known as overflow. The following table shows the smallest and largest values in decimal, binary and hexadecimal that can be stored in a LongInt.

LongInt Range of Values
 Smallest ValueLargest Value
Decimal-21474836482147483647
Binary1000 0000 0000 0000 0000 0000 0000 00000111 1111 1111 1111 1111 1111 1111 1111
Hexadecimal800000007FFFFFFF

Back to Top

Real

A Real is a six-byte non-ordinal data type. This means that it cannot be used as the loop control variable in a for-to-do loop.

The range of a positive Real variable is from 2.9 x 10-39 to 1.7 x 1038; approximately the same holds for negative values. A Real maintains about 11 significant digits.

Consider the following code fragment:

x : Real;

x := 13.2;
writeln(x);

This will result in 1.3200000000E+01 being printed. This is know as E-notation and is equivalent to 1.32 x 101 in scientific notation.

Since normal people find reading such notation bothersome, Pascal allows you to specify the number of decimal places to be used in the output. For example, the following

writeln(x:1:2)

would print 13.20 as output. The value after the first colon specifies the field width and the second specifies the number of decimal places to be used. If the first value is less than the total length of the string being printed, the string is printed quad left (left justified); if the first value is more than the total length, the string is printed quad right (right justified) within the field. Thus, decimal values such as those used for money can be made to line up:

x : Real;
y : Real;

x := 13.2;
y := 11;
writeln(x:7:2);
writeln(y:7:2);

would result in the following output, where the carets are used to indicate wordspaces.

^^13.20
^^11.00

Back to Top


Back to ASCII
ASCII CONTROL CODES
00NULNULl10DLEData Link Escape
01SOHStart Of Heading11DC1Device Control 1
02STXend of heading or Start of TeXt12DC2Device Control 2
03ETXEnd of TeXt13DC3Device Control 3
04EOTEnd Of Transmission14DC4Device Control 4
05ENQENQuiry (to request identification)15NAKNegative AcKnowledge
06ACKACKnowledge16SYNSYNchronouos idle
07BELring BELl17ETBEnd of Transmission Block
08BSBackSpace18CANCANcel previous transmission
09HTHorizontal Tab19EMEnd of Medium
0ALFLine Feed1ASUBSUBstitute a character for another
0BVTVertical Tab1BESCESCape
0CFFForm Feed1CFSFile Separator
0DCRCarriage Return1DGSGroup Separator
0ESOShift Out (begin non-ASCII bit string)1ERSRecord Separator
0FSIShift In (end non-ASCII bit string)1FUSUnit Separator

Back to Top


Back to EBCDIC
EBCDIC CONTROL CODES
00NULNULl10DLEData Link Escape20DSDigit Select30  
01SOHStart Of Heading11DC1Device Control 121SOSStart Of Significance31  
02STXend of heading or Start of TeXt12DC2Device Control 222FSFile Separator32SYNSYNchronous idle
03ETXEnd of TeXt13DC3Device Control 323  33  
04PFPunch ofF14RESREStore24BYPBYPass34PNPunch oN
05HTHorizontal Tab15NLNewLine25LFLine Feed35RSRecord Separator
06LCLower Case16BSBackSpace26ETBEnd of Transmission Block36UCUpper Case
07DELDELete17ILIdLe27ESCESCape37EOTEnd Of Transmission
08  18CANCANcel previous transmission28  38  
09  19EMEnd of Medium29  39  
0ASMMrepeat1ACCunit backspace2ASMStart Message3A  
0BVTVertical Tab1B  2B  3B  
0CFFForm Feed1CIFSInterchange File Separator2C  3CDC4Device Control 4
0DCRCarriage Return1DIGSInterchange Group Separator2DENQENQuiry (to request identification)3DNAKNegative AcKnowledge
0ESOShift Out (begin non-EBCDIC bit string)1EIRSInterchange Record Separator2EACKACKnowledge3E  
0FSIShift In (end non-EBCDIC bit string)1FIUSInterchange Unit Separator2FBELring BELl3FSUBSUBstitute a character for another


© 2000-2002 DFStermole
Created 7 Jan 2000
Modified 23 Jan 02