C

C is a programming language created in the 1970s by Dennis Ritchie and Ken Thompson. It is one of the most popular programming languages, and is notably used for the Linux kernel. Since its creation, it has gone through several revisions, the most recent being C17 released in 2018.

The main function

The entry point for any C program is the main function. It takes two arguments and returns an integer.

int main(int argc, char **argv)
{
  // stuff
  return 0;
}

The parameters are as follow:

  • argc: how many arguments were passed
  • argv: the values of the arguments

The two asterisks in front of argv means that it is a pointer to a pointer to a variable of type char. The main function returns an integer that is recieved by the OS. A return value of 0 means that the program ran succesfully. Anything else means an error occured.

The order in which you write functions matters: you cannot call a function that has not been previously declared.

Compilation

The source code for a C program is just a text file, usually with with a .c extension. To run it, we must first translate into machine code that the computer can execute. This is done in three steps:

  1. Preprocessing: the source code is modified according to some rules called preprocessing directives. These begin with a "#".
  2. Compilation: the source code is translated into machine code and stored as binary object files (.o extension). If there are several .c files, there will be several .o files. While these contain actual machine code, they cannot be executed as is.
  3. Linking: the binary object files are combined into one executable.

A commonly used compiler on GNU/Linux systems is gcc (GNU Compiler Collection). It does all of these three steps.

gcc source.c -o executable_file

Memory management

During runtime, a program might need to store data in RAM. The amount of memory needed might be known in advance, but oftentimes the program needs to request memory from the OS at runtime, which is called dynamic memory allocation (the opposite, when the amount of memory needed by a program is known in advance and doesn't change at run time, is called static memory allocation).

Requesting memory from the OS is done with the malloc, calloc, realloc and aligned_alloc functions. We can tell the OS that a memory space isn't needed anymore with the free function.

Pointers

Each time a variable is created, its value is stored somewhere in memory. The address of a stored value can be obtain with the & operator. A variable that stores the address of another variable is called a pointer, and is declared by adding a * after the type name. Accessing the value stored at an address stored inside a pointer is called dereferencing, and is also done by adding a *.

int var = 7; // var stores the value 7
int* pVar; // pVar is a pointer. It stores the address of a variable of int type.
pVar = &var; // the address of var (where 7 is stored) is assigned to pVar
int var2 = *pVar; // the content of the variable pointed by pVar is assigned to a new variable var2

There is a special case for structures: if we have a pointer to a structure and we want to access the value of one of its members, we can use ->.

Linked list

typedef struct _pv{
    int index;
    Point2D coordinates;
    struct _pv* prev;
    struct _pv* next;
} PolygonVertex;

typedef struct {
    int size;
    PolygonVertex* head;
} Polygon;

(used in my 3D projector).

Why is it sometimes called "ANSI C" or "C89" or whatever ?

ANSI C, ISO C, Standard C and (as of 2023) C17 all refer to "the current version of the C language".

ANSI is short for "American National Standards Institute", the organization that manages standards in the United States.

While the first specification of the C language was published by Brian Kernighan and Dennis Ritchie in their book The C Programming Language, ANSI published the first official standard specification for the language. This is why some people call it ANSI C. Since it was released in 1989, we also call it C89 nowadays.

A year later the International Organization for Standardization (ISO) also ratified this version, which is why some people also call it ISO C, or just Standard C.

There has been several revisions of C over the years, each being named according to its year of release. That's where C89, C99, C11 and C17 come from. The original C specification published by Kernighan and Ritchie is called "K&R C".