Create a classic "Hello, World" Windows native code Console App using Delphi. This tutorial is based on Borland Developer Suite 2006 but you can use any version of Delphi you wish.
Got 10 minutes? Want to get started with Delphi Console App?
This article is part of our series of 10 Minute Quick Starts. Each quick start is step by step, assumes you know very little about the subject, and takes about 10 minutes. You can use them to scratch the service of areas you want to learn and as a quick review when returning to something after a long absence.
Console Apps
For console applications, set $APPTYPE to CONSOLE.
--Mike Prestwood
In this tutorial, you will create a classic "Hello, World!" windows console application. A console application is a type of Windows application that has FULL access to the Win32 API, but it's GUI is limited to a DOS-like text window. When Windows starts a console application, it creates a text-mode console window where the program can display text and the user can interact with the program via the keyboard.
Setup
A console application can be as small as about 50KB.
--Mike Prestwood
To get started, you have to have Delphi installed. This article is based on Borland Developer Suite 2006 but you can use any version of Delphi you wish (there may be slight variations in menu options but we try to keep our 10 Minute Quick Starts as generic as possible).
If you don't have Delphi yet, you can download the trial edition for free:
In the dialog box, click Console Application then OK.
Notice the default code has a compiler directive that specifies the type of application (a console application). Alter the begin/end block as follows (see bolded code). Notice the ReadLn command which waits for the user to enter a value and press enter. In this case, we are using it to wait until the user presses ENTER.
About $APPTYPE: For console applications, set $APPTYPE to CONSOLE. When set to console, the compiler generates a console application complete with the Input and Output standard text files automatically associated with it. The other valid value for $APPTYPE is GUI in which the compiler generates a graphical user interface application.
program Hello;
{$APPTYPE CONSOLE}
uses SysUtils;
begin WriteLn('Hello, World!'); WriteLn('Press ENTER to exit.'); ReadLn;
end.
Run-it! Now let's test your very first native code Windows program. You can run it within Delphi (called debugging) or you can run the EXE outside of Delphi. To run it, either click Run | Run, the green trianlge toolbar run icon, or press F9.
That's it! You've created your first console application using Delphi.
Part 2: Uses Clause and the Size of EXE
Replace the three WriteLn and ReadLn code lines with the following code:
ShowMessage('Hello, World!');
Now click Run | Run to attempt to run the program. It will fail! So, don't panic. I wanted to show you what happens when you use a routine that's not currently available because you haven't yet included the needed unit your application. If you lookup the ShowMessage routine in the help system, you will that it is included in the Dialogs unit.
About the Uses Clause: The uses clause lists the units currently available. You can include a uses clause in a program, library, or in a unit (either in the interface or implementation section). These units may in turn have uses clauses of their own. The System unit and the SysInit unit are used automatically by every application and cannot be listed explicitly in the uses clause.
Tip: It's a good idea to open and become familiar with the code in commonly used units and is especially important with the System unit which implements string handling, floating point operations, file I/O, dynamic memory allocation, etc. Refer to the System unit help topic in Delphi's help system for more information.
Alter your uses statement as follows:
program Hello;
{$APPTYPE CONSOLE}
uses SysUtils, Dialogs;
begin ShowMessage('Hello, World!'); end.
Now click Run | Run to run the program. Notice the console application is created, a Windows dialog is displayed, and once you click OK, the dialog closes and finally the console windows closes.
Application size. A console application can be as small as about 50KB. Including Dialogs and using the ShowMessage routine increased the size of the application to include some Windows GUI overhead code. On my computer, it increased from 44KB to 404KB. Dialogs includes a lot more code than the 400KB in it and in the Pascal units it includes but the compiler optimizes out code you don't use. Because Windows GUI applications have a bit of overhead, you're simple console applications grew by nearly 10x.
Part 3: Using ReadLn with WriteLn
In this final console appliccation variation, we'll use ReadLn along with WriteLn to gather some information from the user. For good measure, we'll introduce Delphi's if statement and single and multi-line comments. Since Delphi is a strong typed language, we'll also use IntToStr to convert an integer to a string.
Using what you learned so far, create a new console application using the following code:
program Hello;
{$APPTYPE CONSOLE}
uses SysUtils, Dialogs;
var FirstName: String; Age: Integer; begin //Gather name. WriteLn('What is your name?'); ReadLn(FirstName);
//Gather age. WriteLn('What is your age?'); ReadLn(Age);
WriteLn('Hi ' + FirstName + '. You are ' + IntToStr(Age));
{ Two apostropies= one in a literal string. Notice no statement ending semi-colon when preceding an else so as to continue the statement. } if Age < 14 then WriteLn('You need a parent''s permission to join our group.') else WriteLn('You are eligible to join our group.');
WriteLn; WriteLn('Enter to exit.'); ReadLn; end.
Click Run | Run to run the program.
What Next?
If you wish to type in a simple console windows game, go to the following link:
I want to learn PHP but I‘m so caught up in my homework. Only if I could get help with statistics homework online then I can learn it. I will come to this exact website to learn online.