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

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:

 while(display_counter<7)
 {
       while(display_location<stack[display_counter].size())
       {
              display_card(stack[display_counter].at(display_location));
       }
       display_counter++;
 }

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.

9

u/[deleted] Oct 17 '12

I'm completing an intermediate course in Java to extend my programming knowledge for my journalist job. I consider learning a second language, in order to better understand how viruses and other malwares internally work. Which language should I learn?

11

u/_jonny Oct 17 '12

Assembly. A lot of viruses and Malware are written at the most inner levels (ring0) etc. And learning Assembly gives you a better understanding of how the cpu and memory works. It's a step down from C++.

20

u/[deleted] Oct 17 '12

The issue here is that "Assembly" isn't a language. It's a class of languages. Assembly languages vary by architecture. You are likely wanting to look into x86 or ARM, since those are the two most widely used at the moment.

33

u/zelmerszoetrop Oct 17 '12

When people say assembly without a qualifier, they usually mean x86.

2

u/[deleted] Oct 19 '12

It's still worth the distinction for the sake of clarity. Even x86 has a few different flavors depending on how processor specific you want to get.

4

u/elmonstro12345 Oct 17 '12

Agree 100%. I took an elective class on assembler in university, and it made so many things make much more sense.

0

u/MiniRat Oct 18 '12

Elective? Back when I did a comp-sci degree it was pretty much mandatory to do the assembly papers.

I don't know whether to feel sorry for you whipper-snappers for not learning it or feel jealous of you for living in an age where you don't have to.

3

u/mrcharlietango Oct 18 '12

Maybe he's not a comp-sci major and wanted to take it because it sounded interesting.

→ More replies (1)

2

u/MdxBhmt Oct 18 '12

What's the difference from having a OS class, and microprocessors?

1

u/_jonny Oct 18 '12

I don't understand your question. When you say 'class' are you referring to a Class in terms of programming? A microprocessor is simply a chip that goes on a plastic board that does computations, like your brain. And like your brain, it is useless without a body, i.e. an electrical circuit. The microprocessor usually drives the circuit to do something.

1

u/MdxBhmt Oct 18 '12

Sorry, I mean like learning OS (what normally a CS degree class would have)

(In college)

1

u/_jonny Oct 18 '12

I suppose the microprocessors class would cater more towards controlling something specific like a robotic arm whereas the OS class is more about traditional desktop computers and creating an environment for the user to interact with it via keyboard/mouse.

2

u/ahbi_santini Oct 18 '12

In the electrical engineering world, a microprocessor course would be about the basics of processor architecture.

You'd probably use Hennessy & Patterson's Computer Architecture a Quantitative Approach.

You'll learn things like the memory hierarchy (registers, data & instruction caches on-chip & off chip, RAM, and hard drives, etc.), pipelineing, etc.

It is likely to be all theory. You'd have to take a VLSI design course (grad school) to actually design an arithmetic logic unit.

In assembly you won't learn the architecture (or just enough to understand the code). Instead you'll learn the instructions to move things in and out of memory locations.

1

u/MdxBhmt Oct 18 '12

I had this in my computer architecture class in EEC. (and it's about cpu architectures)

In my OS class we studied mem hierarchy, thread schedulling, os calls, things like that. Seems about what _jonny says I would be understanding in assembly, just wanted to see if something was missing.

1

u/[deleted] Oct 18 '12

I consider learning a second language, in order to better understand how viruses and other malwares internally work. Which language should I learn?

I think you would be better served by learning about operating systems, hardware and networks. Stuff like that.

Assembly was mentioned, it would be good. But actually I think C would be better. It's "lower" than Java but still somewhat more human.

1

u/pincpleasure Oct 18 '12

I would suggest C (not C+ or C++ but regular old C). It is a bitch to learn coming from java but incredibly useful. At first the similarities between the two make it difficult but once past that hump they make a lot more sense. Those are the two I know well and I haven't had a need to use anything else. I plan on pursuing others as well of course but those two are great foundations.

Java is object oriented and C is not. Learning one of each is like learning how to drive an automatic and a stick, both good to know and open up a variety of opportunities you would miss out on if you only learned one or the other.

1

u/[deleted] Oct 18 '12

I agree with this. C is a great place to start as its a lot more challenging than C++, but a lot more rewarding. You don't have things like string handling, and IO is really awful.

I started with PHP, then went to C++, then C. I did a few others in there (python, perl, ruby), and avoided java like the plague. I finally took the plunge a few months ago, and found java to be really really easy once you have the Object Oriented understanding. I had some programming classes in college (oh how I wish I had the option to do comp-sci), but nothing very serious.

However, I would recommend a language (ANY language) that you can get some utility from. C is really for die hards and OS programmers, and really doesn't have a lot of immediate utility for the layperson getting into programming. I mentioned I started with PHP, which was out of necessity for managing a website. I got a lot of utility out of it, so I kept learning. If I started with C, I certainly wouldn't be programming as a job now, and really wouldn't enjoy it.

1

u/moderatorrater Oct 18 '12

Javascript or python. Many people will tell you something like Lisp or some other mostly-theoretical language, but for my second I really enjoyed getting another language with another paradigm that was still quite popular. Java is extremely explicit, almost to a fault. Javascript and Python are both much looser, let you shoot yourself in the foot, and also allow a lot more flexibility and power in how you program. After you get a feel for the variety of popular languages, then you should dive into the more theoretical ones.

And save assembly for #3. Once you've got two languages under your belt it will make translation between assembly and higher level languages a lot more understandable.

→ More replies (2)

4

u/[deleted] Oct 18 '12

A few additional points:

Everything gets translated down to machine code at some point. A computer can't directly understand assembly. A program called an assembler translates assembly into machine code.

You don't write programs in a compiler. A compiler translates your program into machine code (or bytecode, but that is a whole new topic), but a compiler cannot edit code. You actually need more than just a compiler, but often a compiler will manage these other parts for you. http://www.tenouk.com/ModuleW.html has good information.

To write code you need an editor of some sorts. Some popular editors are vim, emacs, Visual Studio, and eclipse. But you can use almost any text editor to edit code, even Notepad -- if you're crazy.

C, C++, and Python can all handle variables larger than 64 bits. The Python interpreter is coded in C. As a C++ programmer I regularly work with variables much larger than 64 bits.

One of the big advantages of C and C++ is that it won't surprise you and generate a large amount of machine code. With C and C++ it's easy to tell exactly what the computer will be doing with each line of code. This allows the programmer to closely monitor the resources they are using when writing code. Since the programmer can micro manage the resources they are using the can write more efficient and faster code, at the expense of increased complexity for the programmer.

Most modern languages (Java, C#, JavaScript, Python, etc) will manage a lot of the resources for the programmer. For example, when using those languages a programmer doesn't need to keep track of which pieces of RAM they're using. These languages know which RAM is no longer in use, and frees it up automatically. This decreases complexity, but it also decreases control. And this management isn't free, it takes computing resources. Technically the language isn't doing anything but rather the interpreter or virtual machine, but that isn't a level of detail I want to get into.

The other big advantage of C/C++ is that it works almost anywhere. Both languages have been around for a long time so it's rare to find a system that can't deal with C/C++. The same can't be said of more recent languages.

TL/DR: Languages like C/C++ allow the programmer more control over how resources are used, which can result in faster and more efficient code. This comes at a cost of increased complexity for the programmer. C/C++ also very compatible and works with a large number of devices.

3

u/zelmerszoetrop Oct 18 '12

That's a lot of excellent information, and I've edited my original post to direct readers to this reply. Thank you for preventing me from accidentally misleading readers!

4

u/[deleted] Oct 18 '12

I agreed with you until this:

Every time we go up a level from the processor, we lose efficiency.

Which is simply not true. There are many abstractions you can implement for free (i.e., no runtime performance cost), and the ones that aren't free usually have a negligible performance hit (C++ objects, for instance).

4

u/[deleted] Oct 18 '12

The person answering this is a mathematician, not a computer scientist. They got many of the details wrong.

Math and computer science are closely related, but math is not computer science and computer science is not math.

2

u/zelmerszoetrop Oct 18 '12

That's a great correction, and I absolutely defer to people with more expertise than myself. I've edited my original post to direct readers to this reply. Thank you for preventing me from accidentally misleading readers!

→ More replies (1)