More How to
Program in C
### 5.0 Parts and pieces of a C program
----------------------------------------------------------------------
As we have seen in the above example, the process to create
an executable program from a source code can abbreviated as:
source code --> compiler --> executable
program
Imagine that you want to write a really big program, say,
a 3D-CAD-engine with midi interface, drumkit and real time video.
This will really need a large number of lines of code to write
down. What? You want to write all these lines into one
very long text file? Oh, no, that is not a good idea!
It will be easier to write, debug, and
build into a working program if you split it into a couple of
files. To make things easier, you would name each file
after its contents. For example, if you want to use the
number Pi (3.1415926...), you could declare it as a constant
and put it into an extra file.
And now???
Looking at the above example, you will
get a problem: each source file fed into the compiler seems to
produce one complete executable program!
Lost?
No! You can instruct the compiler to produce
output of an "intermediate state", if you want
half translated source code. These resulting files are
called "object files" (don't ask me why...).
After translating all single files into
object files, in a last step you have to "link" them
into an executable program.
LINK?
Yes! Linking is a process, where all "untranslated"
things, left in the files, are translated and make to fit together.
Now, you have your program!
These were the "outer pieces" of a program. But
the program is divided internally, too.
These parts are called functions or procedures.
There is an academically difference between them, but I take
it not that seriously. Later I will show you why.
For now, let's speak about procedures,
and keep in mind, that there is another name ("functions"),
which nearly the same meaning.
Ok, back to the main theme... ;-)
A procedure is a part of code, which do
a certain thing for us, for example, it makes a sound, adds two
numbers or blacken the screen (a simple screensaver). Each procedure
has its own name.
But one procedure is special. It is the
start, where execution of the program begins. This function is
called "main." All other functions must have
other names.
Look at the above example program. It consist
of one procedure only, the "main"-procedure.
### 6.0 Your Programming Environment
----------------------------------------------------------------------
Let's have a closer look to what is called
a "programming environment". But, what is it?
A programming environment consists of all
the things you need to write programs. Often you can buy
things called "integrated programming environment",
mostly programs which should do "all" for you. Mostly
they aren't worth the money...
Why?
Simply: You can get similar things for
free, with all source codes of all used programs including the
compiler itself (mostly a best protected secret of companies
like MicroSh*t -- so this is a "hack" itself: you gain
knowledge of a secret by simply downloading a tar ball ;-)
And, more important, you can fit things
together in your way, as _you_like it. No colored "click
here, crash there" with the mouse. If something fails, YOU
have the control. Search the error, learn from it, fix it! You
don't need people who tell you, what's good for you, do it your
way!
Back to the / ...;-)
A programming environment for the language
"C" consists mainly of a couple of programs and files:
-- an ASCII text editor (don't a word processor!)
-- a C compiler
-- libraries
-- header files
Until now, we know the ASCII editor, which
we have used to enter our first example program. We also know
our C compiler and of what kind it is (K&R or ANSI). But
what are "libraries????
Great halls of books? Yes and NO. In this
case: NO. A library is a collection of often used procedures.
These procedures has been pre-translated by the compiler.
So, if you want to use a procedure which has been written by
someone else and included into a library for you, the only thing
you have to do is to instruct the compiler to link against this
library.
Linking is the process where half translated
parts of a program, mainly different source codes were made fit
together by translating them fully (see above).
The only thing you need to use the libraries
are header files.
Imagine the following situation: You are
writing a program. You want to use a library function, say "printf"
of the HelloHacker program. But instead of typing "printf"
you hacked "rpintf" into your keyboard.
You feed the source code into the compiler.
Now the compiler comes to "rpintf" and wants to know
whether this is a function defined later in the source code or
in a library (in which case it would pretranslate it and won't
produce an error message) or whether this function is "unknown",
which means: "I don't know anything about this function.
Hey, hacker! This one I don't know! So: Define one, or I will
not work for you!"
Most compilers aren't that friendly and
won't say anything nearly that nice. It will say something like:
undefined reference to `rpintf'
This is the reason why header files are
used. Header files contain (not only) so called "prototypes".
Prototypes are "announcements" for later defined procedures
of libraries.
So, before you can define or use a function,
you have to write down a prototype of it. It is like saying to
the compiler "Hey, compiler, if you will find this
one later in the source code, don't grumble! Here is what it
should look like!"
Additionally some important constants are
defined in header files, for example the often used constant
PI. It is more easy to type "PI" than to remember 3.1415926...
every time and write it down correctly. And other things are
defined there. Becoming curious? OK, type
more /usr/include/stdlib.h
This is only one of the standard header
files. Much stuff, isn't it? Maybe you will find the file in
another place on your computer, maybe /usr/local/include or something
like this. But in most cases it is in /usr/include...
Oh, before I forget! One I have left out in the above list
of things found in a programming environment: The debugger. A
debugger is a program which helps in finding bugs in a program.
But because debuggers are not part of any definition of C, each
debugger works in a different way. It is beyond the scope of
this tutorial to show how to use a debugger. If you want more,
try
man db
or
man gdb
at your account. Manpagers are often the best source of informations,
you can get.
More how to program in C --->