Faculty of computing and information management
Programming methodology
Lecturer: Mr. HAGGAI
Lesson 5: Writing and compiling a program
5.1. Introduction
In our last lesson we discussed about programming languages and programming tools.
In this lesson, you will be introduced to programming using c. The programming
process begins with the creation of a text file which contains the statements of the
program in a programming language. (This source file is a text file, usually ASCII,
which can be produced with a text editor, such as Windows notepad, or in an
Integrated Design Environment.) This source file is then processed by a special
program called a compiler. Each programming language has its own compiler, and the
compiler must be matched to the hardware and operating system that the new
program will run on.
5.2. Objective of the lesson
By the end of this lesson the learner will be able to:
Describe the procedure of writing, compiling and building a program in c.
Describe the general structure of a c program.
Describe program errors.
5.3. Lesson outline
This lesson is structured as follows;
5.1. Introduction
5.2. Lesson objectives
5.3. Lesson outline
5.4. Types of c files
5.5. Preprocessors
5.6. Steps for writing and compiling c program
5.7. Program structure
5.8. Testing vs debugging
5.9. Types of errors
5.10. Revision questions
5.11. Summary
5.12. Suggested reading
5.4. Types of files
Writing and compiling C programs requires four kinds of files:
[i]. Source code files. These files contain function definitions, and have names which
end in .c extension.
[ii]. Header files. These files contain function declarations (function prototypes) and
various preprocessor statements. They are used to allow source code files to
access externally-defined functions. Header files end in .h extension.
[iii]. Object files. These files are produced as the output of the compiler. They
consist of function definitions in binary form, but they are not executable by
themselves. Object files often end in .obj.
1
[iv]. Binary executables. These are files produced as the output of a program called
a linker. The linker links together a number of object files to produce a binary file
which can be directly execute.
5.5. Preprocessors
The first step of the compile process is a special preprocessor program. The
preprocessor program reads the source file as text, and produces another text file as
output. The preprocessor makes substitutions of one-character string for another,
substitutes the text of a header file for #include statements, and even expands some
function calls into other text. The substitutions can be conditional. The output of the
preprocessor is a text file which does not contain any preprocessor statements, which
is ready to be processed by the C compiler itself. Examples of preprocessors.
#define: Substitutes a preprocessor macro.
#include: Inserts a particular header from another file.
#undef: Undefines a preprocessor macro.
#ifdef: Returns true if this macro is defined.
5.6. Steps for writing and compiling c program
Developing a program in c involves four major steps:
Step 1: Writing the code
Use an editor to write the source code. Save the source code files with .c
extension .For example, myprog.c, database.c, and so on.
Step 2: Compile the source code.
Compile the program using a compiler. If the compiler doesn't find any errors
in the program, it produces an object file. The compiler produces object files
with an .obj extension and the same name as the source code file for
example, myprog.c compiles to [Link]). If the compiler finds errors, it
reports them. If errors are reported return to step 1 to make corrections in
the source code.
Step 3: Linking the program
Link the program using a linker. If no errors occur, the linker produces an
executable program located in a disk file with an .exe extension and the
same name as the object file for example; [Link] is linked to create
[Link]. If errors are reported return to step 1 to make corrections in the
source code.
Step 4: Execute the program.
Test the program to determine whether it functions properly. If
not, start again with step 1 and make modifications and
additions to the source code.
Flow chart: Writing and compiling c program
5.7. Program structure in c
Start
Create the source code:
prog.c
32
Compile source code:
[Link]
A program in c has the following components:
[a]. #include <stdio.h> : This is the first line of the program. It is a preprocessor
command which directs the compiler to include stdio.h file before actual
compilation.
[b]. main(): The main function is where program execution begins.
[c]. Comment(s): Any statement enclosed by /*...*/ will be ignored by the compiler
and is put to add additional comments in the program.
[d]. {} : The opening and closing braces are used to enclose a function block in this
case main.
[e]. printf(...) : This library function available in C which prints(outputs) string on the
screen.
[f]. Scanf(…): This library function available in C which takes(input) from the user
e.g. through the keyboard.
[g]. Statement(s): It is a complete instruction that directs the computer to
carry out some [h]. task. In c tatements always end with a semicolon (except
for preprocessor directives such as #define and #include).
[i]. return 0; terminates main()function and returns the value 0 to the operating
system. Example:
/*Program to add two numbers and display the sum*/
#include<stdio.h
> int main()
{
int a,b,sum; /*Declare
variable*/ printf(“Please the
enter value of a: \n”);
scanf(“%d”,&a); printf(“Please
enter the value of b: \n”);
scanf(“%d”,&b); sum=a+b; printf(“The
sum of %d and %d is %d. \n”,a,b,sum);
return 0;
}
5.8. Testing and debugging
Testing is a process of finding defects in the code or finding if the program does
what it is supposed to do. Debugging is the process of finding and removing a
specific bug(s)/error(s) from the program. Testing process can be manual or
automated but debugging is always manual.
Example: Debugging
The program tre.c is being compiled. During compilation an error was detected and
so the object file could not be formed. The error is in line 9 , a syntax error which is
a missing ;
5
Example: Testing
Even after debugging, the program must be run and different values of a and b
entered to see if it actually calculates the sum as expected. See the output
when testing
Please the enter value of a:
3
Please enter the value of b:
45
The sum of 3 and 45 is 48.
Press any key to continue
5.9. Types of errors
In programming, an error also known as a bug is a mistake or omission that may
prevent the program from compiling and running correctly as per the expectation of
the programmer.
There are three types of errors in programming:
[i]. Runtime Errors
[ii]. Compile Errors
[iii]. Logical Errors
[a]. Runtime Errors
These are errors that occur during the execution of a program. Generally these
errors occur due to some illegal operation performed in the program. These errors
may stop program execution.
Some illegal operations that may produce runtime errors are:
• Dividing a number by zero
• Trying to open a nonexistent file
• Lack of free memory space
[b]. Compile Errors
Compile errors are those errors that occur at the time of compilation of the
program. These errors may be further classified as: [i]. Syntax Errors
These are errors detected during compile time when the rules of the programming
language are not followed. Example
int a, b:
The above statement in a c program will produce syntax error as the statement is
terminated with:
rather than;
[ii]. Semantic Errors
These are errors reported by the compiler when the statements written in the
program are not meaningful to the compiler. Usually arise from wrongly typed
expressions and formulas. Example
a+b=c;
In the above statement we are trying to assign value of c in the value obtained by
summation of a and b which has no meaning in a program in c. The correct
statement will be
c=a+b;
6
[iii]. Linker errors
These are errors reported when an executable file of the program cannot be
generated. This may due to wrong function prototyping, incorrect header files.
These errors are not easy to detect.
#include<math>
Math file cannot be located in any directory and so the program will throw an error.
[c]. Logical Errors
These are the errors in the output of the program. The presence of logical errors
leads to undesired or incorrect output and are caused due to error in the logic
applied in the program to produce the desired output. These errors may not be
detected by the compiler and so the programmer has to check the entire coding of
the program line by line.
5.10. Revision questions
a) Explain the possible causes of the following errors in a program.
i). Syntax error
ii). Semantic error
iii). Logic error
iv). Linker
error.
b) Study the c program below, identify and correct any errors noted.
#include <stdio.h>
main()
{
float x y, sum; printf(Enter the
value of x:\n"); scanf("%d",x);
printf("Enter the value of y:\n");
scanf("%d",y); x+y=sum;
printf("The sum of x and y is: %d\
n",sum);
}
c)A program errors can be syntax, logic , linker or semantic errors. Classify the
following errors. i). An indefinite loop
ii). Misspelling keywords
iii). Dividing a number
zero iv). Using undeclared
variable.
v). Misspelling function name.
d) Describe the following files as used in a c program.
i). Source code file
ii). Object file
iii). Binary executable file
iv). Header files.
[4 Marks]
e) Describe the process of writing and compiling a c program.
[4 Marks]
7
f) Discuss the following program errors, give an example in each case.
i). Logical error
ii). Run time error
iii). Compile error.
[6 Marks]
g) Briefly explain the following files which were observed when compiling a
program in c. i). hello.c
ii). [Link] or hello.o
iii). [Link]
[3 Marks]
h) When compiling a program in c, Susan, a stage III diploma student in KCAU
observed an error thrown by the compiler as follows:
nice.c(8) : error C2143: syntax error : missing ';' before ':'
Identify and explain all the information provided in this error message. [4
Mark]
5.11. Summary
In this lesson we have discussed how we can write, compile, debug, run and test a c
program.
We have also describe important files in c. Finally we have identified and discussed
common program errors.
5.12. Suggested reading
[1]. An introduction to programming by Wu Thomas, Norman and Theodore: McGrill
(1997).
[2]. C programming language by Brian W. Kernighan, Dennis Ritchie, 2nd ed:
prentice hall(2003).
[3]. C how to program by H.M. Deitel and [Link], 3 rd ed: Prentice hall(2001).