Some basic C
If you are unfamiliar with the C programming language, we have provided some examples to get you started. They assume familiarity with another programming language like java and their main purpose is to highlight some differences
between the languages. Copy the files from ~aca319/labs/c to your directory. Have a look at them. Then try to compile and execute them one by one to see what they do. Go through the source and put some comments in for yourself.
To get you started here is a brief describtion:
- Hello: compile with gcc -o hello hello.c This is just prints out a message...
- Args: compile with gcc -o args args.c Execute it with some parameters (e.g. ./args 1 2 3). It will print
out whatever arguments you have given it. You may find this useful when writing your own programs
- Sfunc: compile with gcc -o sfunc sfunc.c This program contains a very simple function which is called
from the main
- Cookie: compile with gcc -o cookie cookie.c cookie_main.c This program is functionally identical to sfun, but have a look at the way in which it is broken up. It is usuall to seperate different modules into separate files. In this setup we have the main body in cookie_main.c and the cookie function in cookie.c . Also notice the presence of the header file cookie.h. It contains the interface definitions of the fnctions. These are included in cookie_main.c
with #include "cooke.h"
- ref_func: compile with gcc -o ref_func ref_func.c Have a look at the way in which the argument is passed this time. Notice anything different ? What happens ? This is quite important.
- Array: compile with gcc -o array buggy_array.c Oops.. There are some syntax erros in this one. Fix them. Otherwise it creates 2 arrays. One is statically allocated (hard coded) and the other one is dynamically allocated. Oh it takes an argument,the size of the dynamically allocated array. See how useful it is ? You can change the size by giving it a different argument. Meanwhile the size of statically allocated array is always the same.
For more information on C I recommend you check out this place This should be fairly thorough as it is based on
the C bible ("The C programming Language" by K&R, the creators of the language). If you want something else do a net search, read a book etc.
Last modified on Mon Mar 18 20:13:15 WST 2002