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

110 comments sorted by

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.

3

u/elmonstro12345 Oct 17 '12

I think the thing with Lisp, Haskell, Prolog, and similar languages is that they are VERY, incredibly (to me, almost impossibly) good at what they are designed to do, but they aren't that great with other tasks. So when a business looks to hire someone, they go for someone with C/C++/Java, etc. because those languages are popular, and because they are quite a bit more flexible than Haskell, etc., even if they aren't as good in a lot of areas.

1

u/[deleted] Oct 17 '12

It also depends on the business and the role. If you're creating things for Windows, they are looking for .NET developers. If you're doing TIBCO, Java.

1

u/MdxBhmt Oct 18 '12

I'm interested to know which task haskell and lisp are not that great.

2

u/itoowantone Oct 19 '12

For Lisp, the deficiency is in the implementation, which could be fixed with money, and not with the language design. There are vast amounts of money for improving e.g. C++ compilers but very little available for improving Lisp compilers. Also note there used to be Lisp-specific processors, which would e.g. use hardware to tag memory pages by data type. C models today's processors, I.e. Intel x86. Even so, today's Lisp compilers often can perform close to, as well as, or sometimes better than C or C++.

The larger issue is library support and interfacing Lisp to non-Lisp APIs. If the lib were written in Lisp, calling would be fast. Having to cross the C-Lisp boundary often means converting data formats, due to the way data in Lisp is tagged by type on x86 CPUs. That slows things down. All or most of these issues could be addressed with sufficient funding, but not enough people are buying commercial Lisps to support the development. The open source community does wonderful things with Lisp, but that community is orders of magnitude smaller than the C/C++ community, so only so much can be done, comparatively.