The stages of developing your C program are as follows.
Creating the program
Create a file containing the complete program, such as the above example. You can use any ordinary editor with which you are familiar to create the file. One such editor is textedit available on most UNIX systems.
The filename must by convention end ``.c'' (full stop, lower case c), e.g. myprog.c or progtest.c. The contents must obey C syntax. For example, they might be as in the above example, starting with the line /* Sample .... (or a blank line preceding it) and ending with the line } /* end of program */ (or a blank line following it).
Compilation
There are many C compilers around. The cc being the default Sun compiler. The GNU C compiler gcc is popular and available for many platforms. PC users may also be familiar with the Borland bcc compiler.
There are also equivalent C++ compilers which are usually denoted by CC (note upper case CC. For example Sun provides CC and GNU GCC. The GNU compiler is also denoted by g++
Other (less common) C/C++ compilers exist. All the above compilers operate in essentially the same manner and share many common command line options. Below and in Appendix [*] we list and give example uses many of the common compiler options. However, the best source of each compiler is through the online manual pages of your system: e.g. man cc.
For the sake of compactness in the basic discussions of compiler operation we will simply refer to the cc compiler -- other compilers can simply be substituted in place of cc unless otherwise stated.
To Compile your program simply invoke the command cc. The command must be followed by the name of the (C) program you wish to compile. A number of compiler options can be specified also. We will not concern ourselves with many of these options yet, some useful and often essential options are introduced below.
Thus, the basic compilation command is:
cc program.c
where program.c is the name of the file.
If there are obvious errors in your program (such as mistypings, misspelling one of the key words or omitting a semi-colon), the compiler will detect and report them.
There may, of course, still be logical errors that the compiler cannot detect. You may be telling the computer to do the wrong operations.
When the compiler has successfully digested your program, the compiled version, or executable, is left in a file called a.out or if the compiler option -o is used : the file listed after the -o.
It is more convenient to use a -o and filename in the compilation as in
cc -o program program.c
which puts the compiled program into the file program (or any file you name following the "-o" argument) instead of putting it in the file a.out .
Running the program
The next stage is to actually run your executable program. To run an executable in UNIX, you simply type the name of the file containing it, in this case program (or a.out)
This executes your program, printing any results to the screen. At this stage there may be run-time errors, such as division by zero, or it may become evident that the program has produced incorrect output.
If so, you must return to edit your program source, and recompile it, and run it again.
The C Compilation Model
We will briefly highlight key features of the C Compilation model (Fig. 2.1) here.
The Preprocessor
We will study this part of the compilation process in greater detail later. However we need some basic information for some C programs.The Preprocessor accepts source code as input and is responsible for
- removing comments
- interpreting special preprocessor directives denoted by #.
- #include -- includes contents of a named file. Files usually called header files. e.g
- #include <math.h> -- standard library maths file.
- #include <stdio.h> -- standard library I/O file
- #define -- defines a symbolic name or constant. Macro substitution.
- #define MAX_ARRAY_SIZE 100
No comments:
Post a Comment