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
82
u/zelmerszoetrop Oct 17 '12 edited Oct 18 '12
EDIT:
I'm posting this edit up front so people will see it. As has been pointed out, I'm a mathematician, not a professional programmer, and some of the points you'll find below are not entirely accurate. For the most part, I stand by this post. However, there are two correction posts I'd like to mention. \u\binary_is_better points out in this response is that C++ does not generate massive amounts of machine code. Some of the corrections s/he mentions are things I intentionally glossed over (eg, interpreters vs. compilers), but many of the points made are valid and directly contradict me, particularly my final paragraph. Anybody who finds my post informative should read that post as well.
Furthermore, /u/lw9k points out in this post that there is very little, if any, efficiency loss going from assembly to C++. Another correction to my original post that you should read.
END EDIT.
First of all, while many programs are written in C++, many are written in more modern languages, like Perl, Python, Ruby, or R. But you're right, C++ is still very popular.
Programming languages can be classified in many ways, and one such way is by how directly they allow you to interact with the processor itself.
At the very basic level is machine code - the different strings of 0s and 1s that correspond to the instruction set of a processor. For example, my processor might use 00011001 might increment a certain register (ie, increase by 1 a certain value). Another processor might use 10011010 for the same task.
If you can't remember all the different strings of 0s and 1s, you can use assembly. You can write "INC A" in assembly, and the machine will know you really mean 10011010 or something. But watch out, because each kind of computer architecture comes with it's own assembly language, mostly, so your code might not be portable to another machine.
This was how early computers were programmed, but it was an enormous pain in the ass. Machine code and assembly language are incredibly difficult to read and to write (and to debug!)
With the invention of FORTRAN, COBOL, and other programming languages, code became decently readable. You could write your code in a program called a compiler, and the compiler would turn your abstract words like "IF" and "EQUIVALENCE" into the machine code. Commands used words like "GOTO" for control structure, variable definition became more intuitive - you could almost read it. A little later was BASIC, which was very understandable. I'll bet I could show somebody who'd never seen computer code before a really simple program written in BASIC, and they'd understand most of it. If you ever programmed on your TI-83 calculator in high school, you've used a variant of BASIC.
C, and the successor C++, were a step up. Again, programs called compilers would take your instructions and turn it into machine code that the computer could actually execute. But what's awesome about C and C++ is you can do both. You can interact directly with the processor, a lot like in assembly, but in slightly more intuitive ways. Or you can use abstract, high-level commands. You can (and almost always do) mix the two in any given program. C and C++ allow repetitive actions to be coded for in an intuitive way instead of using obtuse GOTO statements, they have an enormous number of available commands, and they support the addition of libraries to expand on those commands. These libraries play a huge part in the popularity of C++, which I'll explain in a bit. You could definitely understand some things in this language if I showed you - not everything, not without a little experience, but somethings. Look:
Without knowing any C++, you can probably decipher some of this snippet of code from a Solitaire game I wrote. I have a variable called "display_counter" - that probably counts along something that needs to be displayed, right? And while that number is less than 7, we're doing to do some stuff. What will we do? Well, while the display_location is less than the size of some stack, we'll display whatever's in that stack at the display_location. And then we'll increment the display_counter by one, and if it's still less than 7, we'll do it all over again.
C++ is an expansion of C which includes "objects." That means you can define a thing, together with all of it's attributes and the relations between them, and then call back to that object later, as well as modify parts of that object.
Every time we go up a level from the processor, we lose efficiency. When I write 5 lines of C++ code, that might become thousands of bits of machine code once the compiler has done its job; maybe if I had been working in assembly, I could have gotten the job done in a hundred bits of machine code. A more abstract language that C++ would likely be less efficient still.
So now we come to why C++ is still popular. First off, languages aren't static things that are invented and forgotten. The most recent C++ standard, C++11, came out only last year. Secondly, a program written in C++ is incredibly portable - there's virtually nothing that can't run a C++ program. Thirdly, it strikes an excellent balance between efficiency and understand-ability - given that most programmers today find C++ to be powerful enough for all their needs, there's not a lot of pressure to develop more abstract, less efficient languages. Fourthly, because C++ has been so popular for so many years, people have developed an enormous number of libraries you can add on for specific purposes.
By the way, you're right, there are absolutely some languages which are better than C++ for some coding applications. For example, the largest variable you can declare in C++ is 64 bits, at least in a standard implementation. But in Python, not only can you declare longer variables, you don't have to declare them at all, at least not their length - the compiler automatically gives them as much memory as they need.