C For Everyone
C for Everyone, by Richard Man and CJ Willrich, is a new 400 page book covering the C language, with an emphasis on using it in embedded systems. I imagine a lot of embedded.com readers know Mr. Man, as he’s the principal behind compiler-vendor Imagecraft. The idea of a C book by a compiler writer appeals.
The book is well-written and almost chatty at times with a nice history of the language.
Section 1 is a short, high-level introduction to the C programming language, which is accessible to anyone wanting to learn about C. No real programming experience is required.
Section II is the real meat of the book. It provides a thorough description of C with pretty much all you need to know to build a system. There’s nothing about Windows APIs or the like; this is a book for an embedded wannabe.
It does a great job explaining types, and even has practical recommendations for deciding when to use a particular type. But, though the information is exactly correct, I disagree with the conclusion. MISRA and other standards mandate avoiding base types for all of the reasons we know, and I think the book would be better for providing that advice.
To be fair, there are lots of “Best Practices” suggestions, all of which are spot-on, and desired for C newbies. Indeed, the authors also offer valuable “Bug Trap” sidebars about oddities of the language many don’t know. For instance:
unsigned char uc =0xFE; if(~us == 0x1)…
… is bound to fail, for reasons they describe well.
Edsger Dijkstra would be horrified, but the authors describe when it makes sense to use the goto statement. They do warn that undisciplined use of the statement leads to spaghetti code. And I agree with them. While goto should be avoided, in certain (unusual) cases its use makes the code more comprehensible.
There’s practical advice, too. Two pages cover the rules for reading a complex declaration using the right-left-right rules. And there are some subjects you normally don’t see in a C book like variadic functions, recursion, and interrupt handling.
Newbies always get fouled up with pointers, but C for Everyone has a great chapter on the subject. I also recommend Understanding and Using C Pointers by Richard Reese
Oddly, with the exception of a few references, the book doesn’t cover Imagecraft’s fascinating library for Cortex-M parts. Their Jumpstart API is probably the second-fastest way to get started programming these parts. The fastest is mbed.org, which has a web-hosted tool chain so there’s no painful IDE installation required. But what makes mbed most interesting is that they have eliminated all of the peripheral programming we embedded people deal with. The Cortex-M parts have some quite complex I/O, and even the simple stuff often requires a lot of head scratching to set up. Mbed provides an API to tremendously simplify this. For instance, to configure a pin to be an input to an ADC and then read the analog, all you write is:
AnalogIn adc(p19); // Pin 19 is now an A/D input scale = adc.read(); // Read the A/D
The problem with mbed is that it doesn’t really scale for professional work. Sans IDE there’s no debugger so one is left using printf statements. And one must have faith that mbed will be around tomorrow; if their site disappears, so does your project.
Imagecraft sells a quite inexpensive ($99 for non-commercial work, $249 otherwise) traditional C environment with an API for Cortex-M parts that’s as easy to use as mbed’s. They support some ST parts today and plan to add many more. The book C for Everyone is part of the Jumpstart Microbox, a $99 kit including tools, the book (eBook), and an ST board with a debug link (this is for non-commercial work). Or, you can buy the eBook alone for $24.95. A printed version will be available through Amazon soon.
I do wish the book’s index were more complete (though the eBook version doesn’t really need one as it’s searchable; I was provided with an early printed book). And it’s not a beautiful work; the typography is rather pedestrian and there are few illustrations. But all-in-all this is an excellent work that will now be one of the standard suggestions I’ll give when people ask me that “how do I become an embedded programmer” question.
And they ask it a lot – 2021 times last month, which is about average.
What is your favorite book for embedded C?
Jack G. Ganssle is a lecturer and consultant on embedded development issues. He conducts seminars on embedded systems and helps companies with their embedded challenges, and works as an expert witness on embedded issues. Contact him at firstname.lastname@example.org. His website is www.ganssle.com.