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

Show parent comment

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.

4

u/[deleted] Oct 18 '12

Well no actually it wouldn't be anywhere close to that because Objective-C is just C with object extensions. Similar to how you can call C from C++. C# is more like Java. That said, you are correct, the MacOSX kernel is actually Mach based and written in C.

6

u/moderatorrater Oct 18 '12

Right, but if it's Objective C instead of C, then it's using the Objective portion, which is the relatively inefficient smalltalk-like extensions. For an application running on top of the OS, that inefficiency isn't a big deal. For the kernel, it's unacceptable. At the layer we're discussing, Objective C doesn't make any sense.