As one works their way up from the Arduino “blink” sketch into increasingly sophisticated projects, part of that process entails more exposure to others’ code…open source projects, libraries that encapsulate particular functionality and so forth. Such code originates from folks of all backgrounds and skill levels, and you will invariably encounter some head-scratching moments. Not necessarily bad code, but weird.
This guide explains a few lesser-seen elements of C syntax. “The Arduino language” is really C++, which is basically C with added flavor crystals. C was developed back in the early 1970s and has become one of the most widely-used programming languages. With such a long and diverse history, you’re bound to find some odd creatures under the floorboards.
There are a couple of reasons this might be of interest:
- To expand one’s own C (and C++, and thus Arduino) programming knowledge; maybe there’s valuable tips here to use.
- To write code that’s friendlier to newcomers; maybe these are things to avoid. If it confused you, it may confuse others, and that’s not ideal for teaching!
There are a number of ways that strange code might manifest…
- Familiarity with a programming language can lead to contractions, just as with spoken languages. In English, “is not” and “can not” become “isn’t” and “can’t.” C and most other programming languages have some shorthands that aren’t always obvious to new learners.
- With C’s long history, some of today’s projects are written by folks with decades of experience…who may have habits ingrained when disk and screen real estate were highly constrained, and who’s natural “flow state” is to always pack more code in fewer characters.
- Again with long-time programmers…even into the late 1990s, one was fortunate to get a free (and rudimentary) C compiler with a $30,000 UNIX workstation. Commercial optimizing compilers could add thousands more. So there was a tendency to attempt optimizations manually. Some of the code in today’s projects is carried over from posterity. Some is just written weird out of habit.
- Programmers have a wicked sense of humor. Obfuscation—intentionally writing to be difficult to understand—can be funny, and there’s even contests for such things. You don’t want this in teachable code, but occasionally one might drop a punchline for other veteran programmers. On rare occasion obfuscation is done with intent to be “31337” or to conceal trade secrets…but that’s petty, please don’t.
- Maybe I wrote it. If reading the source code to some Arduino library and it’s like “What were they thinking?”, check for my name on it. With the exception of malicious obfuscation, I’m guilty of all of these. Sorry about that.
This is not a comprehensive C programming guide; it only covers a few specific sticky topics that have raised questions in the Adafruit Forums. For well-rounded learning, there are many classic books on the subject, or just by following along with Arduino tutorials.
Nothing discussed here is broken or illegal. It’s all part of the formal C lexicon, just less-used…like the English language words “brambles” or “lugubrious.”