Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Basics and Types

Integer and Real Types in Pascal

Learn Pascal's numeric types: Integer, Byte, Word, Int64, Cardinal, Single, Double and Extended, their ranges, overflow, div and mod, conversions and formatting.

Numbers are the raw material of most programs, and Pascal gives you a family of numeric types so you can pick the right size and kind. Whole numbers use integer types; numbers with fractions use real types. Choosing well saves memory and avoids surprises such as overflow and rounding errors.

This lesson lists the types, shows their ranges, explains div, mod and the / operator, and warns you about the usual traps. Pascal listings are read-only. The C stand-ins at the end let you explore the same ideas, such as wrap-around and floating-point rounding, on this site.

Integer types

The sizes below are those of Free Pascal and Delphi on 32-bit and 64-bit systems.

Type Size Range
ShortInt 8 bits -128 to 127
Byte 8 bits 0 to 255
SmallInt 16 bits -32,768 to 32,767
Word 16 bits 0 to 65,535
Integer 32 bits (in Delphi and FPC's objfpc/delphi modes) -2,147,483,648 to 2,147,483,647
Cardinal / LongWord 32 bits 0 to 4,294,967,295
Int64 64 bits about -9.2e18 to 9.2e18
UInt64 64 bits 0 to about 1.8e19

Careful: in Turbo Pascal and FPC's default mode Integer is 16 bits. Using {$mode objfpc} or {$mode delphi} makes it 32 bits. When the exact size matters, name it: Int32, Int64, UInt8, and so on.

High(Integer) and Low(Integer) return the largest and smallest value of a type, so you never need to memorize them:

PASCAL
{$mode objfpc}
program Limits;

begin
  WriteLn('Byte:    ', Low(Byte), ' .. ', High(Byte));
  WriteLn('Integer: ', Low(Integer), ' .. ', High(Integer));
  WriteLn('Int64:   ', High(Int64));
end.

Output:

TEXT
Byte:    0 .. 255
Integer: -2147483648 .. 2147483647
Int64:   9223372036854775807

Integer arithmetic

PASCAL
var
  a, b: Integer;
begin
  a := 17;
  b := 5;
  WriteLn(a + b);    { 22 }
  WriteLn(a - b);    { 12 }
  WriteLn(a * b);    { 85 }
  WriteLn(a div b);  { 3  - whole-number quotient }
  WriteLn(a mod b);  { 2  - remainder }
end.

Division has two spellings. div divides two integers and gives an integer (the fraction is dropped toward zero). / always produces a real number, even for integers: 17 / 5 is 3.4. Storing / into an Integer variable is a type error; use div, or convert with Round/Trunc.

mod follows the sign of the left operand: -17 mod 5 is -2. Dividing by zero raises a run-time error, so check the divisor first.

Useful built-ins: Abs(x), Sqr(x), Odd(x), Inc(x), Dec(x), Inc(x, 5), Succ(x) and Pred(x).

Overflow

If a result does not fit the type, the value wraps or an error is raised, depending on the compiler's checks:

PASCAL
var
  b: Byte;
begin
  b := 250;
  b := b + 10;   { 260 does not fit in a Byte }
  WriteLn(b);    { 4 with range checking off; error with {$R+} }
end.

Turning on range and overflow checking while you develop ({$R+} and {$Q+}, or the matching IDE options) turns silent wrap-around into an immediate run-time error. That is the best way to find these bugs.

Real types

Type Size Precision (digits) Notes
Single 32 bits about 7 Small, e.g. graphics data
Double 64 bits about 15-16 The default choice
Extended 80 bits on x86 about 19 Not available everywhere
Real Same as Double in Delphi Avoid in new code
Currency 64-bit scaled integer 4 decimals exact For money in Delphi

Reals are stored in binary, so many decimal fractions are approximations. 0.1 + 0.2 is not exactly 0.3. Never compare reals with =; check that the difference is small:

PASCAL
if Abs(a - b) < 0.000001 then
  WriteLn('close enough');

Round(x) rounds to the nearest integer (halves go to the nearest even number in Delphi and FPC: Round(2.5) is 2), Trunc(x) drops the fraction, Floor(x) and Ceil(x) live in the Math unit.

Formatting numbers

To print reals sensibly use :width:decimals in WriteLn, or the Format function from SysUtils:

PASCAL
uses SysUtils;
...
WriteLn(Format('%d items cost %.2f', [3, 4.5]));
WriteLn(Format('%8.3f|%-8d|', [3.14159, 42]));

Output:

TEXT
3 items cost 4.50
   3.142|42      |

IntToStr(42) gives '42', StrToInt('17') the integer 17 and FloatToStr(3.5) the text of a real number.

Mixed arithmetic

When an integer meets a real in an expression, the integer is promoted to a real first. 5 + 2.5 is 7.5. The result of / is always real. This rule, plus div, covers nearly every case.

Stand-ins in C

Wrap-around and rounding behave much the same in C, so you can explore them here. These programs are C.

C C stand-in: Byte overflow and ranges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C C stand-in: comparing reals safely

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does 17 div 5 produce?

    div is integer division and drops the fraction. 17 mod 5 would give the remainder 2.
  2. 2. What type does the expression 17 / 5 have?

    The slash operator always yields a real result, even if both operands are integers.
  3. 3. Which type holds 0 to 255?

    Byte is an unsigned 8-bit type. ShortInt is signed (-128 to 127) and Word is 16-bit.
  4. 4. Why should you avoid comparing two Double values with =?

    Many decimal fractions are not exactly representable, so compare using a small tolerance.
  5. 5. Which compiler setting turns silent integer wrap-around into a run-time error?

    Range checking and overflow checking add checks that raise errors instead of wrapping.

Summary

  • Integer types differ in size and sign: Byte, Word, Integer, Cardinal, Int64; use High and Low to learn their limits.
  • div and mod work on integers; / always gives a real.
  • Reals are approximations: use Double, compare with a tolerance, and Currency for money.
  • Enable range and overflow checks while developing.
  • Round, Trunc, Format and IntToStr convert and format numbers.

Next: Booleans, Characters and Strings