Click here to print
What is a computer ?
To simplify, lets consider that a computer is composed by three entities: A CPU, a memory and a bus to communicate with peripherals.
What is a program ?
A program is a succession of instructions, store in memory, which will be interpreted by the CPU. Each of one can have an effect on the memory.
What is an operating system ?It is the program load at boot time. It is responsible to make the material abstract to other programs. The goal is to allow the writing of executable programs that work on heterogeneous architectures (portability), and without having to take into account the state of the machine at execution time (concurrency between programs).
What is a programming language ?
A language is composed with vocabulary (a set of key words) and a grammar (a set of rules that define is a succession of words is valid or not). C language is "human understandable", we say it to be a high level language, by opposition to the set of words a CPU understand.
With C, one has to write a source file. Once this file written, he can compile it to produce a binary file, ie a file containing the machine word that the CPU understand. In fact, there is two compilation phases: the translation itself, and the link edition, that permits to call pre compile parts of the executable. This permits to share code between programs (systems call or functions of C library for example).
- Invented to write UNIX, the first portable operating system (it can be compile and execute on different types of architectures).
- Still very commonly use today
- It inspired quite every commonly used languages: C++, Java, python, php etc.
The C has evolved several time. To warranty the compatibility, a standardization effort has been made. The most commonly use C is the "ISO C 90" C (it exists a more recent standard, the C99, but not very use, mainly because of the lack of good compilers)
To compiler a file "program.c" in C90 on linux: gcc -ansi -Wall -pedantic program.c -o program
- gcc is "the gnu C compiler" and is the C compiler included with standard linux distributions
- -ansi enforce gcc to produce ISO C 90 code
- -Wall let gcc print all warning messages
- -pedantic make gcc fail if the source code is not ISO C 90 compliant
- program.c is the source file
- -o program allows to specify that the program executable file must be named "program" (without this, gcc creates a file named a.out)
To understand essential notions of C programming (instructions, variables, types, structures, functions, pointers), is is necessary to understand how UNIX (remember that C and UNIX has been invented by the same people at the same time) let the programs see the memory. Each programs will view the memory as an infinite array of binary word (byte) beginning at address 0. The operating systems will later translate the addresses used by the program to real addresses.
What happens in real memory does not concern the programmer.
Moreover, every adresses are not allowed. The translation into real adress must be possible. Unfortunatly, the compiler will make very poor verifications. It is possible to write uggly things and to compile them. The resulting programs will fail at execution time, blocked by the operating system. For example, if a program reserved 5 variables of size 8, it uses 40 bytes: it is then possible to write code that modify these 40 bytes as 40 variables of one byte (for example). It is also possible to write and compile a code that will manipulates the 46th byte (still for example), but in that case, the program will be killed by the OS (terminating with segmentation fault).