Ask HN: C vs C++

8 points by rumpelstiltskin ↗ HN
As a fledgling programmer, I need some advice. I made a list of languages to learn by next year and on the top of the list is C/C++ (figured that's a good place to start). However, a buddy of mine, an experienced programmer, suggested I drop C++ and just move on to JAVA after C. When asked why, he said 'C++ is way too complicated and error-ridden to serve any real purpose. C is more than enough'

Is this true? Should I forget C++ and move on to another language after C?

22 comments

[ 3.1 ms ] story [ 52.2 ms ] thread
It really depends on what do you want to do. If you are going to write stuffs on Windows OS for financial companies, you need to learn C++ to read existing codes.

If you want to build new system with Linux/BSDs. Then learning C is enough for you to do all useful things. Then you learn Java just as your friend says.

C++ is really complicated if you use features beyond basic templates and classes. And in reality, it lacks of standard Application Binary Interface for using binary libraries as C does. And different compilers may interpreter some spec in C++ differently.

C and Java is much simpler and uniform.

Well, Java has his problems too. I'd suggest learning C and then switching to a real object language (for instance io - http://www.iolanguage.com/ )

Do you want to learn those languages for your career or just for yourself ?

This is the third or fourth post with the same question in the last month or so.

At the risk of being grumpy, I'd make a longer reply, but I already blogged on this just this morning.

http://www.whattofix.com/blog/archives/2009/05/tired-of-erla...

So go with C++. It doesn't have as many letters as Java.

On the "grumpy" track: http://news.ycombinator.com/item?id=619739 (now get off my lawn.)
Dang kids!

I like pontificating as much as the next guy. But there's this pattern that keeps repeating: person asks question. People speculate on what they might have meant and offer answers. Person comes on and says no, that's not it at all. Then people spin off on that piece of information and speculated on what the situation is. Repeat and rinse.

Should I study C++ or Java? Well gee, what's important to you? Picking up complex concepts? Learning to work in large shops? Becoming an all-round nerd? Without context -- and a lot of it, including your life situation and priorities -- it's just an invitation to bullshit. Even then there's plenty of room for disagreement. It's like asking, What kind of music should I learn to like?

Is this true? Should I forget C++ and move on to another language after C?

Why do you need to move on to anything else? As your friend said... "C is more than enough".

C is more than enough to getting work done, but you need to look beyond to expand your mind and play with higher-levels of computing than pointer manipulation and bit flipping.
I haven't thought about pointer manipulation or bit flipping for years. Once you know a language you stop thinking about the grammar.
It's not the grammar that's in your face, ime, it's the minimal semantics. In C I'm more conscious of low-level details that I don't need to think about: planning where to put things, stack, heap, static area or even, gasp, registers is not a good way to implement higher level data structures and algorithms.

I prefer to focus on lists, graphs, queues, objects, events, continuations/control-semantics, and communication messages. I prefer to code in a high-level languages that allows me to get lower and lower as my needs for performance arise, and C is not it; in C I would need to build up my language to the higher semantic level my applications usually require, before I can even see it work. It puts the performance cart before the usefulness horse.

It depends on what you want to do. If you want to work on the linux kernel, you need to know C. If you want to work on Ableton Live, you need to know C++. If you want to work at Wells Fargo you need to know Java.
As far as your buddy goes it sounds like he isn't vary familiar with C++ or familiar with it in a environment in which it was poorly used. If anything C++ is much simpler to use than C on anything that is nontrivial.

Anyway whichever way you go know that idiomatic C and C++ are very different(see http://www.research.att.com/~bs/new_learning.pdf for an idea of the differences). If while learning C++ you notice that it looks like C know that your source of info is doing it wrong. In fact I find idiomatic C++ to be much less error prone than idiomatic Java.

There are things you can do with C++ that you simply cannot with java. I would recommend learning enough java to pick up OOP concepts, and maybe even some design patterns. Getting to that point should take no more than 6 months (if you work with it daily).
your buddy sounds misinformed.

however, your best course of action depends on what you're trying to accomplish. he may have produced a good suggestion despite the reasoning being flawed or based on bad information.

C and C++ are two languages that share a base syntax. They are used in different problem domains. To randomly pick one for you to learn now is not a question we can answer. Your friend's reasoning isn't sound.
I have to agree with this, C++ isn't error ridden, maybe you friend has run in to some bad C++ code. (If this is true, for example Adobe Products should be failing miserably, their mostly C++).

I would say learn both, once you learn one it's not far off the learn the other. Knowing straight C ways including manual memory management, and learning C++ along with STL/Boost is a powerful combination.

C has a huge number of libraries already out there to do everything and anything. C++ has STL/boost which makes life very easy by giving your a lot of high level algorithms at your fingertips. Now wrap your C libraries in classes and use them in mix with STL/Boost. Now that's a powerful combination, best of both worlds.

In order to program productively in C++ without too many segfaults, you need a deep understanding of what's going on, why the wrong one of your three two-argument copy-constructors is being called, when the invisible initialization of superclass vtables takes place, which spurious error symptoms indicate delete[]d non-arrays and so on. It takes quite some time to learn all this. Other languages like Java will also require a mental model of the execution environment, but in most languages that model is smaller and misunderstandings are easier to find.

In short: learn C++ if you expect to program in C++, but prepare to spend a lot of time before you get the good "I'm sitting on top of a seven-wheel motorcycle at 200mph, wheeee!" feeling C++ can give you.

Don't forget C++. Even if you stick to other languages you would probably still want to be able to read C++. There a tons of great stuff written in C++ out there already.
C is a small, beautiful, elegant language that's fine tuned to systems programming and high-performance programming, with a surprising ability to be used on higher levels with enough skill. As a programmer and computer scientist, C will teach you a good abstract model of how a program is executed on the low level (memory management, stack, heap, and so forth), especially if you learn some assembly along with it, and learn how to make C and assembly work together. It is not the best applications language and there are proportionally fewer opportunities to write and read C code than there once was, but it's necessary if you want to be a great programmer.

C++ is a big, clunky, difficult, ugly language. Understanding C++ is an accomplishment, but not because it involves any fundamental learning beyond the peculiarities of that language. As a programmer and computer scientist, C++ will teach you how to design a clunky mess of a programming language. But it is widely used, for good reasons. It's the right blend of performance and abstraction for many applications. It's a good language--maybe the best--for learning certain data structures, just because it combines pointers (data structures are mostly built out of pointers) with encapsulation (which helps reinforce the mental model that, for instance, rotation subroutines are part of what makes an AVL tree an AVL tree). And as an application language, it's very much usable. So it is worthwhile, but probably not a high a priority as C (or, indeed, other languages that can teach you more than simply how to use that particular language).

Calling C small, beautiful and elegant is quite a statement, considering what larger-than-academic projects end up looking like after having been macrofied to the point where only the person who wrote it can make sense of the code.

The bottom line is that they are both rather ugly but practical languages. C++ offers more abstraction means, but is more complex. But for larger projects, I'd rather have that then GLib style hacked up C.

That's vaguely what I meant when I said that C isn't a great applications language but C++ is a passable one. Different languages are well suited for different tasks, and if you use the wrong language for the task you'll always end up with a mess. So sometimes C++ is more suitable than C. And sometimes some other language entirely is more suitable. But the language design of C--compared to C++--is a masterpiece of clean and simple design.
It depends on what your goal is.

If you want a well paid job in business area, then C# and/or Java.

If you want to broaden your CS knowledge, then Lisp, Smalltalk, Eiffel.

If you want to work on shrink-wrap software then C++.

It's worth pointing out that Bjarne Stroustrup, creator of C++, is critical of it.