Delphi Tutorial

Console Applications, part 2: Structure; handling Input and Output

Structure of a Delphi Console Application

In our previous lesson Setting up and compiling a Delphi Console Application, we wrote this very basic example:

program ConsoleTest; 
uses SysUtils;

  WriteLn('Program ConsoleTest is running.');
  WriteLn('Press the ENTER key to stop');

Let's have a look at the meaning of the different keywords in the source code:

  • program means that this unit as the program's main source unit, the .DPR file. This keyword is followed by the name of the project file, without the extension .DPR.
    When you compile a project, Delphi uses the name of the project file for the name of your EXE file that it creates.
  • $APPTYPE controls whether to generate a console application or a graphical UI application (UI = User Interface). Here, the {$APPTYPE CONSOLE} directive tells the compiler to generate a console application.
  • uses is followed by a list of all the units that the unit ConsoleTest uses, that is: the other units that are part of the project.
    We see that Delphi included the SysUtils unit. Also another unit is included, the System unit, but since System is included in every Delphi program, it's not necessary to mention it in the uses directive.
  • In between the keywords begin and end you add your code.
    Note that the last end keyword is always followed by a dot character -- also called point, period, or final stop. As you've probably guessed ;) this indicates the end of the project file.

Handling Input and Output

In a console application, you don't use VCL controls for input and output. Communication with the user is handled with Read and Write commands:

  • WriteLn displays a message, followed by an end-of-line code that positions the text cursor at the beginning of the next line.
    Example: display the line of text Program ConsoleTest is running :

    WriteLn('Program ConsoleTest is running');
  • ReadLn inputs keystrokes, until the ENTER ("return") key is pressed.
    Example: read data until Enter is pressed and put in variable S:


    Example: simply wait until Enter is pressed:

  • Write displays data without an end-of-line.Thus, the ouput of the next Write command will appear directly after the displayed text.
    Example: display 3 strings on the same line:

    Write('one ');   // note the space after the word 'one'
    Write('two ');   // note the space after the word 'two'

  • Read to directly input data to one or more variables. The Read command waits until the number of data values that are typed (separated by spaces, if there is more than 1 value) is equal to the number of variables provided. Sounds complicated, doesn't it? That's why Read isn't used often, most input is done with ReadLn.
    Example to input a single character to variable C, given that C is of type "char" (press ENTER afterwards) :


To be continued...

Part 1: Setting up and Compiling a Console Application

Source Code and Tutorials :: Crash Course Delphi
FAQ :: DelphiLand Club :: DC Library :: Tips :: Downloads :: Links

Copyright 1999-2017