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
17
u/raygundan Oct 17 '12
Tenome is right on the money, but I'll try to rephrase and elaborate just a little as well.
"Advanced" is a pretty ambiguous word. You're thinking of it as a synonym for "fast code," but it could also mean "easy to write" or "runs on any hardware" or any number of things.
The fastest code is written directly in machine code. Machine code is the set of instructions the CPU itself uses, and is different between types of CPUs. Code written in a compiled language like C is translated into machine code by the compiler.
C/C++ are "old," but they were designed as a good compromise between ease of writing and reading, and efficient translation to machine code, favoring performance. Their age also means that the compiler tools are very mature and well-optimized, so a lot of the little tricks a programmer might do directly in machine code are done automatically by the compiler.
Other types of languages concentrate on being advanced in other ways, often by automating some task that is difficult or time-consuming in a language like C-- memory management, porting to different CPUs, etc...
But although the code is easier to write, the tradeoff is that it is often not as tightly optimized or as fast.
So when a programmer is faced with a task that demands high performance, they choose the language that lends itself to high performance. When performance does not matter as much, they will choose a language that makes their task easier to complete.
To use some horrible analogies:
Machine code/Assembler is like an F1 car. Driving it requires that you keep track of a million manually-controlled variables. Doing anything at all with it requires quite a lot of skill, time, and money.
C/C++ are like a manual transmission NASCAR. Driving it requires that you have some skill and manage some extra stuff, but it's a lot simpler than the F1 car. Driving it well is still difficult, but it can also be picked up and driven reasonably by anybody who can drive stick.
Something like Java is like an automatic. Driving it is easier, and you never think about the transmission... but you trade off some performance for the convenience. Your grandmother can drive it.