tags:

views:

864

answers:

18

Between C and C++, which is easier to learn? Which is more powerful? Does either do anything special that the other doesn't?

thanks!

+2  A: 
  1. C++ is easier to learn.
  2. Arguably, neither is more powerful. But C is slightly more low-level.
  3. C++ has classes, templates, and a bunch of other awesome stuff you'll eventually learn to hate.

People write books on this subject but since it looks like you're new, I kept it simple :)

David Titarenco
I wouldn't say C++ is easier to learn. In fact I'd say the opposite... which is why when people claim they are C++ programmers they are actually C programmers using some of C++.
Brian R. Bondy
What makes C++ so easy to use for beginners is the STL. And the STL **is** C++.
David Titarenco
It not even fair to say that C is more low-level, since anything you can do in C you can do in C++.
John Dibling
@David, if by "the STL is C++" you meant that the STL is the essential difference between C and C++, it certainly is not the only essential difference.
John Dibling
That's not what I meant. I meant that something like the STL can't be done with C and it's a huge part of the C++ standard. And a huge part why it's more newbie-friendly.
David Titarenco
@David: fair enough
John Dibling
@dmazzoni said it nicely, C++ easier at first but C is easier to master.
Brian R. Bondy
C++ is easier to learn? What planet are you from?
Alex B
@John: C++ can do the low-level stuff C can, but also high-level stuff C can't do. Insofar C is indeed more low-level than C++.
sbi
@Alex: Have you looked at _Accelerated C++_? It teaches C++ the way it ought to be taught. I have taught C++ this way many times, to students with Java experience and to programming novices, and it _does_ make learning C++ easier.
sbi
@sbi, Yes, and it's very very very deceptive, because all these people will walk out of the class confidently thinking they know C++, which you, of all people, should know isn't true :)
Alex B
@Alex: After 12 lectures and 24 lab sessions my students walked out knowing they had have a first look at C++ and were just far enough to have their application for novice status accepted for evaluation - despite the fact that I walked them all the way from "hello, world" to template-meta programming. And I notice you have avoided answering my question.
sbi
@sbi, sorry I omitted that, no, I did not. But, I was the one of the students walking out of a C++ course that covered advanced concepts like template meta-programming, thinking my C++ skill is "good enough". Boy I was wrong. To actually learn C++, you need to endure pain. :)
Alex B
@Alex: You should read the book. No, I'm not kidding. I read it after ten years of doing C++, after I just had started doing template-meta programming. I read it because I searched input and ideas for _teaching_ C++, but I'm very glad I did because it was also very good for my _programming_ in C++. And, no, I don't think it taught me anything really new (it's a 250p introduction, after all) - except that _it taught me to look at C++ very differently_, which made it the most important C++ lesson I ever had in >15 years of programming in C++.
sbi
Oh, and actually I always _told_ my students after the course that they are now fit to file their application for novice status. `:)` Not that they really needed it - I always had to do overtime for the lab sessions (because they needed so much assistance for their assignments) and many of them told me that I was demanding a lot more than they ever had to do in the previous semesters (while also giving them more).
sbi
+1  A: 

C++ makes it easier for a beginner to start doing useful things. However, it is by and large a superset of C. Also, many of the additional features are fairly complex. Thus, it's much easier to learn most of C than most of C++.

Matthew Flaschen
I have to strongly disagree with you (but I'm not going to -1 since it's a difference of opinion). I know many people that would strongly advise against learning C, altogether. C is ridiculously hard to learn for a newcomer (Fran Allen would agree :p) and very rarely do people write anything in C nowadays, anyway. Unless Fry wants to get into computer science (and isn't just learning how to program for fun), I wouldn't suggest even looking at C.
David Titarenco
@David, what part of my answer are you actually disagreeing with? I didn't say he should learn C (or C++) first. Plenty of useful code certainly *is* still written in C, though, from web servers to IM clients to kernels to programming languages. And what makes you think the only two choices are "computer science" and "fun"?
Matthew Flaschen
I'm disagreeing with "it's much easier to learn most of C then most of C++". I know many people much smarter than me that would also disagree with that. Memory management, just as an arbitrary example, is significantly more difficult in C. Suggesting a newbie programmer learn C is pretty obtuse in my opinion. I'm not going to start debating semantics. "Fun" or "computer science" are merely euphemisms.
David Titarenco
@David, learning most of C++ includes learning `std::malloc`, `new`, and "raw" pointers, along with other potentially tricky features, like macros. I did *not* tell him to learn C. I don't know why you keep saying that. I made it clear that C++ is easier when starting out, but harder to master (or approach mastering).
Matthew Flaschen
@David: I think that the point here is learning 100% of C++ is much harder than learning 100% of C, since 100% of C++ = 100% of C plus a whole lot more.
John Dibling
So Matthew, you think that a new/casual programmer plans on learning the **entire** language? I mean come on. The STL makes C++ so much newbie-friendly it's not even a fair comparison. But like I initially said, it's a difference of opinion and to each his own.
David Titarenco
@David: You seem to be debating Matthew's assertion that a newbie programmer should learn C. Only one problem: Matthew never made that assertion. As far as I can tell, Matthew never made any recommendation for one language over the other whatsoever. Why/what are you arguing?
John Dibling
@David, I think you're mischaracterizing what I wrote. I never said he could learn **all** of C or C++. However, presumably he wants to **eventually** do more than skim the surface.
Matthew Flaschen
@John, exactly. I tried to outline the trade-offs, taking into account where Fry may want to go in the future. He probably won't be able to master either, but he can definitely come closer with C.
Matthew Flaschen
Again, with the semantics: `most` vs `all`. Ignoring the fact that C isn't a subset of C++, I really don't think a novice programmer should try to learn C. That's all I'm saying :)
David Titarenco
@David: You might be grumpy. I suggest a break away from the computer. :)
John Dibling
I think this entire comment sequence followed from a simple typo: Matthew wrote "it's much easier to learn most of C then most of C++" when he probably meant to write "it's much easier to learn most of C *than* most of C++".
Alexandros Gezerlis
@Alex, guilty as charged. Good catch. However, you would think my first comment would have clarified that.
Matthew Flaschen
+9  A: 

C is much smaller language, so it will be easier to learn the entire language.

C++ has some capabilities that make it easier (encapsulation, richer user defined types, etc). However, C++ also has some features that are better avoided (multiple inheritance), so if done wrong learning it could be a disaster.

Also the C++ language is so huge that I doubt there are many that know very nook and cranny of the language.

Accelerated C++ is a great way to learn C++. It avoids the darker corners and still teaches C++, as opposed to C with classes.

caspin
What's wrong with multiple inheritance? Multiple inheritance is the foundation for interfaces in C++. I would agree that multiple *implementation* inheritance should be avoided most of the time, but multiple inheritance itself is fine.
Billy ONeal
@Billy, I should have qualified my comment. Multiple inheritance is best avoid until it is absolutely needed. multiply inheriting interfaces is one paradigm that *can* be done in C++ but is better left for Java or C# where it is better supported. In all my professional career I've only seen multiple inheritance done correctly with std::iostreams, boost::non_copyable, and boost::enable_shared_from_this. And the latter two are more of a mixin paradigm than a multiple interfaces paradigm.
caspin
-1: I don't see how it's "Better supported" in Java and C# given that C++ supports multiple implementation inheritance, while Java and C# don't. You can do everything Java and C# do with C++'s model, but not vice versa. Therefore I fail to see how one is "better supported".
Billy ONeal
@Billy Java and C# support the concept of an interface directly with the `interface` mechanism. In C++, you have to simulate it with purely abstract base classes. That's why it's "better supported" in Java and C#.
FredOverflow
@FredOverflow: What is the difference between an interface and an abstract base class? Nothing!
Billy ONeal
@Caspin: We use MI here, with no problems. There are potential problems (the diamond pattern, to mention the worst), but it can be used safely and productively. Just don't push it.
David Thornley
+24  A: 

C++ is a little bit easier to learn at first, because you can do more powerful things with less code. But C is easier to master.

C is far easier to master, because it's a very small language. Some of the concepts (like pointers, aliasing, bit manipulation) are subtle, but you can master the syntax in a few months and never encounter a line of C code that you can't understand. The one exception is preprocessor macros - overuse of macros can make seemingly innocent C code do bizarre things.

C++ is almost impossible to master - the language is incredibly complicated, and there are dozens of obscure details that can get you into trouble. It can be very hard to read unfamiliar C++ code. Most large C++ projects have "style guides" and rules that disallow or restrict certain C++ features, to keep the project from getting too difficult to work with.

Your last question is, does either do anything special that the other doesn't. C++ is almost a perfect superset of C. That means that every C program is either already a C++ program, or with some trivial changes you can make it into a valid C++ program. This was by design. So C++ can do a lot of things that C can't.

There's only one thing that C can do that C++ can't: on most systems, C has a consistent, stable ABI. This means that two C libraries can talk to each other, no matter which version of C compiler they used. C++ program produced by different compiler versions often can't talk to each other. For this reason, C is used more often by very low-level libraries that need to be used throughout a system.

dmazzoni
Visual studio is the only compiler without a stable ABI. ICC, GCC, sun's compiler and digital mars all have had stable ABIs for quite a while.
caspin
@Caspin: All the compilers use the same C ABI, including MSVC. The reason the C++ ABIs are not link compatible is the result of features like name mangling. ICC, GCC, and Sun's implementation aren't link compatible for C++. Even if they were, ~10% of computers does not constitute a standard.
Billy ONeal
@dmazzoni: A fair comparison that doesn't descend into either C or C++ FUD! Thank you! +1.
Billy ONeal
Noah Roberts
@Billy, I wasn't claiming ICC and GCC where link compatible. I'm claiming that GCC 4.0 and GCC 3.4 are link compatible. The various versions of Visual studio are not link compatible, that is an anomaly in the c++ compiler world.
caspin
@Caspin: That's not inherently clear from your comment, but I now understand your point. @Noah Roberts: C++ has much the same problem. Given that this is a comparison between C and C++ I don't think that observation against C really tacks.
Billy ONeal
C++ is a lot harder to learn than C. Most of C++ complexities can be replicated using C, you don't get them out of the box. This considering the standard languages, these to be of any use will require deep understanding of the available libraries, at that point they will be at a similar level of complexity.What I think you are stating is that if one only uses the more modern constructs of C++, one can skip the lower level stuff need for C. But then it isn't C++, but a the part of the language one is comfortable with. Literally speaking C++ was a superset of C 89.
Panic
@Panic: Yes, literally speaking. That said, C++ code written like C code is bad C++ code. If nothing else, C++ code written like C code is probably not exception safe.
Billy ONeal
Writing C++ code not using OO and the other abstractions can't be stated to be bad, all faculties of C++ have their applications, the canned answer that using C++ as C is bad is ultimately wrong. It has a lot of leeway I would agree that using C in the first place would be indicated unless some advantages are being used from C++, in C++ you have the possibility of choice how you use it is your own concern it is there that you make your proficiency shine.
Panic
C does not have a standard ABI.
anon
@Neil: But each platform has a *de facto* standard ABI, which is good enough in practice.
Donal Fellows
@billy oneil - Not true actually. You can teach someone C++ without having to throw pointers at them in the first chapter. "std::string x; cin>>x;"
Noah Roberts
@Panic: Depends on how you teach C++. If you teach C++ the way it should be used, it's likely to be easier to learn enough to do useful things than C.
David Thornley
+1  A: 

I actually find C to be easier, first, the standard is smaller, second there is "less to learn", and third, there's no way to suddenly be surprised by unfamiliar syntax when looking at code samples around the net. C has a smaller language footprint, less tokens have multiple context based meaning.

With that being said, between option A and option B, I choose option C...C# that is. The language is cleaner and makes worrying about memory management and reference types a little less confusing (although you still have to worry about them, it's much less troublesome for a new developer.) At the same time, you'll learn "Pure" Object oriented programming in a language that is still evolving. C# is one of the fastest growing languages, and the demand for .NET devs is starting to surpass that of C (but not C++) programmers.

My personal reccommendation however... Learn C, and then learn C#, or learn C#... and then learn C... Either way, once you know those two, learning C# takes just a few weeks and seems familiar.

My source? Me, 3 of my department co-workers, and a 2 hour discussion on this very topic around the office a few weeks ago, the devs that started With C seemed to have the most firm grasp on how code REALLY works, the devs that started with C++ leaned towards loose testing and "hacker" style coding, and the devs that started with C# usually had a really good grasp on "best practice." such as testing and design patterns. Either way, the C++ first programmers usually loathed learning anything else at first and fought against learning other languages simply because they were different. C# first programmers, usually didn't want to learn other languages, because they didn't feel that they'd have much to gain (tho i completely disagree) and C first programmers... learn it, realize how limited it is, and want to get away to something more expressive ASAP (C#/C++/Cyanide pills in their bosses's coffee, etc.)

Firoso
+4  A: 

Maybe not the answer you wanted, but I usually suggest new programmers start with python (www.python.org), then move (if appropriate) to another language when they have some idea what they want out of a language. Python is available for most platforms and is heavy duty enough to sustain large projects when needed.

If the choice is truly either C / C++ then by all means start with C. It's simpler and will serve as a springboard to C++ which you will ALSO want to learn. Both have advantages, and I choose which to use based on the project demands. Outside of pure systems programming / bit twiddling most folks I know find C++ a better choice for medium to large projects since it is object oriented. However there is LOTS of code still maintained in C even when it no longer really makes sense. (X-windows is my favorite example that I'd like to see re-written in C++)

Once you're comfortable with those languages I'd suggest you look also at go. (www.golang.org) It's a new language but I believe it will be a major player in the future. Go looks a lot like C but with some features to make concurrent programs easier to write. In other words, if you want to write programs that make the best use of that multi-core CPU you bought then you need to learn go.

Probably too long an answer. Sorry. :-)

Hotei

Hotei
I've seen entirely too many programmers start with languages like Python and become entirely too lazy. Python might be a productive language, but I'd not recommend it as a first language because it's to easy to abuse it.
Billy ONeal
DO NOT! Learn C expecting it to be a "springboard" into C++. If I ever have to work with you I'd have to do horrible things to you. People that learn C and then start writing C++ like that are a) a dime a dozen, and b) horrible, horrible C++ programmers.
Noah Roberts
A: 

If you know some programming and know what object oriented programming stands for, then definitely go C++. By other hand, if you are new to programming and want to learn basic principles and algorithms, then try whit C.

Since you are new, go for C. It is simpler and you can keep most of language in your head. C++ is just to huge for beginner in my opinion.

ralu
+1  A: 

C is a much smaller language and is, as a result, much easier to learn the fundamentals of. In comparison C++ is much, much, much, much larger and full of all kinds of sharp corners that can get you while you're learning. That being said, C++ is almost a superset of C, and you can start learning C++ by treating it as a "better C" and slathering on the other paradigms (object-oriented, declarative meta-programming, etc.) as you need them. Or you can, even better, start off with the simpler aspects of object-oriented programming and branch out into the other supported paradigms as you get more comfortable.

At their core, neither C nor C++ is "more powerful". They are both Turing complete and thus "equally powerful". What C++ is, however, is far more expressive. The language has many constructs built in that make some concepts easier to express and easier to comprehend than in C, although C could, in theory, do them as well.

One thing that puts C over C++ in terms of learning first, IMO, is that it is effectively the "lingua franca" of programming these days. Pick a programming language. Any programming language. Chances are very high that it has a FFI (foreign function interface) that's based on C. C is basically the "glue" language of the computing world these days, and the bridge, usually, between low-level programming and high-level programming languages. Further, there's also the issue of portability. I can't think of a modern environment that doesn't offer a C compiler (and very few older ones still in use which don't). I can think of dozens without C++ compilers, however, nor Java compilers nor any other of the "popular" languages. If you learn C you'll be able to get at least some programming done on practically any platform short of the tiniest of embedded systems (and even those will mostly have C compilers). C++ has nowhere near the saturation.

JUST MY correct OPINION
Explanation for the -1 vote please? Did I make a factual error? Did the response not actually answer the question?
JUST MY correct OPINION
"They are both Turing complete and thus "equally powerful"." Note that the proof that tells this is true does not also specify that the two UTM's run the same programs at the same efficiency. In fact, what is easy for one could take another more resources and time than are contained in our universe.
Noah Roberts
Had I said "equally efficient" you'd have a point. Seeing as I didn't, you don't.
JUST MY correct OPINION
+2  A: 

I think this is very debatable...

I personally thought that C was easier because there was no classes. But, learning about classes wasn't that much more to learn. Unfortunately, I was naive, and still am for that fact of the matter, and didn't realize there was a whole OOP methodology, that, because C has no classes, makes C++ a lot more difficult to master (I haven't mastered it yet either).

Also, what are you interested in? C++ is very good for making GUI (Graphical User Interfaces) with another library. Whereas, C is good for core development rather than graphics, because it is generally more efficient than C++.

Another point, once you learn C++ you have an easier transition to a lot of other languages because it OO. A lot of popular languages are OO languages and learning C++ gives you an easier transitions to those languages. Whereas, with C, you only get a view of procedural languages.

If your not in a rush why not learn the basics both? They are very similar, C++ is almost a strict superset of C (with a few exceptions). This way, you can learn about the differences. Also, when you learn both you realize that there are differences in the way approach programming with each since C is procedural and C++ is OO.

Yet if you are in a rush, chances are you probably should learn C++ even though it is slightly harder to learn the basics of because it opens up a lot of opportunities for beginners sooner.

thyrgle
A: 

Between C and C++, which is easier to learn?

C++ is completely compatible with C So for C++ there are more things to c.

Which is more powerful?

C++ has added features that can help you, but in terms of achieving something, Both are Equal,

Does either do anything special that the other doesn't?

C++ has many added features that if learnt properly can make your life easy.

Learner
C++ is not completely compatible with C...
Panic
+3  A: 

So much debate. Blah blah blah. I'll make it simple for you:

Learn C++. Read this book first.

John Dibling
The book recommended is _Accelerated C++_. A heartfelt +1 from me.
sbi
Who's afraid of C++ is a great book as well for beginners
fuzzy lollipop
+2  A: 
Norman Ramsey
+2 nicely said (off by one error)
Brian R. Bondy
+1  A: 

Between C and C++, which is easier to learn?

C is a reasonably compact language. If you have the kind of brain that can grasp the concept of pointers, you could learn C inside a month.

C++ is a monstrosity with truly enormous depth. It will take you years to become a C++ expert - and you will probably never truly master it.

Which is more powerful?

C++ is a superset of C. What C++ adds will allow you to do more while writing less code - I'd call that more "powerful".

Does either do anything special that the other doesn't?

C++ has a more well-equipped standard library, which has some very nice data structures and algorithms.

C on the other hand lacks the kind of facilities for doing "data structure stuff" easily and safely. If you want to be productive at writing any kind of non-trivial program - you want the "data structure stuff" ;)

C++ will merrily let you write C-style code. This is considered extremely poor form. And if you learn C before you learn C++ you'll be doubly at risk of doing this. Here is a good starting point of you want to write good C++ code (a very difficult thing).


Now that being said - learn C# (or python or something). From your own criteria: It's easier to learn, it's more powerful, and it does quite a few special things that C and C++ do not.

Andrew Russell
C++ is not a superset of C (it was). I wouldn't encourage no one to learn C# unless it was only for a short term professional reason (learn Python if you must you can always use IronPython or Boo with .net)
Panic
Doing more while writing less code is not "more powerful" it's "more expressive".
JUST MY correct OPINION
Thankyou above two commenters for being pedantic :)
Andrew Russell
@Panic: As long as we're being pedantic, C++ was never quite a superset of C, since there were things that differed slightly between languages. 'A' is an `int` in C and a `char` in C++, and there are cases where end-of-line comments make a program mean two different things in C95 and C++03.
David Thornley
+1  A: 

I think one point is to mention that all of these imperative, statically typed languages are semantically very similar indeed. In fact semantics is much more important than syntax when it comes to learning as IDEs will take care of much of that, plus compilers and debuggers.

The semantics of OOP are quite specific and since C isn't strictly OOP by any means, then C++, C# or Java are better as they support this. It's also much easier (I think) to build GUIs with these more modern languages.

AlexW
C++ actually is a _multi-paradigm language_, not an OO language. It does support OO, but it also supports other paradigms (for example, Generic Programming).
sbi
A fair point, but I'd say OOP is the most significant aspect to the C++ multi-paradigm language features. I also think that OOP should be something to think about even in the very early days of learning to program.
AlexW
+7  A: 

Although C is the "smaller" language and C++ has this !!!HUGE BAG OF FEATURES!!! that so many here seem to find overwhelming, I would definitely say that C++ is going to be easier for a newb than C so long as you stay the hell away from the C part of the language.

You see, you can write C++ code that uses char*, char[], gets, fgets, etc... but everyone who knows C++ knows that you'd have to be freaking insane to want to. Why? Because of all the nasty bugs that live and breed there. You see, working with these types and functions is sort of like going out to a farm with a rifle to make a hamburger. In the end you might very well end up with a slightly leaner product but ALWAYS having to do all that gutting and skinning just gets really freaking old....so at some point you'll make a mistake.

And who in their right mind takes a new cook and hands them a rifle? No, first you hand them a prepackaged chunk of ground beef that's been processed by an expert butcher. This is what the C++ part of C++ is like. You know, the STL and all that...

Sure, at some point you're going to go into an expert cook's kitchen and see a spice you don't recognize. Here's the thing though...you don't have to use it. If you did learn you might make a better burger but if you're happy with what you're eating then you don't need to focus on the mass ratios of sea salt and table salt.

Of course, you have to learn from the right cook. Ted Nugent's cookbook isn't going to be the best for someone that doesn't own an arsenal of assault riffles...and a C++ book that starts out with scanf("%s",name) is likewise absurd. Of course, someone else already linked you with the best beginner book out there, Accelerated C++.

Everyone of course knows that fire+gun+knife==food is much easier to remember than the 20 different packages you need to cook a hamburger like a modern, civilized human being but I prefer the later to the former most of the time and I definitely would consider it the easier path for those just learning to eat.

Noah Roberts
+1 for the Nugent reference. Though The Nuge would probably be an ASM programmer...
Drew Hall
A: 

Everyone seems to be giving mostly an over-engineered answer.

The simple question was:

Between C and C++, which is easier to learn? Which is more powerful? Does either do anything special that the other doesn't?

The simple answer is:

C is easier to learn.

C and C++ are equally powerful languages, it all depends on what type of problems they are applied to.

C++ has a lot more special built-in constructs/abstractions that C, that is why C is easier to learn.

... more could be said but this covers your questions.

Panic
Having taught C++ to students (with one year of Java experience as well as to programming novices) I disagree that C is easier to learn. They had immense problems with the C parts of the language, but immediately picked up, e.g., `std::string`.
sbi
If you go beyond your favorite part of the language and really teach the complete package you will have to teach them what std::string is and the complexity is then the same. If you only teach your favorite part then you aren't really teaching them the real thing (it may depend on the objective of the class), that is why abstractions can be dangerous without a deeper understanding of the basis.
Panic
A: 

In order to avoid a lot of confusions it is best that you learn C first. Understand pointers very well and after you played a little in C (File I/O, some data structures: trees, [double-]linked lists, arrays) and you are confident you can start learning C++ in parallel. C is a very important language to understand because it will make it easier to understand problems that may appear in languages that are more evolved (C++ for example).

Learning C++ first it is easier but is has some disadvantages. Personally I knew a person who always used STL containers and was unable to debug a code with pointers written mostly in a C style. I believe that those persons shouldn't be considered programmers.

Enough talking, I say you learn C first (especially pointers). C++ will just work after that.

Iulian Şerbănoiu
Learning C first usually makes for some very confused C++ programmers who write C in C++, leading to bad style and easily avoided bugs.
sbi
+12  A: 
Owen S.
I can just say: WOW.Did you have a heavy dinner last night?
nico