![]() |
Help | ![]() ![]() |
![]() ![]() ![]() ![]() ![]() |
Programming can be accomplished in two ways. It is either interpreted or compiled, in the former method the "source code" for the program is read as the program executes and controls the process. I am only concerned with the latter method which is described below.
Compilation is the process of turning the file of human readable text known as source code into a machine executable file. Normally, the C programming language is compiled, so this is what I will be concerned with here.
A Simple Program
Well, I had to include this one:
#include<stdio.h> int main(){ printf("Hello World\n"); return(0); }
The above is the example that almost all programming books use as their first program. Test this out by entering the text shown above in your favorite text editor, call the file hello.c for example. Then type:
gcc hello.c
You will then see (if you typed it in correctly) another prompt after a few minutes. The GNU C compiler (gcc) will have made an executable file in your current directory called a.out. This file can now be run by typing it's name, just like running a normal command. If you do this you will find that you will see the phrase "Hello World" displayed on your screen.
More Advanced Programming
Assuming that you have completed the first exercise, then have a got at the
following: try editing the program above to display different messages and
also try not including the \n at the end of the message.
A C program is made up of a collection of statements. These statements are grouped togeather in functions. Every program must consist of at least one function. In the example program, then function is called main this is a special function which is always executed automatically when you run the program. If you wish to write your own functions, then you must call them as well as define them.
An example of a function which is predefined is the printf function. This funtion has been pre-written for you by the programmers who wrote the C library for this compiler. There are many other built in functions and there are manual pages for them on all the computer society machines. Type man printf at the shell prompt to see the page for this function.
To use a pre-defined function, you must tell the compiler that you wish to
do so by adding a function declaration at the start of your source
code. For the pre-defined functions available, this has already been done.
You simply add a line to your file which causes the header file
containing the declaration you wish to use to be read in to your file.
The statement which does this is #include
There are many header files which contain groups of related declarations, and the header you will need for a particular function is detailed in the manual page for that function.
Functions
A function is a block of code which can be referred to in another part of the
program by it's name, such that the referance will cause the code in the
function body to execute. In the above example, the body is the part of
the code between the { and }. The function starts off with a
declaration of the return type of the function (int) which is then
followed by the name of the function (main) and then in brackets after it,
is a list of variables that are passed to the function.
Please email Steve Whitehouse if you have a specific query about programming in C.