Visit www.annedawson.net for more Computer Science Education
resources
Last updated: Saturday 28th February
2009, 11:40 PT by AHD
How to Use
The Borland C++ Compiler
Version 5.02
to create C++ and C programs
1. Create a project
directory
First, you
should create a folder (directory) on your local C: drive to save your C++ or C
programs. Go to the C:\temp folder and create a new
folder named myprogs. If you're unsure about how to do this,
click on the Start button on the taskbar and select Help, or ask for
assistance.
For the
purposes of this document, it's assumed that Borland C++ version 5.02 software
is installed and a directory named myprogs exists in the temp directory of the C: drive:
C:\temp\myprogs

2. Start up the Borland software
Double
click on the Borland C++ icon on the desktop:

If the icon is not on the desktop, click on the Start
button and select Programs from the list of options. From the programs
list, click on Borland C++ 5.02 then
Borland C++.
You should then see the following
screen. Don’t worry if the screen is not exactly like yours – but it should
have the Borland C++ icon and
title in the top left corner.

3. Create a new project
Before you start a new project you
should always select Close all then Windows from
the Borland C++ Windows menu.
From the Borland C++ main screen,
click on File then New then Project to get to the New Target window:
This is the Advanced button referred to later in this
document

In the Project Path and
Name: text box, type:
c:\temp\myprogs\prog1.ide
Notice that the Target
Name is automatically taken from what you type.
In the Target Type:
text box click on Application[.exe]
to highlight it.
In the Platform: text
box select Win32.
In the Target Model:
text box select Console.
The remaining settings are as shown
in the New Target window above and are not changed.

Now click on the Advanced button
to see the following window:

Click in the .def and .rc boxes
to deselect them so that the Advanced Options window looks like this:

Note: By default, the .cpp Node option (for C++ programs)
of Advanced Options is selected. If you're creating a C program, you should
select the .c Node option of Advanced Options.
Now click on the OK button of the
Advanced Options window.
Then click on the OK button of the
New Target window to complete the setup for the new project. You will then see the Project window…

Note: If you selected the .c node in
the Advanced Options window, the files names shown in the project window are
prog1.ide and prog1.c.
The project is called prog1. The project file is prog1.ide in the c:\temp\myprogs directory. Within this
project is a C++ (or C) file
: prog1.cpp (or prog1.c). This is the source code file, the one
that contains the text of the C++ or C program. prog1.exe is the name of the executable file to be generated
from prog1.cpp (or prog1.c).
Now click on the filename prog1.cpp
( or prog1.c) to highlight it, and press the Enter key to open the file. You will see a code window for
C:\temp\cpp\prog1.cpp (or prog1.c).
The code window is where you to enter the text of your program.

Close the Project window and
maximize the code window. In the code window, type in the text EXACTLY as shown below.
C++ programmers type in the code
shown in blue below.
C programmers should type in the green code below.
// Programmer:
// Date:
// File:
c:\temp\myprogs\prog1.cpp
#include
<conio.h>
void main (void)
{
int num1, num2, total;
cout << "type a number then
press the Enter key\n";
cin >> num1;
cout << "type another number
then press the Enter key\n";
cin >> num2;
total = num1 + num2;
cout << "The sum is ";
cout << total;
cout << "\n\n\nPress any key
to end this program";
getch();
}
// Programmer:
// Date:
// File: c:\temp\myprogs\prog1.c
#include <stdio.h>
#include <conio.h>
void main (void)
{
int num1, num2, total;
printf("type a number then press the Enter key\n");
scanf("%d",&num1);
printf("type another number then press the Enter key\n");
scanf("%d",&num2);
total = num1 + num2;
printf("The sum is ");
printf("%d",total);
printf("\n\nPress any key to end this program...");
getch();
}
4. Save the project

Notice in the title bar of the code
window, there is a * after the source code file name. This means that the file has yet to be saved. To save the file, Click on File then Save.
4. Compile your project
To compile your source code (convert
it to machine code) click on Project, and then Compile. If you have typed everything in
correctly, you should see the following window (or a similar window for
prog1.c).

Click on OK.
If there are any errors reported in
this window, the program will not be compiled to machine code. You will need to
find and correct the error, then compile again.
5. Run (execute) project
Once you have 0 errors, you then
have code which will run, once it is linked to the necessary library
files. To compile, link and run a
program which you know has zero errors, you can simply click on the lightening bolt
button in the tool bar (the sixth button from the left which has a yellow wavy
line on it).
Click on the lightning bolt
button. You will see the following
window…

and notice that there is a new
button in the task bar at the bottom of your desktop showing that the
executable code of this program (prog1.exe) is running.
Move the mouse over the window and
click to make it the active window, then:
Type in a number.
Then press the Enter key.
Now type in another number…
Now press the Enter key…
The sum of the two numbers is
displayed. You are then prompted to press any key to end the program. When the
program ends you are returned to the integrated development environment.
Go to the C:\temp\myprogs folder and note the files which have been created for you: the file with the .ide extension is the
project file, the .cpp (or .c) file is the source code file, (a text file which
can be opened in Notepad.exe) and assuming successful compilation, there is
also a file named prg1.obj which is the object file, and a file named prog1.exe
which is the executable file. The executable file may be run by selecting Run
from the Debug menu.
There are other files which have
been automatically generated by the integrated development environment (IDE),
which need not concern us now.
Optional Additional Notes
To set up the compiler for debugging (finding errors in code)
The
following example uses a C++ project, but same method applies to C projects. With the code window open, move your mouse pointer to
the left hand grey column. Notice that the pointer turns into a
white arrow and black circle. Move
the black circle so that it is positioned opposite the line:
total = num1 + num2;
then
click. You will see a red line as
shown in the following display:

The red
line marks a stop point. When you
run the program by pressing the lightening bolt button, the execution stops at
the red line, and Borland opens the code window highlighting the stop line. To
continue running the program one line at a time, when the code window is the
active window, press key F8 (which is usually positioned above key 9 on the
keyboard). Notice that when a
program is running, a button appears on the task bar (the task bar is normally situated
to the right of the Start button on the desktop). When the program is waiting for input, the console
window is active. The console window displays your running program. On the
taskbar, the console window button displays the name of the running
program. You may have to click
inside the console window before you type any required input. To get back to the code window, click
on the Borland C++ button on the taskbar.
After pressing F8 while in the code window, click on the console window
button to look at the current state of your program in the console window. As you run the program one line at a
time, you will alternatively click on the Borland C++ and the console window
button. Ideally you will arrange
the console and code windows so that they are both visible on the screen at the
same time.
If you set
up your new projects in the way described in this document, you can run the
code one line at a time by pressing the F8 function key on the keyboard. This is very useful if you are trying
to find logical errors in your code.
A logical error is one which produces unexpected results, but the
program compiles with 0 errors.
Finding and correcting errors in code is a process known as
debugging. The Borland C++
Integrated Development Environment (IDE) contains debugging tools as well as a
text editor for entering code, and the compiler and linker.
You should
spend a few hours experimenting with the C++ integrated development environment
(IDE) to make sure that you can create new projects, enter code (i.e. a C++ or
C program) for the project, and be able to compile, link and run your
program. To help you become
familiar with the integrated development environment, click on Help on the menu
bar, then Contents, and browse though the various documents for information.
Try to
become familiar with the debugging tools of the IDE. You will find that once you are a skilled debugger, you will
develop your programs much faster, and they are less likely to contain errors.
Help on C and C++ Language keywords
Note: C and C++ keywords are
shown in bold type in the code
window. If you need help on any
keyword, click on the word, and then press function key F1. The F1 key is
normally situated to the right of the Esc key. A help screen will appear. Try
to get into the habit of using the Help system as much as possible.
IMPORTANT
Be careful how you save your files
when using Borland. If you use the File then Save as
option to save a .cpp or .c file, make sure you're in the source code window
and not the projects window when you save. If you inadvertently corrupted your
source code by overwriting it with the contents of the project binary file,
your .cpp or .c file will contain a jumble of text and strange characters. To
revert back to the last saved version of your .cpp or .c file: quit Borland,
locate the folder of your .cpp or .c file. Delete the file, then find a file of
the same name but with the extension .bak. Rename the .bak file with so it has
the extension .cpp (or .c).