Académique Documents
Professionnel Documents
Culture Documents
Avinash Pokhriyal
The Compiler
A C program is made by running a compiler which takes the typed source
program and converts it into an object file that the computer can execute.
A compiler usually operates in two or more phases (and each phase may
have stages within it). These phases must be executed one after the
other. As we shall see later, this approach provides a flexible way of
compiling programs which are split into many files.
A two-phase compiler works in the following way:
Errors
Errors are mistakes which we the programmers make. There are different kinds
of error:
Syntax
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.
For example, suppose you write the folloiwing program in the source file abc.c:
and again compile it. Again, there are two error messages:
Logical
Errors in goal or purpose (logical errors) occur when you
write a program that works, but does not do what you
intend it to do. This is like you intended to send a letter
to all drivers whose licenses will expire soon, but, you
send a letter to all drivers whose licenses will expire
sometime. Here is a program to denmonstrate logical
error:
main()
{
int x=2000, y=2000, z;
z=x*y;
printf(%d,z);
}
main()
{
int x=5, y=4, z=3;
if(x>y>z) printf(TRUE);
else
printf(FALSE);
}
Case sensitivity
One of the reasons why the compiler can fail to produce the executable file for a
program is you have mistyped something,even through the careless use of upper
and lower case characters. The C language is case dependent. Unlike languages
such as Pascal and some versions of BASIC, the C compiler distinguishes between
small letters and capital letters. This is a potential source of quite trivial errors
which can be difficult to spot. If a letter is typed in the wrong case, the compiler
will complain and it will not produce an executable program.
Declarations
Compiler languages require us to make a list of the names and types of all
variables which are going to be used in a program and provide information about
where they are going to be used. This is called declaring variables. It serves two
purposes:
firstly, it provides the compiler with a definitive list of the variables, enabling it
to cross check for errors, and
secondly, it informs the compiler how much space must be reserved for each
variable when the program is run.
Questions
What is a compiler?
How is a C program run?
How is a C program compiled usually?
Are upper and lower case equivalent in C?
What the two different kinds of error
which can be in a program?
Filenames
The compiler uses a special convention for the file names, so that we do not
confuse their contents. The name of a source program (the code which you write) is
filename.c.
The compiler generates a file of object code from this called filename.o on Unix (or
filename.obj on Windows) as yet unlinked.
The final program, when linked to libraries is called filename on Unix like operating
systems, and filename.EXE on Windows derived systems.
The endings `dot something' (called file extensions) identify the contents of files for t
he 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 .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.
Command Languages and Consoles
Another important object is the `panic button' or program interruption key. Every
system will have its own way of halting or terminating the operation of a program or
the execution of a command. Commonly this will involve two simultaneous key
presses, such as CTRL+C, CTRL+Z or CTRL+D etc.
Libraries
The notion of the C language is KISS (Keep It Small and Simple). Special functionality
is provided in the form of libraries of ready-made functions. This is what makes C so
portable. Some libraries are provided for you, giving you access to many special
abilities without needing to reinvent the wheel. You can also make your own, but to do
so you need to know how your operating system builds libraries.
Libraries are files of ready-compiled code which we can merge with a C program at
compilation time. Each library comes with a number of associated header files which
make the functions easier to use. For example, there are libraries of mathematical
functions, string handling functions and input/output functions and graphics libraries.
It is up to every programmer to make sure that libraries are added at compilation time
by typing an optional string to the compiler.
#include <stdio.h>
main ()
{
printf ("C standard I/O file is included\n");
printf ("Hello world!");
}
A program wishing to use a mathematical function such as sin
would need to include a mathematics library header file.
#include <stdio.h>
#include <math.h>
main ()
{
double x,y;
y = sin (x);
printf ("Maths library ready");
}
A particular operating system might require its own special library for
certain operations such as using a mouse or for opening windows in a
GUI environment, for example. These details will be found in the local
manual for a particular C compiler or operating system.
Although there is no limit, in principle, to the number of libraries
which can be included in a program, there may be a practical limit:
namely memory, since every library adds to the size of both source
and object code. Libraries also add to the time it takes to compile a
program. Some operating systems are smarter than others when
running programs and can load in only what they need of the large
libraries. Others have to load in everything before they can run a
program at all, so many libraries would slow them down.
To know what names libraries have in a particular operating system
you have to search through its documentation. Unix users are lucky
in having an online manual which is better than most written ones.
some more basic elements are: comments , preprocessor commands , functions , declarations ,
variables
statements .
The skeleton plan of a program, shown below, helps to show how the elements of a C program relate.
/****************************************************/
/*
/*
Skeleton program plan
/*
/****************************************************/
#include <stdio.h>
/* Preprocessor defns */
#include <myfile.c>
#define SCREAM arghhhhh
#define NUMBER_OF_BONES 123
/****************************************************/
main ()
/* Main program & start */
{
int a,b;
/* declaration*/
a=random();
b=function1();
function2(a,b);
}
/****************************************************/
function1 ()
/* Purpose */
{....}
/****************************************************/
function2 (int a, int b)
/* Purpose */
{....}
*/
*/
*/