Sie sind auf Seite 1von 11

INTRODUCTION TO BORLAND C 5.

02 Creating a new program

This creates a new file with a default name: noname00.cpp

Another newly created file would be given the name: noname01.cpp and so on.

Before we begin editing the file, let us save it with a new name: sum.c. Note that the file extension being used here is .c and not .cpp. The Borland C program can also be used to create C++ (pronounced C plus plus) files.

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

Type in the file name sum.c in the textbox next to File name then click Open. It is useful to create a directory in which all your C files can be stored.

Now we can begin editing our file.

Note the asterisk beside the file name (see the arrow shown in the image above). The asterisk suggests that our file is not saved so before we compile and run the program, let us save the file. (Select File Save).

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

To compile the file, elect the menu option as shown in the image below:

Note the dialog box that tells us the status of the program after compilation:

Note that the program did not successfully compile (see above). The information in the dialog box window indicates that there is 1 warning & 1 error, the details of which are outlined in the Message window at the bottom of the screen. Let us examine the information produced in the Message window. Click Ok to clear the dialog box.

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

Messages are displayed with the file name and line number where the error was detected, as well as the text of the message itself. An error message (such as that in the row highlighted in blue in our image above) is a problem that should be fixed before the program can be successfully executed. These normally contain syntax errors code which goes against the rules which govern the programming language. The exclamation mark beside errors has a red colour. A warning message (which has a yellow exclamation mark) is a problem that can sometimes be overlooked when executing a program but which should be examined as often times it can lead to logic errors errors in the design of the program which fails to meet the program requirements and usually produces unintended results. Note that upon selecting the message, the line of code corresponding to it is automatically highlighted as shown in the image below:

Context-sensitive help is usually available for messages that appear in the message window. To get help on a particular message, select it and press F1 to display the message description. Let us select the error message and press F1. The following is displayed:

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

Help for the warning message produces the following information:

Let us try to fix the problems in our program. Often times, fixing the errors in our program will get rid of the warnings produced so let us examine the error identified.

Our error tells us that there is a semi-colon missing from our statement. Recall that in C, all statements MUST end in a semi-colon (;). Note in our code however that the statement highlighted in our source code does have a semi-colon. It is important to note that the compiler generates messages as they are detected and as such, the true cause of an error might occur one or more lines before OR after the line number specified in the error message. In our example, the statement BEFORE the line number specified is missing a semi-colon. Edit the source code and place a semi-colon after the statement: sum = num1 + num2 Save the program and then re-compile.

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

We have now successfully compiled our program! There are no errors and no warnings! Note too that the previous warning sum is assigned a value that is never used has also disappeared upon correction of our error. Often times, when errors are corrected, warnings disappear so it is useful to correct the errors, then re-compile your program.

Let us now execute (run) the program. Click Ok to clear the dialog box. Select the menu option as outlined in the image below:

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

Note the dialog box that is displayed while the programs executable file is being created:

The following output screen will then appear. An interactive dialog results in which the computer will prompt the user to enter a value for the first number.

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

The user will then type in a number (e.g. 10) then press ENTER

The screen will then prompt you for a second number which you should also type in (20, in our example). The computer will then process these values by adding them and will then display the result. Note that the lines in the program are carried out sequentially, one after the other.

Press the ENTER key to clear the command window.

CONGRATULATIONS!!!! You have successfully created and executed your first program!

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

Let us take a step back and go through the program line by line. The program has been slightly modified to include comments which enhance good programming practice. See the image below:

Note the following: (1) Comments have been included in the program, first to identify the programs purpose ( prompt the user to enter two numbers ). Additional comments have been placed throughout to explain the purpose of each line. As you write more and more programs, some comments such as output statement to prompt the user or input statement to accept a value from the user can be left out as these will be naturally understood. Note that there are two formats for comments. Comments which extend across more than one lines are enclosed within the /*(beginning) and */ (ending) characters as shown in the statement above which identifies the purpose of the program. Comments which take up just one line can also use these characters or if placed at the end of a C statement can use the // characters as shown in the body of the program above. (2) C is a case-sensitive language. Its reserved keywords and most functions that we will use are typically typed in lowercase. Variable names created by the programmer that are typed in one case must be used consistently in that case throughout the program. C treats a variable declared as num1 as a different variable if later on the name Num1 is used. See below for an example of this.

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

(3) Recall from your notes the general format of a C program. The lines include <stdio.h> and include <conio.h> make reference to special files (header files) which contain information that must be included in the program upon compilation. Certain functions are associated with each library, as shown below from the Borland C documentation.

(4) Every C program must have a point at which the program starts up. The function main() is one that appears in every C program and indicates that the program begins at this point. Note the brackets at the end of the word ( ). More will be said about main later. (5) Note that the lines of the program within the main function are enclosed within a set of curly braces { }. It indicates that these are the statements associated with main. The opening curly brace { must begin the body of every function and the closing curly brace } must end the function. (6) The lines associated with main have been indented. Indentation is particularly important as it enhances the readability of the program. Extra spacing also enhances readability, which is highly recommended as good programming practice. (7) printf is a function which in this case provides output to the screen. Note that the line which appears within the printf (Enter the first number) is actually displayed to the screen as a prompt to the user.

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

10

Scanf is a function accepts input from the keyboard. The user will enter a value which will be stored in the computer, in this case the value of 10. (8) The line sum = num1 + num2; carries out a process where the value of the variable in num1 (10) is added to the value of the variable in num2 (20), producing a result (30) which is assigned to the variable sum. In the output statement that follows, printf(The sum is %d, sum), the computer outputs the text written but then uses the value assigned to sum to replace the %d combination of characters. The %d is a conversion specifier which corresponds to the data type of the variable being used. This will be further discussed later, along with the other lines in the program. Note that upon successive execution of the program, the values entered for num1 and num2 may be different. For example, the user could enter 5 and 7 to produce a result of 12.

Recall in your notes the 6 phases through which a C program goes through in order to be executed: (1) Edit (2) Pre-Process (3) Compile (4) Link (5) Load (6) Execute We can now associate what we have discussed in the program with these phases: - The Edit phase is where we actually write or create the source code the lines of the program for us to add two numbers and display their result. We have named our program sum.c which we are editing to carry out the task of adding 2 numbers. This is the source file which contains our text. The Pre-Process phase is done before actual compilation begins, and directs the computer to process the code by searching for and including specific files (eg. stdio.h) or making certain types of replacement (not shown in our program). In the Compile phase, object code is produced which is a form that can be understood by the central processing unit of the computer. If you look in the directory to which your file has been saved, you will actually see a file sum.obj which contains the object code or compiler output. The Link phase links the object code with the libraries stored elsewhere on the computer, creating an executable file (sum.exe) that is saved. A very large program can have multiple source files which, when the program is compiled produces various object files. The link phase would then tie all these together to produce a single executable file. Whether large or small, the executable file is now in a form which can be loaded and run by the operating system. The Load phase moves the executable file (sum.exe) from disk to primary memory, the area in which programs are run. Finally in the Execute phase, the CPU executes the instructions one line at a time to eventually produce our sum of 30, or the result of the addition of any two numbers entered by the user.

So there you have a detailed description of the execution of a simple program in C and how it is actually processed.

HAPPY PROGRAMMING

Borland C 5.02 Mini-Tutorial Lisa Shaw - School of Computing & Information Technology

11

Das könnte Ihnen auch gefallen