C still rules the world
- Transfer
Many of the C language projects that exist today began to be developed decades ago. The UNIX operating system started in 1969 (and was written in assembler), but already in 1972 it was rewritten in C. More precisely, this C language was created so that something would appear that would be convenient to rewrite the UNIX kernel from assembler and get slightly higher-level code, less dependent on architecture and allowing you to do more useful work on every line of written code.
The development of the Oracle database began in 1977 (also in assembler) and was also rewritten in C in 1983. By that time, it was already one of the most popular languages in the world.
In 1985, Windows 1.0 was released. Although the code of the Windows operating system is not open, it is well known that the kernel is mainly written in C with small assembler inserts. Linux development began in 1991 and began immediately in C. The following year, it was published under the GPL and used as part of the GNU Operating System, which itself began as a project in C and Lisp, so many components were written in C.
But C projects are not only what started decades ago, when the choice of languages, frankly, was quite limited. A lot of C-code is written and now, new projects begin on it. There are reasons for this.
Despite the current trend towards the use of higher-level languages, the foundation of the IT world is still based on C. These are just some of the systems written in C that are used daily by millions of people.
As mentioned above, the Windows kernel is mainly C code. You can have a different attitude to this operating system, but for several decades now it has occupied the largest market share for desktop OS.
Linux is also written mostly in C. 97% of all the world's supercomputers run on Linux. Its large share in the server market is undeniable, and someone uses it on the desktop.
You won’t believe it, but the third “big” OS in our list is also written in C (at least its core).
When we talk about a device driver, it doesn’t matter what operating system, in the vast majority of cases we are talking about code in C.
The kernels of iOS, Android, and Windows Phone are also written in C. In essence, they are mobile adaptations of pre-existing Mac OS, Linux, and Windows kernels. So right now the C code is working in your pocket.
All the most popular databases in the world (Oracle Database, MySQL, MS SQL Server, PostgreSQL) are written in C (some in C / C ++). Databases are used in all types of systems: finance, media, telecom, healthcare, education, sales, web, etc. If you do not live on a desert island, you encounter database systems (and, as a result, the work of C code) every day.
Modern cinema is created mainly by special software designed for this. Highly loaded systems, which are required to process huge amounts of video data, are often written in C and C ++. The more effective the code, the faster the result will be, the more opportunities will open up for artists and animators to fine-tune the details of the future film masterpiece. And the more money the filmmaker will save.
Just remember your usual day. You wake up from an alarm clock (which is possibly controlled by a microcontroller with a code for C). Then your microwave (also with a microcontroller) warms up your breakfast. You pour coffee from the coffee machine (well, you already understand). At breakfast, you turn on the TV or radio (again embedded iron and code). Then you open the garage door (from the remote control). A very large part of the code in all of the above devices is written in C.
And so you got to your car. The following systems work (and most likely are written in C):
… This list can be greatly expanded depending on the sophistication of your car.
Every day you come across ATMs, cash registers, traffic lights, access control systems, video surveillance - everywhere there are microcontrollers, everywhere there is a C code.
Today, there are many programming languages, and many of them, frankly, allow you to write code faster and easier than in C. High-level languages give us excellent abstraction tools, rich standard libraries, working with different data formats out of the box, easy access to networks and the web, to databases, etc.
But, despite all of the above, there are reasons why the C code is still working and will work for a long time. In the world of software development there is no “silver bullet”, there is no one language that would cover all niches. In some areas C is still the preferred choice, and in some it’s the only possible choice.
C was developed as a “portable assembler”. It is as close to machine code as possible, but still it is not machine code. There is at least one C compiler for any processor architecture in the world (well, unless you soldered your own processor somewhere in the garage yesterday). Moreover, for the main architectures, C compilers have been written and optimized for several decades. And this means that they are just wildly effective. It will take you a lot of time to write and optimize the assembler code to the level that improves the result generated by default by the standard C compiler.
C has also become a kind of universal language for programmers to communicate. You don’t know in what language to express the idea so that a developer unfamiliar to you on the other side of the world understands what algorithm you wrote? Just write in C. Alex Allain from the Dropbox team said this:
Easy access to arbitrary memory cells along with pointer arithmetic made the C language an excellent choice for system programming. At the junction of hardware and software are memory addresses, which are actually the input / output ports of some devices. Drivers and OS kernels communicate through them with the outside world. The ability to do this quickly and easily is the key to efficiency.
The microcontroller can be designed in such a way, for example, that a byte at address 0x40008000 will be sent by UART to some peripheral device, then when bit number 4 in the memory location at address 0x40008001 will be set to 1.
Code C for sending a byte for such a microcontroller will look like this:
Pay attention to the volatile keyword - it is necessary here to prevent the compiler from optimizing, for example, a loop in which the memory cell at a given address is assigned the same value several times in a row (the compiler can decide to do this only once if the keyword volatile will not).
It is a well-known fact that system programming cannot rely on languages with a garbage collector. Moreover, in some systems dynamic allocation of memory is generally prohibited. Embedded applications should use their resources very sparingly. Some of them work in real-time operating systems, where a call to the garbage collector (with its indefinitely long run time) is not acceptable. A ban on dynamic memory allocation requires the availability of convenient means of working with previously allocated memory - such as C pointers.
Rantime C is very small. The binary obtained after compiling and linking the C code will be smaller than the binaries in many other languages. Even compared to C ++, the size is often 2 times smaller. C ++ is forced to support abstractions, such as exceptions, which are not given for free. This is certainly a good tool in some cases, but it requires additional code.
Let's look at such a code in C ++:
Class methods A, B, and C are declared somewhere in other files. Thus, the compiler cannot yet parse them and see if they throw exceptions. That is, he must be prepared for the fact that yes, they generate. And they need to be processed. If an exception occurs, you need to be able to rewind the stack and call the destructors of all created objects. This all increases the amount of generated code. We get an overhead of the C ++ language compared to C. For many applications this is not permissible. And, although C ++ compilers often make it possible to disable the use of exceptions, this is also not in vain, since the code of the standard C ++ library uses them for error messages. You will either have to live without this information, or rewrite parts of the standard library.
And we are talking about the C ++ language, whose principle is "You do not pay for what you do not use." For other languages, overhead will be even worse in terms of binary size and speed. The C language does not give you many cool features, but those that you use will work exactly as it will be written in your code.
C language is not so difficult to learn, but the benefits of this can be substantial.
As mentioned above: if you write in C, you will always be understood. Many implementations of algorithms in books or articles are given for the first time in the C language. This gives maximum portability, maximum ease of use on any platform. I saw how programmers in some high-level languages scoured the Internet in search of an implementation of an algorithm in their language simply because they did not understand the details of its implementation in C.
Bear in mind also that C is an old and common language, so if you If you are looking for some kind of algorithm on the Internet, then you will have the maximum chance if you are looking for its C-implementation.
When my colleagues and I discuss the nuances of the behavior of some parts of the code and we have difficulty understanding what is happening, we have to go down to lower levels and it all ends with “speaking C, it works like this ...” - and we recall the “pointers” ( even in languages where they are not) and copying “by reference or by value” (again, even if the default language implements only one thing) and so on.
We rarely get into the details of what is going on in assembly code, but at the level of language C we really think and speak.
Many really cool things are written in C. When you decide to dig into the database engine or the kernel of the OS for your own pleasure or for money - you will do it in C. There is no reason to deny yourself the pleasure of touching something so fundamental because of ignorance of this programming language.
The world is not ruled by Masons. The world is ruled by programmers in C.
Language C has no “expiration date”. It is close to hardware, portable and very practical in terms of resource use. You can find a project for you in a heap of interesting areas. The greater functionality of more modern languages does not necessarily mean the great practical benefit of the code that will be written in them.
The world is full of devices that use C code. We use them every day. C language is not only the past, the present, but also, as far as the eye can see, the future in many areas of software development.
The development of the Oracle database began in 1977 (also in assembler) and was also rewritten in C in 1983. By that time, it was already one of the most popular languages in the world.
In 1985, Windows 1.0 was released. Although the code of the Windows operating system is not open, it is well known that the kernel is mainly written in C with small assembler inserts. Linux development began in 1991 and began immediately in C. The following year, it was published under the GPL and used as part of the GNU Operating System, which itself began as a project in C and Lisp, so many components were written in C.
But C projects are not only what started decades ago, when the choice of languages, frankly, was quite limited. A lot of C-code is written and now, new projects begin on it. There are reasons for this.
How exactly does language C rule the world?
Despite the current trend towards the use of higher-level languages, the foundation of the IT world is still based on C. These are just some of the systems written in C that are used daily by millions of people.
Microsoft Windows
As mentioned above, the Windows kernel is mainly C code. You can have a different attitude to this operating system, but for several decades now it has occupied the largest market share for desktop OS.
Linux
Linux is also written mostly in C. 97% of all the world's supercomputers run on Linux. Its large share in the server market is undeniable, and someone uses it on the desktop.
Mac
You won’t believe it, but the third “big” OS in our list is also written in C (at least its core).
Drivers
When we talk about a device driver, it doesn’t matter what operating system, in the vast majority of cases we are talking about code in C.
Mobile OS
The kernels of iOS, Android, and Windows Phone are also written in C. In essence, they are mobile adaptations of pre-existing Mac OS, Linux, and Windows kernels. So right now the C code is working in your pocket.
Database
All the most popular databases in the world (Oracle Database, MySQL, MS SQL Server, PostgreSQL) are written in C (some in C / C ++). Databases are used in all types of systems: finance, media, telecom, healthcare, education, sales, web, etc. If you do not live on a desert island, you encounter database systems (and, as a result, the work of C code) every day.
Graphics, video, special effects
Modern cinema is created mainly by special software designed for this. Highly loaded systems, which are required to process huge amounts of video data, are often written in C and C ++. The more effective the code, the faster the result will be, the more opportunities will open up for artists and animators to fine-tune the details of the future film masterpiece. And the more money the filmmaker will save.
Embedded Development
Just remember your usual day. You wake up from an alarm clock (which is possibly controlled by a microcontroller with a code for C). Then your microwave (also with a microcontroller) warms up your breakfast. You pour coffee from the coffee machine (well, you already understand). At breakfast, you turn on the TV or radio (again embedded iron and code). Then you open the garage door (from the remote control). A very large part of the code in all of the above devices is written in C.
And so you got to your car. The following systems work (and most likely are written in C):
- automatic gearbox control
- tire pressure detection system
- sensors
- preservation of the seats and mirrors
- on-board computer screen
- signaling
- electronic stabilization system
- Cruise control
- climate control
- keyless ignition
- seat heating
- airbags
… This list can be greatly expanded depending on the sophistication of your car.
Every day you come across ATMs, cash registers, traffic lights, access control systems, video surveillance - everywhere there are microcontrollers, everywhere there is a C code.
Why do people still write in C?
Today, there are many programming languages, and many of them, frankly, allow you to write code faster and easier than in C. High-level languages give us excellent abstraction tools, rich standard libraries, working with different data formats out of the box, easy access to networks and the web, to databases, etc.
But, despite all of the above, there are reasons why the C code is still working and will work for a long time. In the world of software development there is no “silver bullet”, there is no one language that would cover all niches. In some areas C is still the preferred choice, and in some it’s the only possible choice.
The combination of portability and efficiency
C was developed as a “portable assembler”. It is as close to machine code as possible, but still it is not machine code. There is at least one C compiler for any processor architecture in the world (well, unless you soldered your own processor somewhere in the garage yesterday). Moreover, for the main architectures, C compilers have been written and optimized for several decades. And this means that they are just wildly effective. It will take you a lot of time to write and optimize the assembler code to the level that improves the result generated by default by the standard C compiler.
C has also become a kind of universal language for programmers to communicate. You don’t know in what language to express the idea so that a developer unfamiliar to you on the other side of the world understands what algorithm you wrote? Just write in C. Alex Allain from the Dropbox team said this:
C is a great language for expressing common ideas in programming in a way convenient for most people. Many of the conventions and principles of the C language were borrowed by other languages, so with the help of the code in C you will probably be able to communicate even with those programmers who never wrote in C
Direct and quick access to memory
Easy access to arbitrary memory cells along with pointer arithmetic made the C language an excellent choice for system programming. At the junction of hardware and software are memory addresses, which are actually the input / output ports of some devices. Drivers and OS kernels communicate through them with the outside world. The ability to do this quickly and easily is the key to efficiency.
The microcontroller can be designed in such a way, for example, that a byte at address 0x40008000 will be sent by UART to some peripheral device, then when bit number 4 in the memory location at address 0x40008001 will be set to 1.
Code C for sending a byte for such a microcontroller will look like this:
#define UART_BYTE *(char *)0x40008000
#define UART_SEND *(volatile char *)0x40008001 |= 0x08
void send_uart(char byte)
{
UART_BYTE = byte; // записываем отправляемое значение в ячейку 0x40008000
UART_SEND; // поднимаем бит номер 4 в ячейке 0x40008001
}
Pay attention to the volatile keyword - it is necessary here to prevent the compiler from optimizing, for example, a loop in which the memory cell at a given address is assigned the same value several times in a row (the compiler can decide to do this only once if the keyword volatile will not).
Deterministic Resource Usage
It is a well-known fact that system programming cannot rely on languages with a garbage collector. Moreover, in some systems dynamic allocation of memory is generally prohibited. Embedded applications should use their resources very sparingly. Some of them work in real-time operating systems, where a call to the garbage collector (with its indefinitely long run time) is not acceptable. A ban on dynamic memory allocation requires the availability of convenient means of working with previously allocated memory - such as C pointers.
Code size
Rantime C is very small. The binary obtained after compiling and linking the C code will be smaller than the binaries in many other languages. Even compared to C ++, the size is often 2 times smaller. C ++ is forced to support abstractions, such as exceptions, which are not given for free. This is certainly a good tool in some cases, but it requires additional code.
Let's look at such a code in C ++:
// Объявление класса А. Реализация методов находится где-то в другом месте
class A
{
public:
A(); // Конструктор
~A(); // Деструктор (called when the object goes out of scope or is deleted)
void myMethod(); // Просто метод
};
// Объявление класса В. Реализация методов находится где-то в другом месте
class B
{
public:
B(); // Конструктор
~B(); // Деструктор
void myMethod(); // Просто метод
};
// Объявление класса С. Реализация методов находится где-то в другом месте
class C
{
public:
C(); // Конструктор
~C(); // Деструктор
void myMethod(); // Просто метод
};
void myFunction()
{
A a; // Вызван конструктор a.A()
{
B b; // Вызван конструктор b.B()
b.myMethod();
} // Вызван деструктор b.~B()
{
C c; // Вызван конструктор c.C()
c.myMethod();
} // Вызван деструктор c.~C()
a.myMethod();
} // Вызван деструктор a.~A()
Class methods A, B, and C are declared somewhere in other files. Thus, the compiler cannot yet parse them and see if they throw exceptions. That is, he must be prepared for the fact that yes, they generate. And they need to be processed. If an exception occurs, you need to be able to rewind the stack and call the destructors of all created objects. This all increases the amount of generated code. We get an overhead of the C ++ language compared to C. For many applications this is not permissible. And, although C ++ compilers often make it possible to disable the use of exceptions, this is also not in vain, since the code of the standard C ++ library uses them for error messages. You will either have to live without this information, or rewrite parts of the standard library.
And we are talking about the C ++ language, whose principle is "You do not pay for what you do not use." For other languages, overhead will be even worse in terms of binary size and speed. The C language does not give you many cool features, but those that you use will work exactly as it will be written in your code.
Reasons to learn C (if you haven't already)
C language is not so difficult to learn, but the benefits of this can be substantial.
Mutual language
As mentioned above: if you write in C, you will always be understood. Many implementations of algorithms in books or articles are given for the first time in the C language. This gives maximum portability, maximum ease of use on any platform. I saw how programmers in some high-level languages scoured the Internet in search of an implementation of an algorithm in their language simply because they did not understand the details of its implementation in C.
Bear in mind also that C is an old and common language, so if you If you are looking for some kind of algorithm on the Internet, then you will have the maximum chance if you are looking for its C-implementation.
Understanding How Your Computer Works
When my colleagues and I discuss the nuances of the behavior of some parts of the code and we have difficulty understanding what is happening, we have to go down to lower levels and it all ends with “speaking C, it works like this ...” - and we recall the “pointers” ( even in languages where they are not) and copying “by reference or by value” (again, even if the default language implements only one thing) and so on.
We rarely get into the details of what is going on in assembly code, but at the level of language C we really think and speak.
The ability to work on interesting projects
Many really cool things are written in C. When you decide to dig into the database engine or the kernel of the OS for your own pleasure or for money - you will do it in C. There is no reason to deny yourself the pleasure of touching something so fundamental because of ignorance of this programming language.
Conclusion
The world is not ruled by Masons. The world is ruled by programmers in C.
Language C has no “expiration date”. It is close to hardware, portable and very practical in terms of resource use. You can find a project for you in a heap of interesting areas. The greater functionality of more modern languages does not necessarily mean the great practical benefit of the code that will be written in them.
The world is full of devices that use C code. We use them every day. C language is not only the past, the present, but also, as far as the eye can see, the future in many areas of software development.