Chapter Three
Chapter Three
Chapter Objectives
By the end of this chapter the learner should be able to;
Describe the characteristics of C programming language.
Describe the process of developing and Executing a C program
Describe the compilation process of a C program and C program file naming
conventions.
Differentiate between Syntax and Logical Errors
Describe the structure / format of a C Program
C is one of a large number of high level languages which can be used for general purpose programming,
that is, anything from writing small programs for personal amusement to writing complex applications. C
is a general-purpose computer programming language developed between 1969 and 1973 by Dennis
Ritchie at Bell telephone Laboratories. It is unusual in several ways. Before C, high level languages were
criticized by machine code programmers because they shielded the user from the working details of the
computer, with their black box approach, to such an extent that the languages become inflexible: in other
words, they did not allow programmers to use all the facilities which the machine has to offer. C, on the
other hand, was designed to give access to any level of the machine down to raw machine code and
because of this it is perhaps the most flexible of all high level languages.
The C language has been equipped with features that allow programs to be organized in an easy and
logical way. This is vitally important for writing lengthy programs because complex problems are only
manageable with a clear organization and program structure. C allows meaningful variable names and
meaningful function names to be used in programs without any loss of efficiency and it gives a complete
freedom of style; it has a set of very flexible loop constructions (for, while, do) and neat ways of making
decisions. These provide an excellent basis for controlling the flow of programs.
Another unusual feature of C is the way it can express ideas concisely. The richness of a language shapes
what it can talk about. C gives us the apparatus to build neat and compact programs. This sounds, first of
all, either like a great bonus or something a bit suspect. Its conciseness can be a mixed blessing: the aim is
to try to seek a balance between the often conflicting interests of readability of programs and their
conciseness. Because this side of programming is so often presumed to be understood, we shall try to
develop a style which finds the right balance.
C allows things which are disallowed in other languages: this is no defect, but a very powerful freedom
which, when used with caution, opens up possibilities enormously. It does mean however that there are
aspects of C which can run away with themselves unless some care is taken. The programmer carries an
extra responsibility to write a careful and thoughtful program. The reward for this care is that fast,
efficient programs can be produced.
C tries to make the best of a computer by linking as closely as possible to the local environment. It is no
longer necessary to have to put up with hopelessly inadequate input/output facilities anymore (a legacy of
the timesharing/mainframe computer era): one can use everything that a computer has to offer. Above all
it is flexible. Clearly no language can guarantee intrinsically good programs: there is always a
responsibility on the programmer, personally, to ensure that a program is neat, logical and well organized,
but it can give a framework in which it is easy to do so.
The C compiler combines the capabilities of an assembly language with features of a high-level language
thus making it suited for writing both system software and business packages. C program uses a variety of
data types and operators thus making programs written in C to be efficient and fast. C is highly portable
and is well suited for structured programming. C is basically a collection of functions that are supported
by the C library and because new functions can be added to the C library, C has the ability to extend
itself.
3.2. Characteristics of C
We briefly list some of C's characteristics that define the language and also have lead to its popularity as a
programming language.
Small size
Extensive use of function calls
Structured language
Low level (BitWise) programming readily available
Pointer implementation - extensive use of pointers for memory, array, structures and functions.
C has now become a widely used professional language for various reasons.
It has high-level constructs.
It can handle low-level activities.
It produces efficient programs.
It can be compiled on a variety of computers.
Its main drawback is that it has poor error detection which can make it off putting to the beginner.
However diligence in this matter can pay off handsomely since having learned the rules of C we can
break them. Not many languages allow this. This if done properly and carefully leads to the power of C
programming.
A compiler usually operates in two or more phases (and each phase may have stages within it).
A two-phase compiler works in the following way:
Phase 1 scans a source program, perhaps generating an intermediate code (quadruples or pcode) which
helps to simplify the grammar of the language for subsequent processing. It then converts the intermediate
code into a file of object code (though this is usually not executable yet). A separate object file is built for
each separate source file. In the GNU C compiler, these two stages are run with the command gcc -c; the
output is one or more .o files.
Phase 2 is a Linker. This program appends standard library code to the object file so that the code is
complete and can "stand alone". A C compiler linker suffers the slightly arduous task of linking together
all the functions in the C program. Even at this stage, the compiler can fail, if it finds that it has a
reference to a function which does not exist. With the GNU C compiler this stage is activated by the
command gcc -o or ld.
The endings `dot something' (called file extensions) identify the contents of files for the compiler. The
dotted endings mean that the compiler can generate an executable file with the same name as the original
source - just a different ending. The object file is only working files and should be deleted by the compiler
at the end of compilation. The .c suffix is to tell the compiler that the file contains a C source program and
similarly the other letters indicate non-source files in a convenient way.
3.6. Errors
Errors are mistakes which we the programmers make. There are different kinds of error:
Errors in the syntax, or word structure of a program are caught before you run it, at compilation time by
the compiler program. They are listed all in one go, with the line number, in the text file, at which the
error occurred and a message to say what was wrong. A program with syntax errors will cause a compiler
program to stop trying to generate machine code and will not create an executable. However, a compiler
will usually not stop at the first error it encounters but will attempt to continue checking the syntax of a
program right to the last line before aborting, and it is common to submit a program for compilation only
to receive a long and ungratifying list of errors from the compiler.
As a rule, look for the first error, fix that, and then recompile. Of course, after you have become
experienced, you will recognize when subsequent error messages are due to independent problems and
when they are due to a cascade. But at the beginning, just look for and fix the first error.
When a programmer wants to make alterations and corrections to a C program, these have to be made in
the source text file itself using an editor; the program, or the salient parts, must then be recompiled.
3.7. C Libraries
In C, a library is a set of functions contained within a single "archive" file. The core of the C language is
small and simple. Special functionality is provided in the form of libraries of ready-made functions. This
is what makes C so portable. Libraries are files of ready-compiled code which we can merge with a C
program at compilation time. Libraries provide frequently used functionality and, in practice, at least one
library must be included in every program: the so-called C library, of standard functions.
Each library comes with a number of associated header files which make the functions easier to use.
Header files contains the prototypes of the functions contained within the library that may be used by a
program, and declarations of special data types and macro symbols used with these functions. It is up to
every programmer to make sure that libraries are added at compilation time by typing an optional string to
the compiler.
Functions; the basic building block in a C program is the function. In general, functions are blocks of
code that perform a number of pre-defined commands to accomplish something productive. It must have
a name and it is reusable ie it can be executed from as many different parts in a C Program as required.
Information passed to the function is called arguments and is specified when the function is called. And
the function either returns some value to the point it was called from or returns nothing.
Every C Program will have one or more functions and there is one mandatory function which is called
main() function. This function is prefixed with keyword int which means this function returns an integer
value when it exits. This integer value is returned using return statement.
Structure of a Function
There are two main parts of the function. The function header and the function body.
int sum(int x, int y)
{
int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum
return ans //return the answer
}
Function Header
It is the first line of a function, example; int sum(int x, int y). It has three main parts
The name of the function i.e. sum
The parameters of the function enclosed in paranthesis
Return value type i.e. int
Function Body
What ever is written with in { } in the above example is the body of the function.
Symbolic constants are usually written in upper case to distinguish them from lower case variables.
Values defined here remain constant throughout the program.
printf("Multiplication of %d and %d is %d", a,b,c); /* request to print the answer on the Screen*/
getch(); /*command used to pause the results on the screen*/
}
/* mul() /* ……….. Sub-program mul */
int mul(int x,int y)
{
int p;
p = x*y;
return (p);
}
NB. the values of a & b are passed to x & y respectively when the sub-program is called.