programming-C or C++

In an effort to be more knowledgeable about programming Arduino, would I do better to teach myself C or C++. I know C++ is based off of C... but there seem to be some differences.... thus the question.

thanks in advance for your suggestions.

I am sure that other people might have a different opinion. I personally think C is the most suitable language for embedded systems. So, naturally I'd start with C.

While AVR-GCC does allow some C++ constructs, the support is quite limited.

So, I'd vote for the plain-old C :)

I agree with AlphaZeta. C++ is a wonderful language--for desktop computers. IMO it does not belong in embedded systems (such as arduino). The reason is that C++ can enable some bad behavior (by inexperienced programmers). C is more rigid (and smaller).

Also there is less syntax in C than C++. C++ contains some very nice syntax but it can be seemingly irregular to new programmers and can be confusing.

As someone who has taught C++ as a first language to college freshmen I can with some confidence say that while it has probably the most long term utility to a programmer it's not a great first language.

Besides C++ was originally an extension of C (and still mostly is/should be) so anything you learn in C counts in C++ when you want to learn that later

While AVR-GCC does allow some C++ constructs, the support is quite limited

Limited in what way?

The reason is that C++ can enable some bad behavior (by inexperienced programmers)

That's true of all programming languages including C. It's not a reason to choose one language over the other.

C is more rigid

Rigid? C? Compared to what? C++?

Just focus on learning C.

Let me make this clear. C++ is a wonderful language. I in no way mean/meant to bash C++.

The reason is that C++ can enable some bad behavior (by inexperienced programmers)

That's true of all programming languages including C. It's not a reason to choose one language over the other.

The OP asked C or C++ so my answers were in relative terms. Also, I wasn't saying it was a reason to choose one language or another in general. I was saying that its a good reason to choose to learn C first (as opposed to C++)

C is more rigid

Rigid? C? Compared to what? C++?

yes compared to C++. The OP was asking about one vs the other.

I agree with mspguy.

In addition to his reasons, I think it would be difficult to find C++ educational materials (books, classes, etc) that teach it in a way that is relevant to small embedded systems like Arduino. A lot of "the power of C++" lies in its ability to draw upon a vast number of libraries and classes that implement "standardized" access to a lot of the things you want to do in a typical desktop environment, and a lot of C++ books will focus on that (creating windows, handling mouse events, interacting with the file system, etc.)

First off, there isn't much difference between C and C++ for the basic operations and thus not much difference for the beginning programmer. The real value of C++ lies in memory management in classes, the STL and other advanced features which you really will not be using the first months or so.

Second, C is (nearly) a perfect subset of C++, so it's not like learning two distinct languages. Do note that correct C++ programming is not at all like compiling C with a C++ compiler.

Concrete, solid advice would be to start with K&R and, if you're interested in C++, move on to the C++ Annotations. The last URL is an entire bookwork on correct C++ programming for people moving over from C (or even Java).

Of course, if you want to get started on C++ right away and 'skip' C, the The C++ Programming Language is the place to start.

On a general note, I do not agree with some things being said here, specifically regarding to the rigidity of C vs. C++. C++ is much stricter and will not allow you to do many things due to is very strict typechecking. C++ can be used more safely than C by casting, hiding your data, etc. But you will have to do it. It's not inherent to any languge that you always use it correctly. ;) Also, C and C++ have roughly the same syntax (barring some C++-specific constructs) and it would be mistake to say C++ is complex due to its syntax. The complexity of C++ (vs. C) is due to its massive semantics. Lastly, I do not agree with the idea that C++ is somehow more bloated than C or that it only has real value in larger, more complex programs, but these sort of statements can be argued about forever.

Incidentally, and just out of interest: if someone wanted to only ever use the AVR 8bit processors, would the experienced people here suggest learning AVR assembler first before C, or after, or learning both simultaneously, or not ever learning assembler at all?

If you master the C language, you won't have a problem transitioning to any other programming language, at least in my experience, because in the end they're almost all alike (not all, but most of the popular are). Along with the K&R i'd recommend to get the C Programming 2007 video Tutorials from VTC, where they use the K&R book to teach the language.

would the experienced people here suggest learning AVR assembler first before C, or after, or learning both simultaneously, or not ever learning assembler at all?

The problem is that if you want to program beyond (below?) the primitives that the Arduino libraries provide, you must also learn something about the Architecture of the AVR. What a register IS, who changes it, and how, how that interacts with other bits of hardware on the chip, how it all ties together to talk with your software. High level languages insulate you from that (some more than others.) Technically, I believe that you can learn the necessary architecture details without actually learning "AVR Assembler" per se. But you would have to have a background in a similar assembler on a different architecture to build on, or a book that explained the architecture at the C level (some ARM books do this.) (these things would usually be explained as part of an assembler course.)

I guess they key useful thing to take away is what registers you have to access, what's the difference between a register and a RAM location, what's the difference between those and flash locations and EEPROM, and what operations can you expect to be efficient between what sort of operands. You probably don't need to know the exact instruction to use or which instructions can't use which registers, or about the T bit, unless you plan to actually write assembler code. But you will need to know a lot about clearing, setting, testing, inidividual bits in specific registers; not you "usual" C programming.

Hi All.... Thank you for the responses. My feeling is C is probably the place to start. I appreciate your remarks. Thank you.

Rhetorical questions here. Is it even possible to NOT learn C if one started off attempting to learn just C++, as C is indeed a subset of C++? It's not like learning C++ as a first language via a beginners guide could hide the lower level functions, structures, and operators used in C? I understand that C++ contains OO via classes, but surely that's not the starting point for a book covering C++ aimed at beginners?

If I was illiterate in English and my goal was to able to write English letters to my family would any means possible end up where I didn't also end up learning how to read English even though that was not my stated goal?

To keep it in perspective of a beginner wanting to learn to program their new Arduino board. If they choose to just learn C would they then not be 'allowed' to use the serial library because it's implemented using a C++ class structure?

I just think in context of the Arduino platform one best explains to a beginner that they will be learning C/C++ to program their board, without having to explain the differences and similarities between classic C Vs C++, as I think it just brings up distentions that don't apply to the Arduino use of AVR gcc.

Maybe I'm just being over critical about the subject? ;D

Merry Christmas all.

Lefty

Rhetorical questions here. Is it even possible to NOT learn C if one started off attempting to learn just C++, as C is indeed a subset of C++?

A retorical answer then. :) Yes, it's quite possible. I've never learned C and began with Java, later moving on to C++ (through the C++ Annotations link I've given above).

I know about some C-like constructs where they share C++ syntax and functions, but I for example have never learned to work with malloc and friends, C casts, atoi and suchlike, and would have problems with function overloading etc.

I'm sure I would have to delve in the C books to write the same programs in C as I could in C++. Just because C is a subset of C++, does not mean you need to know everything C has to offer, specifically those parts of the language replaced by C++ alternatives.

So it's quite possible to just learn C++ and ignore the depricated parts of the C language, meaning that while you're fluent in C++, you couldn't write the same programs in C.

would have problems with function overloading etc.

Function overloading is a C++ feature, not a C feature.

malloc and free from C are replaced, in C++, by new and delete, but they perform the same functions. And guess how new and delete actually work. Yep, they call malloc and free.

meaning that while you're fluent in C++, you couldn't write the same programs in C.

I don't think that one can be fluent in C++ without understanding that classes are just an extension of C. A class just encapsulates functionality that is basically provided by C.

One can not write any C++ code and not use the C code that C++ is a superset of. All the standard operators (+, -, *, /, ++, ==, =, etc.) are basic C, as are many of the statements like if, for, switch, etc.

PaulS, I think you failed to see my point. I fully agree with you that the normal mathematical operations are the same in C and C++, but the parts of C that are replaced need not be studied by C++ programmers. And this can lead to very nasty errors. Your statement for example:

malloc and free from C are replaced, in C++, by new and delete, but they perform the same functions. And guess how new and delete actually work. Yep, they call malloc and free.

...is one of those nasty errors. While new[] and friends may call malloc() and free() eventually, they perform different tasks. For example, operator new() may call constructors of objects and initialise memory in the other ways than malloc() would. Likewise, delete[] will call detructors of objects whereas free() will not.

Yous assumtions that operator new() and malloc() perform the same function would have led to very, very nasty bugs. This is my point exactly: you may know what you're talking about in C but apparently you do not know the ins and outs of C++ and make mistakes that would render your programs useless.

But we're digressing very much from the original topic. Sorry for that... ::)

I guess we'll just have to agree to disagree, then. There is only one pool of memory available to any application running on any given computer. There can, therefore, only, fundamentally, be one memory management function. For C/C++ applications, that is malloc and free.

The new and delete operators are addons for C++ applications. But, they can not directly allocate and de-allocate memory. At the core, then, they must call malloc and free.

Now, this is not to say that all they do is call malloc and free. Certainly there is a lot more to what new and delete do than simply calling malloc and free.

But since malloc is THE memory allocation function, and free is THE memory de-allocation function, new and delete have no other way of getting memory and freeing memory than to use malloc and free.

the parts of C that are replaced need not be studied by C++ programmers.

I disagree here. Unless one understands that new and delete don't have their own pool of memory, and can't somehow allocate all the memory that an application might want, one gets into all kinds of trouble with applications failing when there is not enough memory.

Only knowing about the top layer of a multi-tiered programming language causes one to assume that the top layer is all that there is.