Delphi Programming/Data types

First, let's define a type.

In Delphi, like in C, VB and unlike PHP, Python or other modern languages, a variable is typed and can only contain values of this type. To make it simple, a type is a variable attribute.

Basic typesEdit

Delphi is extremely complete for this. There are lots of predefined types and you can also create your own types (we will see it later). Here is a table with the most useful basic types (also called primitive types):

Type Description Size Value
boolean boolean 1 bit True/False
char character 1 byte One item of the ASCII table (or ANSI)
string string of characters Depends on its number of characters Dynamic table of items of the ASCII table (or ANSI)
integer positive or negative integer 4 bytes -2147483648 to 2147483647
cardinal unsigned integer 4 bytes 0 to 4294967295
real (equivalent to double) real number 8 bytes 5.0 x 10^-324 to 1.7 x 10^308
Remarks: Above are only the most popular predefined types. There are dozens of other types just for the integers (smallint, longint, int64...) whose usage fits very specific needs (like the size of the data we will put in) and so optimizes the memory usage. For more information about it, see the documentation in the Delphi help content.


A char (a character) is something like '#', 'A', '2', ';', '.', '?' and so on. It has a number in a library of chars, the ASCII. In Delphi, a char is typed between two single quotes. If the given character is the single quote itself, you should double the character, in other words, you have to type four single quotes.

 1 var
 2   a, b: char;
 4 begin
 5   a := 'H';  // a = "H"
 6   b := 'i';  // b = "i"
 7   WriteLn(a);  // Display "H"
 8   WriteLn(a, b);  // Display "Hi"
 9   WriteLn(a, b, '!');  // Display "Hi!"
10 end.


You have already worked with strings in the last chapter, such as 'Hello World' or 'Hi there' and so one. A string is a sequence of chars.

1  var
2    text1: string;
3    character1: char;

And then write between begin and end:

1  character1 := 'H';
2  text1 := 'ello World!';
3  WriteLn(character1, text1);

If you want to write a single quote in a string, the best way is to type two successive single quotes:

1  text1 := 'I''m John Doe.';

Strings behave like the tables. You can access to a character using an index:

1  text := 'Hello World!';
2  WriteLn(text[1]); // Only print "H".

The first index is 1 (and not 0).

Textual expressionsEdit

Strings can be linked using the + operator:

1  firstWord := 'Hello';
2  secondWord := 'World';
3  WriteLn(firstWord + ' ' + secondWord + '!');
4  wholeText := firstWord + ' ' + secondWord + '!';

The + operator can be used with variables with different types. According to the types, the result will have a different type:

  • string + string → string
  • string + char → string
  • char + char → string
  • char + string → string
  • string + number → error
  • number + string → error
  • number + number → number
  • number + char → error
  • char + number → error


Integers are even integers, you write:

1  a: Integer;


Floats are decimal numbers. They use a floating point. You can write:

1  c: Real;   // Real is normally the same like Single
2  a: Single;
3  b: Double;
4  d: Extended;

An Extended is more precise than a Real, but needs more main storage.


Boolean is a logical value. It can be 'True' or 'False'. Write:

1  var
2    a: Boolean;
3  begin
4    a := True;
5    // or
6    a := False;

But there is only a sense of Boolean when you know the conditional structures in the next chapter.


An array is a collection of strings, floats, integers or any data type. It can also contain an array. In this case, we have an array with several dimensions. You say:

1  a: array [] of data_type;
2   a: array [1..3] of string;

for one dimension. For two:

1  a: array [fX..tX] of
2     array [fY..tY] of data_type;

and so on. A single element of an array is for example:

1  a[1] := 'Hello '
2  a[2] := 'World'
3  a[3] := '!';
4 WriteLn(a[1],
5          a[2],
6          a[3]);


A record (also called a structure in other languages) represents a set of heterogeneous data. Each item is a field. The record type declaration specifies the name and the type of each field. By convention, the name of a record type starts with a "T". A record type declaration has the following syntax:

1 type recordTypeName= record
2 listField1: type1;
3  ...
4 listFieldn: typen;
5 end

For example, the following declaration creates a record type called TDateRec.

1 type
2   TDateRec = record
3     Year: Integer;
4     Month: (Jan, Feb, Mar, Apr, May, Jun,
5             Jul, Aug, Sep, Oct, Nov, Dec);
6     Day: 1..31;
7   end;

Each TDateRec contains three fields: an integer value called Year, an enumerate value called Month and another integer value between 1 and 31 called Day. The identifiers Year, Month and Day are names of fields of TDateRec that behave as variables. However, the TDateRec type declaration doesn't allocate memory for the Year, Month and Day fields. The memory is allocated when you instantiate a record, which you do as follows:

1 var Record1, Record2: TDateRec;

The variable declaration creates two instances of TDateRec, called Record1 and Record2.

You can access to a record field by qualify the name of the field with the name of the record:

1 Record1.Year := 1922;
2 Record1.Month := Nov;
3 Record1.Day := 26;

Or by using the with instruction:

1 with Record1 do
2 begin
3   Year := 1922;
4   Month := Nov;
5   Day := 26;
6 end;


The following example asks a question, retrieves the answer and prints the answer:

1  var
2  _Input, _Linkage: string;
3  begin
4    WriteLn('Hello, what is your name?');
5    ReadLn(_Input);
6    _Linkage := 'Hello, ' + _Input;
7    WriteLn(_Linkage);
8  end.

ReadLn(_Input) means that the application waits for the user to enter a text and then saves the text in the variable _Input.

This other example gets the number of a char in the ASCII:

 1  var
 2    number: Integer; // Declare number as an integer
 3    _Input: string;
 4    _Char1: char;
 5  begin
 6    WriteLn('Enter a char!');
 7    ReadLn(_Input);
 8    _Char1 := _Input[1];
 9    number := Ord(_Char1);
10    WriteLn('ASCII is: ', number);
11  end.

With _Input[1] you get the first char of _Input. And the first char is char 1, (not char 0 like in C++!)