Why I prefer C over any other programming language.
We can all acknowledge that the C programming language (meant as C89, here and in the rest of this text) has been successful and influential. The C89 standard is more than 30 years old (while the first occurrence of C dates back more than 50 years ago, in 1972), yet people still use it to write programs and libraries to this day.
Most command-line utilities shipped with Unix-like operating systems are usually written in C. Not only those, but most operating system kernels and high-performance libraries as well, including math and machine learning libraries such as Torch (and consequently PyTorch), Pandas, and NumPy. Even the implementation of some of the most commonly used low-level interfaces, such as OpenCL, OpenGL, and Vulkan, are written in C. And if that wasn't enough to impress you, C is often used to write database servers as well, like Microsoft SQL Server, MariaDB, SQLite, PostgreSQL, IBM Db2, and Oracle Database.
C has its downsides, like every other language. Many people and companies around the world tried to solve those flaws and created new programming languages, but none of those attempts has completely replaced C yet. If those flaws are so bad, there must be a reason why C is still around. To refresh your memory, here is a list of the downsides I'm talking about.
- Memory leaks are easy to make.
- Strings are more difficult to handle, compared to other languages.
- Abstract data types such as hast tables, trees, and graphs, are not present in the language's standard library.
- Network programming (especially in POSIX systems and Windows) is a headache.
- The result of some expressions may depend on the compiler or the CPU architecture in use.
- A function called with null pointers, or a forbidden sequence of function calls, may cause a so-called undefined behavior which usually results in the crash of your program.
- Some operating systems may require different code bases for specific functionalities or interfaces unless you use an external library as an abstraction layer.
With so many issues, you may think that writing programs or libraries is impossible. Or maybe it is possible but the effort put into it exceeds the result. If you considered learning C before reading this article, now you may feel like it's not worth it. Yet you see a lot of people who know C and use it like any other programming language. So you ask yourself, "What are the advantages?". There are many.
- You learn more about your hardware by using a language that puts you closer to it. This helps you optimize your programs, take shortcuts when possible, and avoid unnecessary resource consumption.
- You don't spend time thinking about what feature to use. Many modern programming languages provide several ways of doing the same thing, some of which may even employ the use of another paradigm. The difference between the various ways you find is quite often the language feature you use. In C there are way fewer approaches to a problem, which are usually more straightforward and down to earth.
- It's an ubiquitous language, because almost all operating systems have a C compiler. The reason for that dates back to when C was invented, to be the primary language for programming UNIX systems. Since nowadays most operating systems are Unix-like, except for Windows and a few others, the C compatibility has been kept. GCC and Clang are two widespread compilers, the default for many operating systems, including almost all Linux-based distros and MacOS. Microsoft made a compiler for Windows as well.
- You can access low-level APIs without overhead.
- It's a simple language that is fast to parse, compile, and run. No more waiting for the program, it's the program that waits for you.
- It won't evolve or add new unexpected features unless you change the C language standard you're working with.
- There are libraries for doing everything. Also, they are easy to install, you just need to use your operating system's package manager.
- Some programs help you reveal bugs such as memory leaks by analyzing your code or your program execution. Valgrind is an example.
- There are tons of guides and programmers out there that can help you.
- It's easy to learn the basics of the language because its syntax is familiar and simple.
- There are no strict conventions to follow for naming, formatting, etc., only recommended or most used ones.
So not only you do learn more about how your hardware works, but you also have more control over the execution of your programs, especially how much memory they allocate and how they use it. In addition, your programs generally take less space on disk and run faster, because they only contain what they strictly need and nothing more.
Every language has its use case, as some readers might say. For example, in C it is easy to communicate with low-level interfaces and write efficient code, but it's arguably awful when it comes to web programming. It's also true that C programs usually require more time and effort to be developed and they end up being longer than the same programs written in most other languages. Not only that, but they expose low-level details which often distract programmers from the core logic of the program. Despite all that, I still enjoy writing C, because all those issues are the result of its simplicity-based philosophy, and trying to fix them results in either less control over your code or more syntax elements and rules, which often lead to other issues.
Not everybody knows but C has dialects outside of its standards. One of them is the Plan 9 C dialect (it does not have an official name, so everybody calls it that way), which has improved the C89 standard in many ways. The greatest improvement is a bigger, more comprehensive standard library which also simplifies some tasks and functions that were a headache to work with, networking for example. The language and system libraries are so blended and work so well together that it is not possible to tell where the language ends and the system begins. However, Plan 9 is a topic that needs a separate article to be properly discussed.
Overall, although writing C code requires some attention for low-level details and it's easy to generate segmentation faults, it offers a lot of freedom and it's a very flexible programming language. Along with those, C also provides incredible efficiency and just enough abstraction over Assembly. For serious projects, I would not exchange C with any other programming language.
Last change: Apr 22 2024 (time in GMT, if present)