135

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?

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
Ross
  • 46,186
  • 39
  • 120
  • 173
  • 5
    Before descending to the below answers (IMO they go in circles), allow me to offer some advice: **TRUE**: C++ *does* retain all of the features and attributes applied to C, thus implicitly '*obsoleting*' C (The point most of answers below try to make). **HOWEVER**: C is *not*, *not*, *NOT* DEAD. People seem to grasp onto the '*there's no need to learn both*' trope for dear life, but in reality this has no basis. To borrow syntax originating from C, *C++ != C*. Knowing C++ will NOT make you a qualified C developer, and likewise, knowing C will NOT make you a qualified C++ developer. – Super Cat Nov 28 '15 at 05:11
  • 9
    **THAT SAID**: There's *NO harm* in taking on C before delving into C++ - The more you know, the better you are - But on the contrary, there's *no need to learn C if you're ultimate goal is to learn C++*. IF you want to learn C++, *go learn C++*, and if you want t learn C, *go learn C*. Both have pros and cons (The simplicity of C and the -ultra in depth- features of C++ are both curses and blessings in their own ways), and both are still used today (C for embedded systems, Unity, PHP, etc - C++ also for Unity, Unreal, etc). – Super Cat Nov 28 '15 at 05:30
  • 7
    **POINT:** *Want to learn C++?* Then skip C. You don't need it. *Want to learn C?* Then good, go learn C. If you *want* to learn both, then go for C first. They both have their places in the world, so don't think that either is sufficient for the other. – Super Cat Nov 28 '15 at 05:36

14 Answers14

141

There is no need to learn C before learning C++.

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++.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.

Please see further reasoning in this answer.

Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • 42
    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 Joiner Jun 23 '10 at 05:45
  • 27
    @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. Bondy Jun 23 '10 at 05:48
  • 8
    @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. Bondy Jun 23 '10 at 06:11
  • 6
    @Brian: But there _is_ benefit to learning C first. C is in wider use, simpler, quicker to learn, more portable, and leads to a better understanding of C++. I don't doubt that a C++ programmer can learn C, it's ridiculous to suggest otherwise. However I do prefer that more programmers know C, than a few programmers only partly knowing C++. The question about which comes first is then quite simple: C opens more doors, is quicker to learn, and is necessary to understand C++ anyway, you might as well begin with mastering this fundamental language, rather than fearing the dark corners of C++. – Matt Joiner Jun 23 '10 at 06:25
  • 2
    @Brian: I might add that after looking at your profile, that for development constrained primarly to Windows such as yours, and probably Visual Studio, the choice of C is potentially a poor one. Visual Studio is an awful C compiler, so much so that the learning of C on Windows is better done by use of a subset of C++. However this doesn't affect my opinion regarding the general use of C on all platforms. – Matt Joiner Jun 23 '10 at 06:31
  • 12
    @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. Bondy Jun 23 '10 at 13:27
  • 3
    @Brian: Perhaps there's some disagreement regarding "need" and "should" here. My argument is that one *should* learn C before C++, for all the merits listed above. C is worthy of learning on its own, and a good stepping stone to C++, but not a *necessary* one as you point out. The OP uses the wording "should", and "advantages". In closing I'm saying that one *should* learn C first, because of the immense advantages to knowing C as a separate language that I've listed in previous comments. – Matt Joiner Jun 24 '10 at 02:50
  • 11
    @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++. – jalf Jan 12 '11 at 13:09
  • 3
    @jalf: I'm not sure that's relevant. The OP asks if there is an advantage to knowing C or C++ before the other. I believe that one should know C regardless, and that it's better to learn C completely first (a matter of weeks) for its undeniable value (see above comments), rather than spend years learning C++ (a relative niche language now that C# and Java are on the rampage), and not understand C as a consequence. Consider now that using C++0x effectively requires relearning a large part of C++ and adjusting one's style completely. Will you now brand C++03 coding as horrible? – Matt Joiner Jan 13 '11 at 04:54
  • 2
    @MattJoiner: If you "boil off" the C++ from C++, you're doing it wrong. I agree that in order to write high-performance programs it is often important to know how the computer is working underneath your abstractions, however, you should design first for what your program does, not how it does it. The point of a good C++ abstraction is that you do not boil it off. The reason C++ is such a good language is that it lets you abstract away certain irrelevant details while still maintaining performance as good as or better than C. – David Stone Aug 12 '12 at 23:17
  • 3
    'I'm able to mentally "boil off" the syntactical sugar that C++ provides' -- No one who believes that is a competent C++ programmer. – Jim Balter Oct 06 '12 at 23:35
  • 5
    i would understand suggesting that no need to learn C before learning Java, because of the latter's memory handling. however, not learning C before learning C++ is inexcusable because in a way you are not learning C++ if you ignore C (because C is a subset), and you are losing a great opportunity to understand WHY a certain C++ construct is useful by finding out how awkward it is to do those things in C. – necromancer Jul 14 '14 at 21:13
  • Seeing your answer and some other answers below make me finally decide to pour my energy into learning C++ first as my second language other than VBA. Thanks for the answer and (++1) from me. :) – Anastasiya-Romanova 秀 Aug 04 '16 at 03:07
89

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.

cdonner
  • 37,019
  • 22
  • 105
  • 153
72

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.

Dima
  • 38,860
  • 14
  • 75
  • 115
21

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. :-)

Head Geek
  • 38,128
  • 22
  • 77
  • 87
14

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.

Adam Hawes
  • 5,439
  • 1
  • 23
  • 30
  • I disagree. Any into to C++ programming book worth its salt will cover at least some of the "gotchas", such as how arrays work (and how to prevent overflow) –  May 27 '16 at 21:25
13

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.

Steve Rowe
  • 19,411
  • 9
  • 51
  • 82
  • 16
    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. – jalf Mar 01 '09 at 03:00
  • 13
    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 Rowe Mar 01 '09 at 04:08
  • 7
    +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. – hillu Mar 01 '09 at 21:33
  • @hillu: Agreed. The sooner C++ and C go on their separate paths, the better. – Matt Joiner Jun 23 '10 at 05:52
  • 2
    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. – dreamlax Jun 23 '10 at 07: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 Daranas Nov 10 '14 at 15:53
10

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.

Community
  • 1
  • 1
John T
  • 23,735
  • 11
  • 56
  • 82
9

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.

hillu
  • 9,423
  • 4
  • 26
  • 30
8

I think learning C first is a good idea.

There's a reason comp sci courses still use C.

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.

bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • 4
    It seems that many comp sci courses these days use Java, which exacerbates the problem you are talking about. – Dima Jul 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 Joiner Jun 23 '10 at 05: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. – Sebastian Mach Feb 04 '13 at 14:12
  • 2
    _"There's a reason comp sci courses still use C."_ Yeah, because most comp sci courses are poorly taught. – Lightness Races in Orbit Nov 07 '14 at 11:24
8

No.

It's generally more useful to learn C++ because it's closer to the most modern OO-based languages, like Eiffel or C#.

If your goal is to learn C++, learn modern, standard C++ in the first place. Leave the mallocs aside.


But Steve Rowe has a point...

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
8

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.

Scottie T
  • 11,729
  • 10
  • 45
  • 59
  • Modern C++ is not too far removed from C when it comes to efficiency. Unless a few hundreds of CPU cycles here and there are going to make a difference I'd choose C/C++ based on the task, not performance. – Adam Hawes Mar 01 '09 at 01:23
  • 4
    In many high performance computing scenarios, those few hundred clock cycles do indeed matter. – Scottie T Mar 02 '09 at 12:15
  • There's a reason academics generally prefer C, especially those of the high performance, scientific kind. – Matt Joiner Jun 23 '10 at 05:56
  • @MattJoiner: Ignorance or Portability? – Sebastian Mach Feb 04 '13 at 14:31
  • 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? – Sebastian Mach Feb 04 '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 Joiner Feb 05 '13 at 00: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? [...] – Sebastian Mach Feb 05 '13 at 08: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? – Sebastian Mach Feb 05 '13 at 09:08
7

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.

TrayMan
  • 7,180
  • 3
  • 24
  • 33
6

My two cents:

I suggest to learn C first, because :

  • it is a fundamental language - a lot of languages descended from C
  • more platforms supports C compiler than C++,- be it embedded systems, GPU chips, etc.
  • according to TIOBE index C is still about 2 times more popular than C++.
Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70
3

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)

good luck.

Marnix v. R.
  • 1,638
  • 4
  • 22
  • 33