The C language for system programming

C has known success thanks to the liberty if offered to programmers. Its drawback is the difficulty to debug programs. It makes fast programs, and a huge collection of APIs is available. It remains the best tool for hard-core programming.

C language, Dennis Ritchie
The first manual contributed to the popularity of C

The C language was originally designed from 1969 to 1972, by Dennis Ritchie to program the Unix operating system. The goal was to be portable. See The development of the C language for a short history.
The tutorial of the language by Dennis Ritchie and Brian Kernighan is considered one of the best ever written.

C seems rather rustic compared to modern languages, but in the 70s as an alternative to Basic, it was extremely evolved. Calling a function by its line number, or having variable names limited to two characters in the Microsoft's AppleSoft basic, made​ programming very tedious. Even Pascal with its heaviness and constraints seemed less interesting than C.

It inspired many other languages ​​including C++, Java, C# from Microsoft and more recently Go and Dart  from Google.

C could still be a better tool than C++ for system programming, that is the opinion of Linux Torvald, who has choosen to use this language for the widely used Git software. But this judgment (in 2007) may date as C++ since has undergone several changes and improvements, especially with the version 11 (despite the evolution of C too as in 2011).
It remains that, for critical system applications, C++ is to much complex and indeterminate. And to overcome the insecurity of the C anguage, Microsoft has created Checked C, an extension that tests possible memory problems in a program.

A language where everything is an expression

A C program is a set of functions that return or not a value, and global variables. Everything can be evaluated as an expression, and this is not necessarily an advantage for security.
Functions and variables have prototypes in header files for shared use of the source file.
The programmer must manage itself the memory, using pointers and functions to allocate of free fields of memory.
It is portable with some restrictions, additional variable types for exemple, are specific to compilers.

The ISO C11 standard defined in 2011 adds new features to the C language: multi-threading, Unicode, lambda functions, anonymous structures and unions when nested.

Data structures:

Scalars: int, long, char, char *.

struct creates an object type made of several variables and was the forerunner of objects.
union can give several identifiers to a variable and is used primarily with struct.
typedef to define new data types ..

Control structures:


// and /* */ comments.
& | && || logical operators.
# directive to the pre-processor.

Compound symbols allows to perform several operations on a same variable. For example, a += b adds b to a and assigns the result toa.

The while loop:

while(x < y)
   ... statements ...

Defining a function:

int name(char *x, int y)
   ... statements ...


Sample code

Displaying the list of characters in a string:

char *s = "demo";
int l = strlen(s);
for(int i = 0; i < l; i++)
   char c = s[i];
   printf("%c\n", c);

Sites and tools for C