I visited a university CS department open day today and in the labs tour we sat down to play with a couple of final-year projects from undergraduate students. One was particularly good - a sort of FPS asteroids game. I decided to take a peek in the src directory to find it was done in C++ (most of the other projects were Java 3D apps).

I haven't done any C before but I have looked through some C code before. From what I saw in the .cpp code in this game it didn't look very different.

I'm interested in learning either C or C++ but will probably learn the other later on. Is there any advantage to me learning one before the other and if so, which one?

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
If this question can be reworded to fit the rules in the help center, please edit the question.

17 Answers
17

They are different languages. It is a common misconception that C++ is in some way dependent on C and not a fully specified language on its own.

Just because C++ shares a lot of the same syntax and a lot of the same semantics, does not mean you need to learn C first.

If you learn C++ you will eventually learn most of C with some differences between the languages that you will learn over time. In fact its a very hard thing to write proper C++ because intermediate C++ programmers tend to write C/C++ instead of proper C++. That is true whether or not you started with C or started with C++.

If you know C first, then that is good plus to learning C++. You will start with knowing a chunk of the language. If you do not know C first then there is no point focusing on a different language. There are plenty of good books and tutorials available that start you from knowing nothing and will cover anything you would learn from C which applies to C++ as well.

I couldn't disagree more. Learning C++ first makes for a very difficult experience "going back" to C. C is a fundamental, basics-only systems language. C++ is a whole new ballgame, approaches to any problem in either language will differ wildly. I was fortunate enough to tinker with C before leaping into a C++ job. Subsequently I'm able to mentally "boil off" the syntactical sugar that C++ provides, and can approach problems with either language. It's very easy for me to identify C as the "does stuff" part of the language pair. Learning C++ first will lead to confused and misled programmers.
–
Matt JoinerJun 23 '10 at 5:45

11

@Matt: I learnt C++ first and I am not a misled programmer. So your claim is not true, proof by counter example. And yes I do have a gold badge in C as well. The truth of the matter is that they are 2 distinct and different languages. You don't need one language as a dependency to learn the other. That is just a silly misconception.
–
Brian R. BondyJun 23 '10 at 5:48

5

@Matt: Also from my linked post: You should learn C++ first, not because learning C first will hurt you, not because you will have to unlearn anything (you won't), but because there is no benefit in learning C first. You will eventually learn just about everything about C anyway because it is more or less contained in C++.
–
Brian R. BondyJun 23 '10 at 6:11

6

@Matt: I'm sorry you still do not understand. Draw a Venn diagram of C and C++ and look at the parts that intersect. C++ is composed of the whole C++ circle that you draw, not only the parts that aren't intersecting. If you happen to know C first, good for you, you can learn C++ faster. If you do not happen to know C first, there is no reason to to learn it first. Do you assume that every book on C++ will not cover pointers and memory management, and string literals, etc.? I'm going to stop discussing now but feel free to leave your closing comments.
–
Brian R. BondyJun 23 '10 at 13:27

6

@Matt: how does C lead to a better understanding of C++? Usually, C programmers who learn C++ end up writing ugly, error-prone "C with classes" style code. I'm not saying this always happens, but it is the most natural progression when starting with C, because C is much closer to that horrible style of C++ than it is to "modern" C++.
–
jalfJan 12 '11 at 13:09

I love this question - it's like asking "what should I learn first, snowboarding or skiing"?
I think it depends if you want to snowboard or to ski. If you want to do both, you have to learn both.

In both sports, you slide down a hill on snow using devices that are sufficiently similar to provoke this question. However, they are also sufficiently different so that learning one does not help you much with the other. Same thing with C and C++. While they appear to be languages sufficiently similar in syntax, the mind set that you need for writing OO code vs procedural code is sufficiently different so that you pretty much have to start from the beginning, whatever language you learn second.

+1 And then there are people as commented in my answer who will say that you can't possibly learn snowboarding without knowing skiing :( Because only in skiing can you learn to go down a hill?!
–
Brian R. BondyJun 23 '10 at 14:24

I learned C first, and I took a course in data structures which used C, before I learned C++. This has worked well for me. A data structures course in C gave me a solid understanding of pointers and memory management. It also made obvious the benefits of the object oriented paradigm, once I had learned what it was.

On the flip side, by learning C first, I have developed some habits that initially caused me to write bad C++ code, such as excessive use of pointers (when C++ references would do) and the preprocessor.

C++ is really a very complex language with lots of features. It is not really a superset of C, though. Rather there is a subset of C++ consisting of the basic procedural programming constructs (loops, ifs, and functions), which is very similar to C. In your case, I would start with that, and then work my way up to more advanced concepts like classes and templates.

The most important thing, IMHO, is to be exposed to different programming paradigms, like procedural, object-oriented, functional, and logical, early on, before your brain freezes into one way of looking at the world. Incidentally, I would also strongly recommend that you learn a functional programming language, like Scheme. It would really expand your horizons.

I think you should learn C first, because I learned C first. C gave me a good grasp of the syntax and gotchas with things like pointers, all of which flow into C++.

I think C++ makes it easy to wrap up all those gotchas (need an array that won't overflow when you use the [] operator and a dodgy index? Sure, make an array class that does bounds checking) but you need to know what they are and get bitten by them before you understand why things are done in certain ways.

When all is said and done, the way C++ is usually taught is "C++ is C with objects, here's the C stuff and here's how all this OO stuff works", so you're likely to learn basic C before any real C++ if you follow most texts anyway.

Learning C forces you to think harder about some issues such as explicit and implicit memory management or storage sizes of basic data types at the time you write your code.

Once you have reached a point where you feel comfortable around C's features and misfeatures, you will probably have less trouble learning and writing in C++.

It is entirely possible that the C++ code you have seen did not look much different from standard C, but that may well be because it was not object oriented and did not use exceptions, object-orientation, templates or other advanced features.

If you decide to learn both (and as other people have mentioned, there's no explicit need to learn both), learn C first. Going from C to C++ feels like a natural progression; going the other way feels like deliberately tying one hand behind your back. :-)

I'm going to disagree with the majority here. I think you should learn C before learning C++. It's definitely not necessary, but I think it makes learning C++ a lot easier. C is at the heart of C++. Anything you learn about C is applicable to C++, but C is a lot smaller and easier to learn.

Pick up K&R and read through that. It is short and will give you a sufficient sense of the language. Once you have the basics of pointers and function calls down, you can move on to C++ a little easier.

Wrong. Pretty much everything C teaches you is a bad practice in C++. C is not at the heart of C++, it just happens to share a lot of syntax. Please, the world has more than enough "C with classes" programmers already. Learn either C or C++. But don't pretend that C is a C++ lite.
–
jalfMar 1 '09 at 3:00

9

I stand by my words. C++ is built on top of C and thus C is at its heart. The idioms used in C++ are much different than those used in C, but the syntax is the same. One can learn the right way to program C++ starting with C. Look at C++ Primer. It basically does that.
–
Steve RoweMar 1 '09 at 4:08

6

+1. C is a smaller language, thus easier to learn. The attempt at being backwards compatible with C is one source of the major problems with C++, another being the template system that's too complicated for 90% of C++ programmers to use.
–
hilluMar 1 '09 at 21:33

1

An understanding of variables, types [including pointers and qualifications], scope, functions, the preprocessor, and linkage is essential to both C and C++. I would say learning these concepts with C will be easier so that you don't have to also deal with knowing classes, another meaning of static, templates (and the ridiculously intimidating template errors that compilers emit) etc.
–
dreamlaxJun 23 '10 at 7:00

1

You have a point. Actually, it may be good to learn C so that you can then move on to C++ with a better knowledge of its roots.
–
Daniel DaranasNov 10 '14 at 15:53

In the process of learning C++ you will learn most of C as well. But keep in mind a lot of C++ code is not valid C. C++ was designed to be compatible with C code, so i'd say learn C++ first. Brian wrote a great answer regarding this.

Having observed people, who have learned Java first, struggle with the concepts of pointers and memory management in C++, I'd say that learning C first is a good idea, in order to grasp these two concepts, isolated from the complexities of other C++ features.

In my opinion its to avoid all the "crowding" of the subject matter the obligation to require OOP carries.

I think that procedural programming is the most natural way to first learn programming. I think that's true because at the end of the day its what you have: lines of code executing one after the other.

Many texts today are pushing an "objects first" approach and start talking about cars and gearshifts before they introduce arrays.

It seems that many comp sci courses these days use Java, which exacerbates the problem you are talking about.
–
DimaJul 13 '09 at 21:31

2

You make a very good point. In addition to be far too complicated for a first language, C++ for the most part encourages hiding low level interaction with the operating system and memory that are critical to good computer science tuition. Arrays should be taught long before objects.
–
Matt JoinerJun 23 '10 at 5:55

1

On the other hand, if you are not bad at maths, functional programming might be more natural, because it looks and feels and works more familiar.
–
phresnelFeb 4 '13 at 14:12

Like the answers to many other questions in life, it depends. It depends on what your programming interests and goals are. If you want to program desktop applications, perhaps with a GUI, then C++ (and OOP) is probably a better way to go. If you're interested in hardware programming on something other than an x86 chipset, then C is often a better choice, usually for its speed. If you want to create a new media player or write a business app, I'd choose C++. If you want to do scientific simulations of galaxy collisions or fluid dynamics, behold the power of C.

In many high performance computing scenarios, those few hundred clock cycles do indeed matter.
–
Scottie TMar 2 '09 at 12:15

1

@ScottieT: Given that apart from the restrict specifier C has no other performance-related syntactic/semantic construct than C++ has, too, why exactly do you think C++ code, with its richer syntax and e.g. meta-programming should yield slower code? Do you even know expression templates?
–
phresnelFeb 4 '13 at 14:34

1

@phresnel: Relevance. C++ is an extremely complex language with features that often aren't worth the buy in. As a general rule, C gives the same performance opportunities as C++ at a significantly smaller cost in complexity.
–
Matt JoinerFeb 5 '13 at 0:36

1

@MattJoiner: Maintainability/Extensibility are also performance factors, though I realise scientific code is often one-shot. However, repeating the expression-templates-argument (as an example), I say that C++ has more optimization opportunities, (also because it is easier to write expression-template-code than intrinsic-code), with one exception (restrict, which however all major C++ compiler provide as an extension). And do you really think that, given the same code, there will be any difference in performance between C++ and C, given the same compiler? [...]
–
phresnelFeb 5 '13 at 8:59

1

@MattJoiner: [...], and when reading statements like "as much as I dislike C++" lets me build up the assumption you are slightly biased in this discussion. What exactly do you think makes C++ code perform more poorly? If C has the same performance-opportunities, what is the equivalent of expression templates, templates in general, compile-time computations on non-intrinsic datatypes, functors? And adding the cost/maintainability-factor: RAII, operator/function overloading, generic standard containers, standard threads, generic standard algorithms, references?
–
phresnelFeb 5 '13 at 9:08

i think c is a really nice programming language, it's compact and somewhat easy to learn.
but if you only want to learn c++ start with c++. but i suggest you to learn both. and if you want to do that; i think it's better to start with c. as said before: it's small and somewhat easy to learn. might be a nice step-up to a more complex programming language as c++. (since c provides you with some basics)

Some C++ books supposes that the reader have minimal knowledge on C, but whoever language you choice isn't wrong way, because lots of programming languages are partial based on C/C++ and for future learning other languages will be little bit easier.

That's an historical artifact because many of the first generation of C++ programmers were originally C programmers. Similarly, most of the early Java books assumed some level of C++ knowledge.
–
Scottie TFeb 28 '09 at 23:10