C is one of the most popular language around all the world, it was initially developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs. Many thousands of projects was developed using C since then. It’s used for all kinds of needs, we can enumerate OS, embedded applications, gaming development, image processing, word processing and database engines.
Almost all the other languages have evolved over years, it’s not really the case of the C language, for example the C11 standatd does not bring many new features to C, only few modifications are introduced.
What makes The C language so popular even after 50 years after its creation?
I- Language stability.
Back to 1990 the beginning of Microsoft Word
The developers of these days remember this was a time when a typical personal computer might have an 8 Mhz processor, 1 megabyte of memory, a 20 megabyte hard disk, and a floppy disk drive. It was a big challenge to develop a word processing application.
Microsoft released recently the “Microsoft Word 1.1″ source code in the Copmuter History Museum. It is worth to take a look to its source code, even if it was implemented 25 years ago, we can learn very interesting techniques from it.
Here’s a code snippet from its source code:
As we can observe the function was declared in K&R style. All the functions use the same style.
When exploring many “Microsoft Word 1.1″ source files, the interesting remark is the fact that the techniques used in these projects not differ a lot from the “Microsoft Word 1.1″ implementation ones.
Let’s take as example the following source code:
It seems that this code is from a C project created recently in GitHub. No sign that’s a code from 25 years ago.
1994 was the year of the first release version of Linux
On 14 March 1994, Linux 1.0.0 was released, but the development was started in 1991, thanks to Linus Trovalds and all the other contributors. The source code of this first version is available here.
Here’s a snippet code from the sched.c source file:
When exploring the source files of Linux kernel version 1 and all the newest ones, the very surprising remark is the fact they are very similar, no big changes in the style or the techniques used. From the beginning the code was simple, clean and basic.
II- Basic mechanisms
Modularity is a software design technique that increases the extent to which software is composed from separate parts , you can manage and maintain modular code easily.
For procedural language like C where no logical artifacts like namespace, component or class does not exist, we can modularize by using directories and files.
Here are some possible scenarios :
- Put all the source files in one directory
- Isolate files related to a module or a sub module into a specific directory.
For example in case of the Linux kernel, directories and sub directories are used to modularize the kernel source code.
Encapsulation is the hiding of functions and data which are internal to an implementation. In C, encapsulation is performed by using the key word static . These entities are called file-scope functions and variables.
Let’s search for all static functions by executing the following CQLinq query
We can use the Metric view to have a good idea how many functions are concerned. In the Metric View, the code base is represented through a Treemap. Treemapping is a method for displaying tree-structured data by using nested rectangles. The tree structure used in a CppDepend treemap is the usual code hierarchy:
- Projects contains directories.
- Directories contains files.
- Files contains structs, functions and variables.
The treemap view provides a useful way to represent the result of a CQLinq request, so we can visually see the types concerned by the request.
As we can observe many functions are declared as static.
Let’s search now for the static fields:
The same remark as the functions, many variables are declared as static.
In the Linux kernel source code the encapsulation is used whenever the functions and variables must be private to the file scope.
After a brief trip in the past of some C source code projects, they are very few implementation techniques change between them and the new C projects ones.
The power of C is its stability over years, it remains basic, no advanced mechanism was introduced to the language, the code still simple to understand and maintain.