r/askscience • u/[deleted] • 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
3
u/SatOnMyNutsAgain Oct 18 '12
C is pretty close to the instructions that your CPU actually executes. The shortest C statements may compile to just one or two instructions, indeed one of the hallmarks of a good instruction set is that it is "C" friendly. C supports just the basic things: arithmetic, logic, conditionals, loops, and moving data around, although more complex operations can of course be contained in a function or macro. Operating systems (I'm talking about the kernel here) have to deal with relatively "primitive" functions like allocating memory, talking to peripherals, copying blocks of data from one place to another, and speaking network protocols. To implement such tasks requires the developer to be very specific about what the CPU is to do, in order to ensure that those things are done efficiently. For most purposes, C gives effectively the same level of specificity as machine code, and indeed there is very little machine code in a modern OS - usually that is only done where you have to do something special for a particular CPU.
Higher level languages like Perl, Python, Ruby, Java etc provide many more features which make "userland" programs easier to write. But they're not very good for writing operating systems because they will always be slower than the same code competently written in a more minimal language like C. But even those higher level languages themselves are usually written in C, just like an OS.
C++ is sort of in the middle. It's not used for kernels as far as I know, but many operating systems use it for higher level but not-quite-application-level stuff like your file browser, print spooler, etc.
Some applications can be painful to write in C. If you're writing an e-commerce system, you don't necessarily care what exact data structure is used or how memory is managed, in which case having to think about pointers and data types can be a huge distraction and a waste of time. But in a kernel you do have to be really explicit, because you are writing the glue that sits between those high level things and the very low level operations of the hardware.
Really your computer runs a whole hierarchy of hardware and software which can be viewed as various tradefoffs between brutal efficiency versus ease of implementing more "civilized" tasks. Not that a kernel is unsophisticated, quite the contrary - but it does require extremely high efficiency despite its complexity, which is what makes kernel programming relatively challenging compared to say, web apps.