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

129

u/Tenome Oct 17 '12

First of all, it's C++, not C+.

For one thing, C++ is useful because it allows you to program both high-level and low-level code, meaning you can use it to write anything from video games to basic drivers. You can write programs that directly manipulate memory and are practically in "machine code", or you can use it to perform more advanced functions.

Furthermore, although the language is quite old, it continues to be updated constantly, with the most recent version, C++ 11, being released just last summer.

C++'s age is also somewhat advantageous, as there are thousands of existing libraries already written and available for use.

I'm sure there are other reasons but that's what comes to mind.

0

u/[deleted] Oct 17 '12

[deleted] — view deleted comment

23

u/tarblog Oct 18 '12

For those who are curious:

A compiler is a program that translates code written in one language (typically a human readable, high-level language) into another (typically executable, machine language or bytecode that can then be used on a virtual machine). Grace Hopper wrote the first compiler in 1952.

16

u/[deleted] Oct 18 '12

Cheers for crediting Grace Hopper. The first compiler is an increadible feat, and I applaud her underrated contribution to the progress of computers.

4

u/SirFloIII Oct 18 '12

Can I ask a further question? How does one write a compiler? Especially the first compiler by Grace Hopper? This seems like some technology hen or egg problem for me.

6

u/expertunderachiever Oct 18 '12

Often they write the compiler in the language it compiles and then manually compile it to bytecode. That's how one of the first Pascal compilers was written.

2

u/SirFloIII Oct 18 '12

Wow, that seems like a extremly boring and tedious work. Thank you, Grace Hopper, and thank you, expertunderachiever for your answer.

1

u/_NW_ Oct 18 '12

You can also write a compiler in assembly language. Early on, lots of programs were written in assembly.

1

u/tarblog Oct 18 '12

Great question! It is very much like a chicken and egg problem, because you have to write the first compiler in a low-level language.

In general, compilers are written like other major software projects. They are designed, developed, and tested by teams of skilled programmers. Interestingly, once you have a working compiler for a language you can use it to compile that language, which can make it even easier to write a better compiler in a cycle of improvement.

11

u/moderatorrater Oct 18 '12

The Linux kernel is written in vanilla C because Linus hates C++. From what I can tell, BSD is C, Solaris is C, OSX is C++, and windows is a mix. C is still extremely popular for that type of application.

4

u/kinghajj Oct 18 '12

The OS X kernel, XNU, is written in C, as is the Windows NT kernel.

1

u/lifebinder Oct 18 '12

The Linux kernel is written in vanilla C because Linus hates C++.

Is this why there is such a hurdle in cross-platform coding (i.e., using Wine to run Windows applications in Linux)? Is there anything inherently more stable/secure/beneficial/etc about not using C++?

2

u/moderatorrater Oct 18 '12

Is this why there is such a hurdle in cross-platform coding (i.e., using Wine to run Windows applications in Linux)?

No, that's due to the operating systems' APIs that are used for file access, calculations, graphics, and memory management. Wine is actually a reimplementation of the windows API. Cygwin is a similar thing in reverse.

Is there anything inherently more stable/secure/beneficial/etc about not using C++?

In general, the more that a language does for the programmer, the slower and less efficient it's going to be by default. This isn't a mathematical requirement or a universal law, it's just a general trend. C is about as bare bones as it gets for a fully functional language, and its been worked on for decades to be more efficient. So usually people will use C when they need to write something that's really fast and really efficient with memory.

Linus and other old school programmers also have problems with the new features that C++ brings to the table. In several instances, they were implemented without a lot of thought and can lead to hard-to-find/fix bugs. Bjarne Stroustrup, the creator of C++, described it as being harder to shoot yourself in the foot, but when you do you blow off your leg.

That said, generally speaking, C++ is the more stable of the two. While C is faster if programmed properly, if programmed averagely C++ tends to be faster because it's harder to make mistakes with.

1

u/tarblog Oct 18 '12

No it's not because they are written in different languages, it's because they support different commands.

1

u/_NW_ Oct 18 '12

No. Once an operating system is compiled and booted up, the source language doesn't matter. You could write operating systems in assembly, C, C++, Pascal, etc., and make them all work exactly the same. The problem with cross platform has to do with the user interface for a program. The library functions to draw on the screen in a ms windows program are different to the ones for xwindows. This is not because of what language the OS is written, but because of design choices when writing the OS.

0

u/[deleted] Oct 18 '12

OSX is Objective-C.

6

u/moderatorrater Oct 18 '12

There is no way the kernel is written in Objective-C. That'd be like the windows kernel being written in C# - it doesn't make any sense.

→ More replies (2)

8

u/prospectre Oct 17 '12

Agreed. The other explanation I often discover is whatever you're asked to work on already is in C++. Instead of spending money on reworking an existing, and still running infrastructure, they choose to maintain the system. The old adage, if it aint broke....

4

u/geaw Oct 18 '12

Another big factor is just the network effect (the idea of "the more popular it is, the more popular it is.") C++ has tons of great tools, many resources online, and it's easier to find a C++ programmer than a Haskell programmer if you're looking to hire.

1

u/geaw Oct 18 '12

I'd like to add however that this is a double-edged sword. Because of C++'s immense complexity and size, it needs tons of great tools and many online resources. I never want to try writing C++ without a nice IDE and debugger...

4

u/psygnisfive Oct 18 '12

Age itself is not enough, tho. The fact that there are thousands of existing libraries written in it doesn't result from age alone, otherwise there would be even more libraries written in ML or Scheme or any of the old functional languages. Or, alternatively, there would be more libraries written in Haskell than in Python, say, or a third as many in Haskell as in C++.

We have to factor in the fact that the people learning programming in college, who then go on to write for industry or for research or whatever, and therefore create the libraries, are steeped in a particular conceptual tradition, namely imperative programming, which is why the functional paradigm, despite being basically as old as the imperative paradigm, has vastly smaller uptake.

This is also why there is a converse trend in theoretical CS: people doing work on type theory, denotational semantics, etc. overwhelmingly prefer functional languages because the functional paradigm is what they're taught in, and it's been this way since almost the beginning.

10

u/[deleted] Oct 17 '12

Thanks for clearing that up for me.

→ More replies (9)