r/askscience • • Oct 17 '12
Why are programs and operating systems still written in C+?
I guess it's more of a computer science question, but I've always wondered why some of the most advanced programs are written in a 25+ year old programming language. Surely someone has come up with a more efficient and powerful code, so why do people still use C+? Also, I don't know anything about programming and what not, so please use simple terms and phrases.
66
Upvotes
View all comments
8
u/Platypuskeeper Physical Chemistry | Quantum Chemistry Oct 17 '12
C and C++ are two different programming languages. C++ is a bit newer (going mainstream around the late 1980s) Neither are really that old though, as they've both undergone changes and had features added since they were first created. (And of course, newer isn't always better)
Popular OSes exist in both languages. Linux is exclusively C (its creator Linus Torvalds is strongly against having C++ in it). From what I gather, Windows uses both (as C++ can be combined with standard C code).
Now, operating systems are by definition quite 'low level' code. They provide the basic interface between software and hardware. So it's usually desirable then, to have a fine-grained control over what's going on at the hardware level. You'd get the most control if you wrote in machine code (assembly language), but that's entirely machine-specific, and would mean your operating system would have to be completely rewritten to run on different hardware. So you tend to want to strike a balance between something that's higher-level in order to be portable, but low-level enough to give you a lot of control over what's going on. If you write an OS in a high-level language, you're effectively delegating much of that to the compiler program that turns your code into machine code, and to the libraries of the programming language.
When it comes to application software, there are other reasons. First, lower-level programming languages have some benefits; it's often possible to write faster code in them (although writing in a low-level language in no way guarantees that your code will be faster). As Tenome pointed out, C++ is quite flexible in that it there are large libraries of existing routines that can be used, which effectively make it a fairly high-level language.
Another factor is momentum. A lot of existing programs are written in C/C++, and it's seldom worth the effort to rewrite a large program in another language for the relatively small potential benefits. There's also a lot of programmers who know C/C++, and people tend to be a bit unwilling to learn a new programming language because it's better for a particular purpose. If they're new to the language, they might not be able to fully exploit its benefits anyway. Also, if they don't know the language, they may simply be unaware of the potential benefits.
There's a business side to that, too. It doesn't really matter if programming language X is better for your task, if you can't find programmers who know it! If you can write a program 10% faster in X but have to pay the programmer 20% more because it's a rare skill, that doesn't make good business sense. So economics favors the existing and popular languages.
If you look at language popularity statistics, C and C++ are in the top 3, accompanied in the top 5 by Java and JavaScript, which have relatively similar syntax. Languages like Haskell and Lisp, which are quite different and can do certain things much more simply/elegantly are rather unpopular. So despite the fact that they may be a technically better choice in a lot of situations, they're not necessarily a better business choice.