tags:

views:

2005

answers:

18

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?

+31  A: 

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

Please see further reasoning in this answer.

Brian R. Bondy
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
@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
@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
@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
@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
@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
@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
+6  A: 

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.

John T
+16  A: 

C is the basis of a lot of wonderful tools we all use. It's quite simple and its definite guide - white bible - is a must-read. As for C++, I agree with Linus - "C++ is a horrible language." - so there's no need to learn it, before or after you learn C.

daonb
Nice, I really like C, I do. But what I like about C++ is not having to recreate link lists over and over again or hash table implementations etc etc over and over again.
apphacker
Linus is a troll. Disliking C++ is your prerogative but you should find someone better to represent that view.
Dan Olson
Someone better, who better than the developer of Linux! I don't agree with Linus because I love C++ but who are you to insult Linus's intelligence. Write your own OS before you dog this guy!
Lucas McCoy
Linus didn't write an OS, he collaborated with others on a kernel. The OS you are referring to is GNU Linux.
apphacker
Thanks for that link, cwheels. Hadn't seen it, and I'm glad Linus said it. C++ has to be one of the worst languages ever invented.
Craig S
I didn't insult his intelligence, I said he was a troll. That is less of a comment on what he says than on how he says it. Writing an OS doesn't make you immune to criticism when you spout stupid stuff on a public forum.
Dan Olson
Linus certainly didn't write an OS. And even if he'd invented spaceflight, that doesn't qualify him to judge a language he hasn't learned.
jalf
C++ has plenty of flaws, no doubt about that. But so does C. Luckily, they also each have their own strengths. And while I'd probably prefer C over C++ for writing a kernel, that hardly makes C a superior language. C is a portable assembler. And these days, most of us expect more from a language.
jalf
Yes. We expect sane object model which supports reflection and introspection.
Pete Kirkham
@Pete Kirkham: That's an amusing attack for two reasons: it's meaningless to C, and will never be considered as part of C++.
Matt Joiner
@Matt the point being that if you want what C doesn't offer, C++ is often not the place to look for it.
Pete Kirkham
@Pete Kirkham: I wasn't sure if you were being cynical, but I see now that you were. Nice comment ;)
Matt Joiner
+1  A: 

No there is no compulsion but i think C++ is easier

Mobin
+3  A: 

No.

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

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

Daniel Daranas
+19  A: 

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
+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. Bondy
+8  A: 

C is a must know language for software engineering. C++, Java, python, are not. You should learn it first without a doubt.

dwelch
Amen. This is spot on from a practical point of view.
Matt Joiner
I disagree - my C# software engineers do not need to know C to be effective.
JBRWilkinson
A: 

Here is an alternate phrasing of your questions, "should I learn VB6 before I learn VB.NET?".

Chris
There is a huge difference: While VB.NET is a break with the past, C++ is actually a step forward from C. In fact, being as compatible with C as possible was one of the top priorities in the design of C++.
Eduardo León
I was simply implying that VB6 wasn't fully object orientated and VB.NET is.
Chris
No, just learn C, then C#, then VB.NET :)
Daniel Daranas
Just don't learn VB.NET at all. VB6 for life.
Matt Joiner
+1  A: 

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
A: 

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.

krisku
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 T
+2  A: 

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
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
In many high performance computing scenarios, those few hundred clock cycles do indeed matter.
Scottie T
There's a reason academics generally prefer C, especially those of the high performance, scientific kind.
Matt Joiner
+7  A: 

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
+10  A: 

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
+7  A: 

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
+7  A: 

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
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
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
+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
@hillu: Agreed. The sooner C++ and C go on their separate paths, the better.
Matt Joiner
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
A: 

I think as I'm still 'roughly' new to programming I'm going to learn C first - I get the impression that this will help me get a better grasp over the 'concepts' of C, which I can expand upon when I come to learn C++.

Ross
Just be sure not to use those 'concepts of C' when you start programming C++.
Paul
@Paul: I think after some good time spent in C land, Ross will see the need for, and apply appropriately the abstractions C++ allows for. Better this way than be told by the booming voices of C++ lords that thou shalt use shared pointers and follow all kinds of necessary "good practises", to avoid problems that one doesn't even understand.
Matt Joiner
+3  A: 

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
It seems that many comp sci courses these days use Java, which exacerbates the problem you are talking about.
Dima
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
+9  A: 

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
I can't believe you didn't get any upvotes for this yet, here's one. I particularly like your last paragraph.
Wayne Koorts
@WayneThank you.
Dima