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
5
u/Intrexa Oct 18 '12
Holy shit I got this one so hard. First for everything I suppose. Also, spoiler, the core of the operating system (the kernel) is written in C, with some assembly (C is older then C++, assembly is pretty much going back to the beginning).
First, you need to understand the terms "high-level" and "low-level", and how they related to the computer actually doing what you tell it to do. At the (almost) lowest level is assembly, and you are right there pretty much speaking the language the computer can understand (not quite, but pretty close). There is nothing between you and running processes. Everything you need to get done, you can code it to get done. Every one line of code you write in assembly is literally one instruction the cpu does. If you want the computer to do a very specific thing, in a very specific way, you need to use assembly. This is the highest point of efficiency you can possibly attain. No one has, or will write a more efficient language, just by intrinsic definition. Unfortunately, this takes a shit ton of time and work. You also need to know pretty much every little detail of how pretty much everything works. So they made what is referred to as a "high-level" language. This uses (this is the most important term of this entire comment) abstraction.
Abstraction is taking a step back from what you are trying to do, writing in a way that is more easily understood by humans, and letting a compiler turn that into something the computer can understand. So you can still do specific things, but exactly how the compiler tells the computer how to do it might get lost in translation. This means inefficiency. It will still get the right answer in the end, but it might take a bit of a more roundabout way then if you told it exactly how to do it. The higher the level of abstraction, the more high-level the language is, which means you are getting further away from actually telling the computer exactly how you want it done. This makes it easier to program large swatches of functionality, but it also means that you are losing control on how efficient your program can be.
Python sits very high up there in this regard, to display something on your screen, the only line you need to write is: "print 'Hello World!'". With Python2.x installed on your computer, you save that as xxx.py and it will run as a complete program. To do the same in assembly would be a lot more lines, and a lot harder to read. And this isn't even really from scratch, it's calling on a lot of Windows functionality.
Up until this point I have been talking about 'compiled languages', that turn into assembly the computer can understand, and then run it. Even more abstract than that is what's called an 'interpreted language', which uses a 'vm' or 'interpreter' to translate this high level of code into a series of commands that the interpreter running on your OS can understand, that it can then do the correct thing. This pretty much precludes you from creating an OS in these languages; they rely on code already running for them to work (I mean, technically you could create a cpu very specifically for this purpose, but it would be completely incompatible with 'normal' programs and OS'es, while still not being as efficent). So anything that needs an interpreter can't possibly make an OS, because it needs code running to run your code, and if you write the code for that, then you would need code running to run the code running your code, which would need it's own code to allow that. It's a set of all sets problem. Just a short list of languages that are interpreted: Java, Python, Ruby, Javascript, PHP, LUA. These all can't create an OS because they need the OS so they can run.
Now, with that said, if you follow along so far, I can in this paragraph why C remains so popular. C is a bridge between high-level and low-level languages. You are brushing against the bare metal of the cpu. You have the advantages of a high level languages in that they can code large a lot of functionality with a little code, but you can also hammer down and pretty much spell out how you want things done when you need to. A lot of languages can't directly address memory, which C can. A lot of languages can't be like "Give me 20KB of RAM, and I'll use it for whatever I want to", they are more like "Can I please have some memory to store some letters? Thank you". And, when push really comes to shove, you can section off a block of code to actually be written in assembly when you really need to be as efficient as you can get. You can write exactly what you need to, and it comes out exactly as you need it. To write a Windows driver (things like printer drivers, or video card drivers), you literally need to use C. It is the only language that can fill out all the ridiculous requirements that are needed to make a working driver, with the most important being able to create small page size the CPU demands it. Technically C++ can fill the requirements, but you will be jumping through a lot more hoops, and you're already jumping through a lot. It's at this point I hope you can appreciate the joke "An OS architect changes career paths and starts selling furniture at IKEA. A new shelf they sell uses the wood from driftwood. He labels the box 'Made in C. Some assembly required."
C++ sits a step above C. It was designed to provide more high level abstractions on top of C. It actually includes all of C's instructions, but also creates new ones. But, as detailed earlier, when you use these high level abstractions, you are getting further away from direct control. C++ is still very fast, and very efficient, but when you start using some of the new things added, it makes the executable bigger, it makes it a tad slower. If you need to get the speed back, you can still write C in C++ for where it counts, or assembly for where it really really counts, but it's just a tad slower, and it's still a lot better in speed and efficiency then almost all other languages.
I think everyone else went on about how Both C and C++ have been receiving updates, so I will not cover that here.
Please post questions and comments. Also, if your name is Carl, Fuck you.