Many of the projects in C, existing 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 could be conveniently rewritten from the assembler UNIX kernel and get slightly higher level code, less dependent on architecture and allowing to perform more useful work on each 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 mostly written in C with small inserts of the assembler. Linux development began in 1991 and began immediately on C. The following year it was published under the GPL license 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 projects in C are not only what started a decade ago, when the choice of languages ​​was, frankly, rather limited. A lot of C-code is being written now, and new projects are starting there. There are reasons for this.
How exactly does language C rule the world?
Despite the current trend towards the use of high-level languages, the foundation of the IT world is still kept in the C language. These are just some of the systems written in C and used daily by millions of people.
Microsoft Windows
As mentioned above, the Windows kernel is basically code in C. It can be applied to this operating system differently, but for several decades it has been the largest share of the desktop OS.
Linux
Linux is also written mostly in C. 97% of all supercomputers in the world work on Linux. Its large share in the server market is indisputable, and someone uses it on the desktop.
Mac
You will not 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 under what operating system, in the absolute majority of cases we talk about code in C.
Mobile OS
The cores of iOS, Android, and Windows Phone are also written in C. In fact, they are mobile adaptations of the previously existing kernels of Mac OS, Linux, and Windows. So right now in your pocket running C-code.
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 an uninhabited island, you encounter systems on databases (and, as a result, code operation in C) daily.
Graphics, video, special effects
Modern cinema is created mainly special software designed for this. High-load systems that require processing of huge amounts of video data are often written in C and C ++. The more efficient the code, the faster the result will be obtained, the more opportunities will open up for artists and animators to fine-tune the details of the future cinema master. And the more money the filmmaker will save.
Embedded development
Just remember your usual day. You wake up from an alarm clock (which may be controlled by a microcontroller with a code in C). Then your microwave (also with a microcontroller) warms up your breakfast. You pour coffee from the coffee maker (well, you already understood). At breakfast, you turn on the TV or radio (again embedded iron and code). Then you open the garage door (with remote). A very large part of the code in all 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), in it:
- automatic transmission control
- tire pressure detection system
- sensors
- preserve the position of the seats and mirrors
- onboard computer screen
- signaling
- electronic stabilization system
- Cruise control
- climate control
- keyless ignition
- seat heating
- airbags
... this list can be greatly expanded depending on the trickyness of your car.
Every day you come across ATMs, cash registers, traffic lights, access control systems, video surveillance - everywhere microcontrollers, everywhere the code is C.
Why do people still write in C?
Today there are many programming languages ​​and many of them, let's say straightforwardly, allow writing code faster and easier than in C. High-level languages ​​give us excellent tools for abstractions, rich standard libraries, work with different data formats out of the box, easy access to networks and web, to databases, etc.
But, in spite of 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 close all the niches. In some areas, C is still the preferred choice, and in some, the only possible option.
The combination of portability and efficiency
C was developed as a “portable assembler”. It is as close to the machine code as possible, but still it is not machine code. There is at least one C compiler for any existing 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 assembly code to a level that will improve the result generated by default with the standard C compiler.
Language C has also become something of a universal language for communicating programmers. You do not know in what language to express the idea, so that a developer you are not familiar with on the other side of the world understand what algorithm you wrote? Just write to C. Alex Allain from Dropbox said:
C is an excellent language for expressing general programming ideas in a way that is convenient for most people. Many of the conventions and principles of the C language were borrowed by other languages, so that with the help of the C code you will probably be able to explain even to those programmers who never wrote C on C
Direct and fast memory access
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 the memory addresses, which are actually the input / output ports of some devices. Drivers and OS kernel communicate through them with the outside world. Being able to do this easily and quickly 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 the UART to some peripheral device, then when bit number 4 in the memory cell at address 0x40008001 will be set to 1.
The C code for the byte mandrel 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;
Pay attention to the keyword
volatile - it is necessary here to prevent the compiler from optimizing, for example, a cycle in which the same value is assigned to a memory cell at a given address several times in a row (the compiler can decide to do it only once, if the keyword volatile will not be).
Deterministic use of resources
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 must use their resources very sparingly. Some of them work in real-time operating systems, where calling the garbage collector (with its indefinitely long runtime) is not allowed. And the ban on dynamic memory allocation requires the availability of convenient tools for working with pre-allocated memory, such as C pointers.
Code size
Rantaym C is very small. The binary received 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 is not given for free. This is certainly a good tool in some cases, but it requires additional code.
Let's look at this C ++ code like this:
Class methods A, B and C are declared somewhere in other files. Thus, the compiler cannot yet analyze them and understand whether they generate exceptions. That is, it must be prepared for the fact that yes, it is generated. And they need to be processed. If an exception occurs, you need to be able to unwind the stack and call the destructors of all created objects. This all increases the amount of generated code. We get an overhead of C ++ compared to C. For many applications, this is not acceptable. And, while C ++ compilers often provide an opportunity to turn off the use of exceptions, this is also not a gift, since the C ++ standard library code uses them for error messages. You have to either live without this information, or rewrite parts of the standard library.
And we are also talking about the C ++ language, whose principle is “you don’t pay for what you don’t use”. For other languages, the overhead of both the size of the binaries and the speed will be even worse. The C language does not give you a lot of cool features, but those that you use will work exactly the way it is written in your code.
Reasons to study C (if you have not already done so)
Language C 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 the implementation of an algorithm in their language simply because they did not understand the details of its implementation in C.
Consider also that C is an old and common language, so 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 difficulties in understanding what is happening, we have to go down to lower levels and this all ends up “speaking in C, it works like this ...” - and we remember “pointers” ( even in languages ​​where there are none) and copying “by reference or by value” (again, even if the default language implements only one thing), and so on.
We rarely get to the details of what is happening to the assembly code, but at the C language level we really think and speak.
The opportunity to work on interesting projects
A lot of really cool things are written in C. When you decide for your own pleasure or for money, decide to dig in the database engine or OS kernel - you will do it in C. There is no reason to deny yourself the pleasure to touch something so fundamental because of ignorance of this programming language.
Conclusion
The world is not ruled by masons. The world is controlled by programmers in C.
Language C has no “shelf life”. It is close to hardware, we port it and is very practical in terms of the use of resources. You can find a project to your liking in a bunch of interesting areas. Greater functionality of more modern languages ​​does not necessarily mean a great practical use of the code that will be written on them.
The world is full of devices in which the code works in C. We use them every day. C language is not only the past, the present, but, as far as the eye can see, the future is in many areas of software development.