The C language for system programming
The first manual contributed to the popularity of C
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 and is even used for applications. See Why SQLite is coded in C, for example. Other software coded en C: Linux, Doom, Quake et SimCity games, Apache, NGINX, PHP, JVM, Git...
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.
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.
What you need to know about C's story
Dennis Ritchie and his colleague Ken Thompson have to work on the language on a Teletype 33 terminal, as shown in the photo below.
Dennis Ritchie (standing) and Ken Thomson at Bell Labs on their Teletype 33 terminal
To imagine what programming looked like under these conditions, you have to watch this video of the terminal in action ... We understand better then the syntax of the language, made to save the typing, and where the sign = is chosen for the assignment and == for the comparison, because the assignment is more frequent than the comparison! Here is a closer view of the keyboard...
Ritchie had to be ingenious in defining the language to reduce the number of keystrokes, for example with the incremental operator ++. Other constructs are much more dubious like nested assignment inside a comparison test.
Moreover, the influence of the keyboard concerns as much the source code of the compiler as that of the programs that it recognizes. But a modern language can be simplified at the expense of a more complex compiler. Keyboards have changed now, yet convoluted constructions of C remains in many other languages including C++, Java, Microsoft C# and very recent ones such as Go and Dart of Google and Swift.
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:
- if ... else
- switch ... case ... default
- while
- for
- do ... while
- break, continue, return
- goto
Symbols:
// 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 ...
return(z);
}
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);
}
See also
- LLVM. Optimized compiler for C.
- C++. Oriented object version of C.
- No Make builds a C project with no makefile.
Sites and tools for C
- Introduction to C. First C curse.
- Cling. C and C++ interpreter.
- Tiny C Compiler. Compile and exrcute directly C code programs. (Win/Linux).
- CheckedC. Extension by Microsoft to add security to the language.
- Cello. An extension to add dynamic lists, objects, to avoid the huge runtime of C++.
- Libmill. An extension to add to C coroutines such as that of Go.
- Legacy-cc. The first C compiler made by Dennis Ritchie, for the curious.