C is one of the most common programming language and is closely tied to the Unix/Linux operating system. It is a rather low level language (slightly more user friendly than assembler) and requires a fair understand of how a computer works.
This tutorial is intended as a quick introduction to the C language. It does not replace a complete course. However the best way to learn C, is to try it out. A good book is "A Book on C" by Al Kelly and Ira Pohl. This site contains some reference lists for the more advanced programmer as a quick reference.
C programs are written as a set of instructions in plain text format. Typically two types of files exist. The bulk of source text are in files with the extension '.c'. For a program it is not necessary to put everything in a single file. In contrary it is strongly recommended to split the code up in individual files, grouping by functionality. This allows to reuse source text for other code. The second type of source files are include files, indicated with the extension ".h". Include can be regarded as the 'index' of the source code, so that parts of the code know how to reference/call other parts. Because the programmer can use preexisting code, call libraries, the system also provides a set of include files. They are typically stored under Linux/Unix/Macs in the directory /usr/include or any of the sub-directories.
The compiler hast the single purpose to convert the source text into an executable. A very common compiler is the GNU C Compiter, which is invoked with gcc at the command line. To understand the command structure it is good to understand how the compiler works:
In a first step it parses through a ".c" source file and includes all necessary ".h" files. It creates an object file (typically with the extension ".o") which is the raw machine instruction. It does it for all source files in the list, the compiler is invoked with.
The object files can have reference to other object files or library functions, however at this stage it does not know how to find and call them. This is the second stage where the linkers goes through all object files and checks for references to other object files and libraries. Once it finds one it links the reference (call) to the actual machine instructions. The result is one program, which combines all the required machine instructions. This is the executable.
The compiling command (at the command line) has the following syntax.
gcc options file1.c file2.c file3.c ...
The following table list the most common compiler instructions for the GNU C compiler:
|-o executable||defines the name of the executble. The default is a.out|
|-llibrary||tries to link agains function from library library. A commonn one is the math library to use function such as sine or exp. The name is m, and the option is -lm. Use this option for each library.|
|-Lpath||search path to the directory holding a libray to be included. This is an extension to the default search path the compiler already know.|
|-Ipath||search path to the directory holding a ".h" to be included in the source text. This is an extension to the default search path the compiler already know.|
|-c||creates only object files and stops before the linking process|
|-Wwarning||reports warning, while compilations (e.g. -Wall)|
|-Wno-warning||suppress warnings, while compilations (e.g. -Wno-implicit)|
|-Werror||treats all warnings as errors|
|-w||equivalent ot -Wno-all|
|-g||create symbolic table (used by the debugger)|
|-p||create code for profiling (testing for efficiency)|
|-On||performs optimization level n with n=0 no optimization and n=3 best optimization|
|-O||equivalent to -O2|
|-fast||as -O3 and machine/chip-dependent optimization|
|-D macro||defines macro (equivalent to the #define instruction)|