Shall I teach myself C or C++?

Hi all,

I'm a software engineering undergraduate in the UK, in a course which teaches concepts mainly through Java. Having read a lot of forums and canvassed employer's websites I think it's vital I am able to program in C or C++ also.

A little background to my course: where programming in Java has been used, it has been used to teach the concepts of programming (almost exclusively OOP), such as the importance of design, testing methodologies, data structures and algorithms, and also graphics implementation (focussed on the maths of it). So I have a good idea of concepts (though I have a HELLUVA lot to learn), it's not been the case that we've been spoiled with library routines.

We've also had some classes using C, and classes on the architecture of computers - based on Patterson & Hennessey's MIPS. This has given me an insight into what happens down on the metal, cache effects and whatnot. With regards to C though, it was a one semester class, and so I've not had much experience of it.

That's just a background which isn't too relevant, really my question is, which one should I learn, C or C++?

Since I already have a fair grasp of OOP, it might be good to learn a procedural language. But I've also heard that C is a subset of C++, so can I kill two birds with one stone and essentially learn both by learning C++? And where's a good book/source/community to learn?

Hope I've made sense, and posted in the right place.

~ Graham

GrahamAllan
Saturday, January 26, 2008

Deleting …Approving …

BOTH. In real world, you need to learn several programming "paradigms" (ways of solving problems). C is procedural, C++ is object oriented. There are others.

You should get the idea that you are NOT just learning C programming language syntax, but procedural programming way of solving problems using C. And the same goes for C++ and OOP.

Many schools dropped procedural, functional and other "paradigms" in favor of ONLY OBJECT ORIENTED tools as Java or C# (.NET), because Java is "today's programming language".

Even if your employer assign Java projects to you, you may find that the concepts you may learn in C/C++ may help you.

You may find that Java and C++ have a different way of handling variables, even if they have similar syntax.

If you think it's the paradigm that makes the biggest difference then that's steering me towards going for C, as I have had, and will continue to have uni classes based in Java. I don't think trying to learn both at this particular moment is a good idea for me, as I have a full semester of classes coming up, and I don't want to confuse matters too much.

Could you then recommend a resource for learning C, which includes the perspective on the paradigm? I have a desktop reference ( http://www.oreilly.com/catalog/cinanut/ ) which defines the language, but doesn't offer much in the way of structured exercises/tutorials for learning the language. I'm ideally looking for a resource that will offer a logical path through the language, and also have a bit on performance/security pitfalls.

If you know of any online communities that offer this kind of thing that would be great too. I think the guys on here would find me quite tedious.

Once again, thanks for the reply.

GrahamAllan
Saturday, January 26, 2008

Deleting …Approving …

If you can find a copy, I highly recommend "Reliable Data Structures in C" by Thomas Plum.

I WISH I'd found this book early in my 'C' career. This book explains a lot of the 'infrastructure' currently used (like in Linux) in creating a 'C' application.

> so can I kill two birds with one stone and essentially learn both by learning C++

That's certainly true to the first approximation, though learning the superset is obviously a heavier stone.

Still, if you know C++ well than you can ipso facto program in C. Also as far as I know all programming environments that support C (especially Windows and Unix) also support C++: if you can only know one then I'd recommend C++, but learning it is a much bigger job than learning C.

Still, with the C++ "STL" it is possible to learn C++ in a slightly superficial way that doesn't include your understanding, for example, pointers and pointer arithmetic. Pointer arithmetic isn't even especially important or useful, however you will find some of it in any 'legacy' code that you may end up working with, and it will also often show up in silly interview questions that are intended as filters, so as well as learning idiomatic C++, you might also make sure you understand show-off tricks like using pointers to copy a string using a statement such as "while (*p1++ = *p2++);".

> And where's a good book/source/community to learn?

I liked _Thinking in C++_ to learn the syntax (how to read it), followed by Meyers' _Effective C++_ (mistakes to avoid when writing it).

Christopher Wells
Saturday, January 26, 2008

Deleting …Approving …

>>>...learn both by learning C++? <<<

I suspect that if you just learned C++ you might have difficulty knowing what the boundary is between them.

EMF
Saturday, January 26, 2008

Deleting …Approving …

I'd go for C rather than C++. C is a much smaller language than C++ and you can reasonably hope to learn it in a month or so. C also forces you to think at a lower level than does C++, so that will be a good remedy for any injuries done by learning Java. Finally, C++ is based on C, it is (mostly) a superset of C (there are very few minor differences, that can be reasonably neglected by the novice student) so you will be able to apply what you learn in C to C++ down the road.

I'm not saying that you shouldn't learn C++, but C will be a quicker study and you can always go back and learn C++ later on. You will probably need to learn C++ as a professional programmer, but there's no reason to suffer through it at the moment, since you already have a perfectly good OO language at your disposal (i.e. Java).

Is a rather simple little language. I've heard it called a portable assembler. Like an assembler it gives you enough freedom ( e.g. pointers, memory management ) to really get yourself in trouble. So it can be a good thing to learn. I do not think there is much demand for it anymore except in small embedded systems.

It is a rather large step from C to C++. At least you have been learning OOP concepts. Like C, C++ requires an understanding of details that Java takes care of for you (e.g., why pass by reference to a copy constructor? ) Considering that you have other studies to be concerned with, you might put C++ off for a while.

EMF
Saturday, January 26, 2008

Deleting …Approving …

The problem with learning C first and C++ later, as I found out, is that you tend to continue doing things the old C way. Stuff like managing your own memory where the STL would be a better choice, using only pointers and never references, using integers where bool or enum would be more appropriate, using #define macros where enumerations or inline functions would be better, and so on.

C99 fixes some of these, but it isn't in widespread use yet.

dev1
Sunday, January 27, 2008

Deleting …Approving …

>>>...continue doing things the old C way.<<<

That is a potential hazard, but not really a convincing argument. I doubt he'll be using C enough to build up those habits.

In any case, one should have the judgment to use the most appropriate language features.

EMF
Sunday, January 27, 2008

Deleting …Approving …

Oh, and enum has been around since K&R.

EMF
Sunday, January 27, 2008

Deleting …Approving …

For the most part if you learn C++ you learn C, but I would start by focusing on the C portion first. You already have the OOP basics from your experience with Java. With just C you will find that you do have to think a bit different (which is a good thing).

Once you have C down then move on to C++.

It really is a shame that so many schools focus on hand-holding languages like Java and VB. There are important lessons to be learned by coding in assembler, C, and C++. They are much closer to the machine and they expose one to a lot of "good stuff".

I'm still amazed at the sophomoric belief that C/C++ is archaic and "broken" some how. C and C++ are what they are. They are incredibly powerful tools and just as vital today as they have ever been.

NuclearToilet
Sunday, January 27, 2008

Deleting …Approving …

Learn C first, then increment it. :)

Totally Agreeing
Sunday, January 27, 2008

Deleting …Approving …

"...hand-holding languages..."This made me laugh :-)

asmguru62
Sunday, January 27, 2008

Deleting …Approving …

"Oh, and enum has been around since K&R."

I'm pretty sure its not in the edition I have from '87.

Arethuza
Sunday, January 27, 2008

Deleting …Approving …

C++ these days is typically used in ways that are quite different from C -- the STL and templates are complex subjects.

I would also recommend one high productivity language, probably either Perl or Python.

Learn shell scripting, and, if going into a Windows environment, batch programming, as well. I mean well enough that you can do serious work in those scripts.

Start with C.

Then the "high productivity" language.

Then C++.

Plenty of work cut out for you, but well worth it.

Read Joel's article on the perils of Java schools. A couple of times. Just to keep you motivated.

dot for this one
Sunday, January 27, 2008

Deleting …Approving …

Here's a slightly contrary view: learn C++ first, preferably from some source which does not assume that C++ builds on a C base.

>>> I'm pretty sure its [enum] not in the edition I have from '87. <<<

Hmmmm. I'll have to check my K&R, but it is at the office.

I was using enum back when I was learning C from K&R, but it might not have been in the original book. My 1982 edition of VAX-11 C does have enum. So it has been part of some versions of C before your edition of K&R.

EMF
Sunday, January 27, 2008

Deleting …Approving …

Thanks everyone for your points of view.

I think I'm going to go with C first, maybe try to copy some of the projects I've done in Java to really highlight how different the approach has to be. I'm not really sure yet, I have ordered the Reliable Data Structures in C book mentioned earlier, as I got a good deal.

I'd like tasks that really bring into focus performance or security pitfalls, as Java can help you ignore that, but I think it will take me a while before I'm at that point.

Probably in the summer I will move on to C++ with a Deitel & Deitel book I have.

Your views, tips and pointers (bad pun intended) are very much appreciated.

~ Graham

GrahamAllan
Sunday, January 27, 2008

Deleting …Approving …

The book you are getting uses the old style of function headers, which aren't even supported as default (or at all?) in C compilers made within the last 15 years. That will be kind of annoying. It's weird that that one was recommended.

You should buy the K&R, 2nd (current) edition instead and read it and follow the examples. It's designed to teach you C. That should take you a semester.

If you still want to keep going, then "C Traps and Pitfalls" and "Deep C Secrets" are the two advanced C books that are essential for really understanding all the stuff you didn't quite get the first couple years working with C.

Scott
Sunday, January 27, 2008

Deleting …Approving …

Historical nit: I checked my K&R and do not find enum. So saying enum has been "around since K&R" was incorrect. However, since it was in some versions of C by 1982, it actually was a feature of C before C++ had gotten far enough to be called C++ or be generally available.

EMF
Monday, January 28, 2008

Deleting …Approving …

One more vote for "learn C first, then C++ later".

As mentioned, C is a small enough language that you can learn enough to be reasonably proficient in a few months. It also makes a nice complement to Java, which is kind of an anti-C, in terms of features (OOP vs. procedural, garbage collection vs. manual memory management, massive class library vs. a small standard library)

On the other hand, C++ is one of the most complex languages in common use, and it's not at all uncommon to take years to get your head wrapped around it. I don't know off-hand of any resources for learning C++ that don't assume that you're at least passingly familiar with C, anyway.

I don't have any recommendations for good tutorial sources for learning C, but once you start learning, a reference like the K&R book, or "C, a reference manual" by Harbison and Steele, will be pretty useful.

If you want to *really* understand C at a deep level, pick up a copy of "The C Standard Library", by Plauger, and "Deep C Secrets" by Van Der Linden.

Thanks for the recommendation on the "Reliable Data Structures in C". I've taken a couple C/C++ courses but my (just completed last month) undergrad degree in CS for the most part used Java (I'd say 3/4 Java, 1/4 C++ with tiny bit of MIPS assembly and shell code thrown in -- this was at University of Wisconsin at Madison). I've read some Linux kernel code (scheduler a bit and some drivers) and the data structures were a stumbling block so I ordered the book.

I'm sure everyone else has actually answered your question how you intended but I'll answer it a different way:

Shall I teach myself (C or C++)?

And the answer is YES. From the position of an employer who conducts interviews, showing an extra-curricular interest will score you bonus points. Contibute to an open source project or give away some freeware/shareware utilities. They don't have to be ground-breaking but the fact that you've written them shows you have a passion for the subject and haven't just jumped on the IT bandwagon.

Adrian
Tuesday, January 29, 2008

Deleting …Approving …

Don't bother with C. You can program C-style in C++ if you like, but with fewer outdated gotchas like not being able to declare a variable after a statement. C++ imposes no extra costs other than those you choose to pay. It is very nearly but not quite a proper superset of C. In practice, almost every C program you encounter will also compile and run in C++.

My credentials: I programmed professionally in C from 1985 to 1994, then switched to C++, which I've been using ever since. I have worked predominantly on mobile devices (Symbian OS, RIM OS, etc.) but also on Microsoft Windows and Mac OS.

I'll just chime in with what I think are good books for both C and C++:

Either "the C Programming Language, 2nd Ed" by Kernighan and Ritchie or "C, A Reference Manual" by Harbison and Steele.

I also like "the C Standard Library" by P.J. Plauger. It's got lots of good code to read, and the author was a member of the ANSI C Standards committee, so he knows what he's writing about.

For C++ I would recommend the books "Effective C++", "More Effective C++" and "Effective STL" all by Scott Meyers. These books won't teach you C++, but they'll give you a strong overview of all the gotchas in the language. There is no way to be a good C++ programmer without knowing all the ways the langauge will make you miserable.

Other than Scott Meyers books, you will also need a good set of references: "The C++ Programming Language, Special Edition, 3rd Ed" by Bjarne Stroustrup and "The C++ Standard Library: A Tutorial and Reference" by Nicolai Josuttis, together cover the material pretty nicely. You probably also want an introduction to the langauge, such as "C++ Primer" by Stanley Lippman.

Depends on your motivation. Is this vocational training or education? It may be a decent plan to learn C or C++ with a view to opening up career opportunities. In which case you maybe need to get some context, do you have a particular problem domain or business area in mind? Does that industry or solution space tend make extensive use of C or C++. This would be a pragmatic approach to addressing vocational concerns. If that's your motivation then I think you also increase your marketability by learning scripting languages and tools such as ant.

Playing a longer game, you could consider what will be most educational. What will help you extend your capabilities and prepare yourself to learn more languages in the future. In which case, as you already have OO skills from Java, I'd suggest that C would be a better "brain workout". having to manage your own memory and making sure you understand pointer arithmetic may make you love Java :-) You might also consider a language such as Ruby, again a contrast with Java.

I'd endorse the view that

Dave Artus
Wednesday, January 30, 2008

Deleting …Approving …

I just want to add that whatever languages you choose to learn, make sure to use at least one book per language that has actual programming problems that you are supposed to write yourself, and make yourself do every one of them.

Also, at least to start, turn off any autocomplete or intellisense options in your editor, and start each program with a blank text file. It can be painful at first, but the time you spend now typing things over and over again will pay off down the road when you know it so well that you don't have to think about it.

Kevin
Thursday, January 31, 2008

Deleting …Approving …

Instead of C or C++, I think you should consider learning D. D was invented by the first person to write a C++ compiler for the PC, and it addresses all of the major shortcomings of C and C++, while retaining complete C performance and power as a systems programming language. It's even compatible with C binaries.

Scott
Friday, February 01, 2008

Deleting …Approving …

Instead of learning 'D', you should learn VB6. It too is an obsolete language, useless on resumes, with no vendor supporting it any more.

I mean, Linux is still being written in C, last I heard. That makes it just TOO useful -- you need something esoteric.

Oh, you can do better than Forth for obscure languages: how about Bliss or Snobol? How about ML (a language I was forced to learn in college, for no apparent reason other than pure sadism) or BCPL (yes, I know that it's the direct ancestor to C, and was used as the original language on the Amiga, but just try finding an implementation today). Maybe Oberon or Occam?

Actually, if you could find good books on any of those languages, and a good implementation of a compiler or interpreter, they'd probably be pretty good languages for a student to learn. The problem with most of the current languages, from a pedagogical standpoint, is that they descend from a fairly limited ancestry. If all you know is C, C++, Java and C#, you get a very constrained notion of the possible range of language features and syntax.

The time to explore these sorts of things is while you are still a student: when you get a job and a family, you'll not have the liesure time to devote to this sort of learning, and you will have missed your best chance to expand your horizons.

I think I would have to demur on this one. If you learn how to program well in the best C++ style and then are faced with a strict C compiler, you will have a problem.

Many aspects of memory management, data structures, scope, compilation units and modularity, among other things are handled quite differently in the C style of programming.

I would support others who suggest learning C first. The "close to the metal" style of C will give a good insight into the various programming burdens the designers of Java were trying to solve. It will be very educational, and it will be fun. There is no "nannying" going on in C, there are no padded walls or safety harnesses to prevent you making a big foobar. The most common output from an experimental C program is "access violation" or "seg fault".

Ian Boys
Saturday, February 02, 2008

Deleting …Approving …

" The "close to the metal" style of C will give a good insight into the various programming burdens the designers of Java were trying to solve."

That's pretty much exactly what I'm looking for. And I've already hit on those page faults with even the simplest messing around with a "Hello world" program (I'm waiting on the books coming, just going through the starters of cprogramming.com).

I'd like to thank everyone who commented for their insight - and I'm sure it won't be long before I'm back bugging you with more questions.

~ Graham

Saturday, February 02, 2008

Deleting …Approving …

My 1988 copy of K&R (the thick one) has enums.

Oh, yeah, and thanks for the tip on Reliable Data Structures in C - sounds good. I just ordered it from Amazon.

Having programmed in C++ for over 8 years I find it VERY HARD to go back to C. There are quite a few quirks which look braindead when coming from C++.

C is OK if you have no use for C++ or unable use it due to microcontroller flash space constraints.

Andrei Errapart
Sunday, February 10, 2008

Deleting …Approving …

"Enumerations are new since the first edition of this book." - K&R 2nd Edition, Appendix C

Can we end this argument now?

Kevin
Monday, February 11, 2008

Deleting …Approving …

Why not for a clean C? Is called Pascal.

You can have the power, less the stupid compiler times and messy rules that breaks for magic reasons.

When, for example, you take 2 weeks in setup a small-medium website under linux and see how involved is compile at that librarys, how everything breaks, how have the exact GCC version mater, why some makefiles not work, and that is ONLY compiling (man, 30 minutes for a simply library?) and not installing and doing productive work, understand why centuries ago, when turbo pascal appear was a massive step forward in productivity.

Learn C/C++ is whorty only for cut 1 week in solving compiling issues when you try to install something :)