Good book to learn C/C++ ?

Status
Not open for further replies.

nlecaude

Well-known member
Hello,
I've come to a point where's I'd eventually like to write my own Arduino/Teensy libraries and was wondering if there was a good book to learn C/C++ that is micro controller friendly ? I already know a lot of Python (classes, oo programming etc) but would need to translate this knowledge to C/C++ in the context of Arduino.

Thanks !
 
With all due respect to K&R, Harbison and Steele is better. Both are excellent, but K&R is now more of a history lesson than a reasonable reflection of current ANSI C.

I wish I had a good C++ reference for you. C++ is absurdly complicated so you'll need to study half a dozen books, but if you stick to a sensible subset, it's safer than C while being about as efficient. You can't just learn the language. You need to learn the pitfalls as well.

For what it's worth, I've been paid to program in C++ for well over 20 years. I'd like to see C++ die of natural causes, but it'll probably be the other way 'round.
 
Learn C on a PC. Master that before undertaking C on a microprocesor.
Procrastinate C++ as it is 100 times more complex.
For me, when I came to view C as a machine-independent assembly language-like thing, not a high level language, it all clicked. C pointers made sense in that context.

All we really have for small memory (Kilobytes, not Megabytes) microprocessors is C and C++, where the latter can and does get out of control in terms of needless abstraction forced upon us by those with a C# background.
Straight C makes the code self-obvious. On larger/Linux based MCUs, there's Python which is a high level language and weird at first but kind of a refreshing change from C++. And these large MCUs can do Java.
 
Last edited:
You can't go wrong with the free "Thinking in C++":

http://www.lib.ru.ac.th/download/e-books/TIC2Vone.pdf

Personally I would start with C to begin with, as I'm an Objective-C coder most of the time and find C++ a tiny bit restrictive at the runtime (which doesn't really mean much on a Microcontroller, hahahah).


-edit- don't forget that if you are using Arduino/Teensyduino then you pretty much already know C! If you look at the code I have posted over the past two months it's all C++ actually in the Arduino IDE :)
 
Last edited:
You don't necessarily need a " good book" to learn C++ just to write an Arduino Library. The myth that you first have to learn C in order to learn C++ has also been busted many times. It is often repeated by people that have preconceptions based on decades worth of programming experience in C but lack equivalent expertise in C++.

I don't think any of the books mentioned so far offer a specialization in the area of applied microprocessor programming but are more aimed at a broader audience. If you google for a combination of the terms C++, embedded, microcontrollers you'll find a good explanation of the benefits and pitfalls of using C++ on microcontrollers.

Something I have learned in 25 years of engineering is that you need to get your feet wet and hands dirty as quickly as possible. You'll learn more from your mistakes and from the obstacles that you'll face than from any book. Or in essence, start writing code as soon as possible.
If you can write a decent sketch, then there is no reason you should not be able to start writing a library.
 
You don't necessarily need a " good book" to learn C++ just to write an Arduino Library. The myth that you first have to learn C in order to learn C++ has also been busted many times. It is often repeated by people that have preconceptions based on decades worth of programming experience in C but lack equivalent expertise in C++.

I don't think any of the books mentioned so far offer a specialization in the area of applied microprocessor programming but are more aimed at a broader audience. If you google for a combination of the terms C++, embedded, microcontrollers you'll find a good explanation of the benefits and pitfalls of using C++ on microcontrollers.

Something I have learned in 25 years of engineering is that you need to get your feet wet and hands dirty as quickly as possible. You'll learn more from your mistakes and from the obstacles that you'll face than from any book. Or in essence, start writing code as soon as possible.
If you can write a decent sketch, then there is no reason you should not be able to start writing a library.

I think most agree that C++ and C are totally different languages, with C being a small subset of the object oriented C++.
 
Learn C on a PC. Master that before undertaking C on a microprocesor.
What? Standard C is going to be the same, whether it's on a PC, or on a microcontroller like the Arduino or a Teensy 3.1 (the one I use). Sure, there are things that are going to be different, but these are not difficult to deal with in most cases. Pointers are not going to be handled any differently in either context.

All we really have for small memory (Kilobytes, not Megabytes) microprocessors is C and C++, where the latter can and does get out of control in terms of needless abstraction forced upon us by those with a C# background.
Why are you blaming this on people with C# background? This can just as easily be blamed on those with C/C++ background.

Straight C makes the code self-obvious.
No, I don't think so. Just because code is written in straight C, it doesn't mean things are going to be obvious. I've had to deal with some pretty badly written straight C code in my time as a developer, and things were definitely not obvious.

On larger/Linux based MCUs, there's Python which is a high level language and weird at first but kind of a refreshing change from C++. And these large MCUs can do Java.
These more powerful MCUs that can run Linux (but don't have to) can do a lot more than just Python or Java. They can do Go, Ruby, and a host of other programming languages, just like Linux on a PC can do. I program in Python and Go for the Raspberry Pi, BeagleBone (Black), and PandaBoard (ES). I can use any programming language that has been built for the platform I'm dealing with at the time. Most of my Device Control System is written in Python, but I plan to rewrite it in Go at some point.

For the Teensy 3.1's I am using now, I just use TeensyDuino, since the code I have for my robots is written for the Arduino, and it's easier to deal with.

Go for the Arduino or Teensy (with TeensyDuino, of course). There are many fine resources out there, as well as knowledgeable people willing to help new folks out when needed.

8-Dale
 
I think most agree that C++ and C are totally different languages, with C being a small subset of the object oriented C++.

One of my favourite quotes (I forget where it's from): In C you write solutions, in C++ you model problems :)
 
Last edited:
Hello,
I've come to a point where's I'd eventually like to write my own Arduino/Teensy libraries and was wondering if there was a good book to learn C/C++ that is micro controller friendly ? I already know a lot of Python (classes, oo programming etc) but would need to translate this knowledge to C/C++ in the context of Arduino.
You may be mixing two things up here, or at least confusing them a little. :)

There are two distinct parts to working with an Arduino/Teensy with TeensyDuino. One is the C/C++ language used to write the code for them. The other is the IDE and build system that actually builds and loads your compiled code into the processor to run. Now, you don't really have to deal with the build system much, unless you want to write libraries, like you want to do. There are differences here that will affect how you write your code and make it ready to be used in sketches.

I just bought a book called "C++ Primer Plus (Sixth Edition)," by Stephen Prata. While I haven't gotten into it yet, it looks like it will be a useful book. There are also many books that cover the various aspects of the Arduino, including writing libraries. A single resource will not get you where you want to go in this case. Make use of the great resources available at the main Arduino website - I still go there regularly and always find new stuff to work with.

The book I'm reading now is "Beginning Sensor Networks with Arduino and Raspberry Pi," and I also have "Pro Arduino." :D I'd suggest getting an Arduino book that covers the area you want to work with it in, as well as a good C/C++ book.

These are just some ideas for you to consider. :) You can generally apply whatever you learn about a regular Arduino to a Teensy using TeensyDuino. I am amazed at what Paul has done, and is doing, with TeensyDuino.

8-Dale
 
I'm far from being an expert in C/C++. But, I went from not knowing really what a function or parameter was to to writing and improving/correcting libraries in less than a year. (I'm still very much a work in progress!)

I have the K & R book for C and C++ Reference Manual by Ellis & Stroustrup (both Bell Labs). I think I actually learned a lot more by just figuring out other libraries. I also have ADD, can't sit still very long, and learn more by doing something than just reading.

I found that many open source libraries I wanted to use had practically no documentation. So, I decided to create my own....so I could use it. I found sites like cplusplus.com and stackexchange.com, went through the tutorials to get a nice overall intro. Then, opened up an existing library and starting making my own documentation for it...line by line. If something didn't make sense...jump back to the websites (or google), search for it, figure it out, document it....next.

The real key bits to try to figure out are not only what the code does and why it's there....but, also trying to document/note why the author did it that way, identifying any other methods which might have worked, and documenting other questions those 'answers' made you ask. And, if you cannot find the answers anywhere...try reaching out to the author. But, I found myself learning a lot of new things I didn't even know I didn't know while I was trying to find a specific answer for something else.

This may not work for everyone, and it can easily lead to many hours worth of tangent searches. But, in the end...you do learn a lot, the library gets much needed documentation, and the entire open-source community could benefit from your hard work.

Good luck!
 
Last edited:
there's a trend to put zero comments in code today.
Professionals do comment for the reader (which may be themselves a year later), and do structured comments for tools like doxygen.
 
there's a trend to put zero comments in code today.
Professionals do comment for the reader (which may be themselves a year later), and do structured comments for tools like doxygen.

I actually have went back to code written over a decade back. Commenting properly is more important than meaningless comments.
You should:
Document each function
Document complex things
Document limitations

If your code follows some standard, such as an RFC, or is pertaining to some sort of particular hardware, you should comment on what documentation your code is based on, even including what section of the document. Paul does this wonderfully in his code, and is a very good example of useful comments.
 
I've found that if you follow popular use of doxygen for functions' purposes, inputs, outputs, and globlas, plus a mainpage overview, it's best.
And time taught me to comment line by line where I'm doing something non-obvious. This what I really dislike about C++, with the function and operator overloading that's done and nary a mention by the programmer. Some people must think it's cute. When I was a supervisor of such, I'd tell folks it'll cost them $ in their raise/reviews for being cute, non-obvious, skimpy with comments and docs. Because later, this terseness to a fault cost the project lots of unproductive time. Or worse, someone tosses out all the code and starts anew, cursing the original author.

Same stuff happens on a smaller scale, on a hobby project that winds up being long-lived.
 
Last edited:
Status
Not open for further replies.
Back
Top