How to Learn C/C++ for Microcontroller Programming?

Sebastian - Oct 11 '21 - - Dev Community

Every Arduino programmer starts with writing the first LED blinking sketch using the C programming languages. You understand basic imperative commands to define output pins and to change pins. You also know what functions are, and how the setup() and loop() methods interact. And from there on, you can use the full power of the C and C++ programming language.

The question "Which language should I use?" involuntary leads to "What’s the difference between C and C++?" and "Which features are available in these languages?".

In this blog post, written autumn 2021, I provide a personal snapshot to answering these questions. I reflect my choices so far, my method of learning languages, and anticipate which language path I will choose.

This article originally appeared at my blog admantium.com.

Past Experiences in Language Acquisitions

I started to learn programming with object-oriented Java at university. A two semesters course for the language, libraries, and lots of assignment. Later into my studies, during a visit in Japan, I was introduced to Ruby. This fascinating language -at the time of the famous pickaxe book, and when Rails got attention - was so much fun to play around with. I literally typed every example into my keyboard, I played with the language to get to know it by heart. In my working career, I extended into web-development, plain JavaScript and React. And then, in this year, I got interested into Arduino, which ultimately leads to C and C++ (and maybe Assembler?!).

First Steps with C

Reading and understanding the Arduino C programs was quite easy for me, at least until I checked into the library code, for example the controlling of a Motor or a LED matrix. At first, I became lost in the chaining of so many * and & (pointers, yup!), wondered why strings are arrays, and became confused by ifndef header definitions. So, my first instinct was to grab a well-known book - C in a Nutshell. I learned the language, but became also distracted by so many implementation details, like the 16 or so different signed, unsigned, long, short chars, words and int. But at the same time, I was digging again into the library code to get my robot working. By reading forum posts about the libraries, and by checking what the lib code statements too, my confusion turned to fascination. C is close to the hardware, close to the inherent abstractions of computers. After 5 weeks, my first prototype was done. Yet I felt not confident to actually write a C program from scratch, was still relying on copying example together until it works.

What was I missing? I understand the language, and can read elaborate source code. But I cannot produce it. Whatever project I'm working on, I want to understand, learn and apply.

How to Learn?

So, back to the question again: How to learn a language? The first step is basic understanding of the syntax, the keywords, and the program structure. This is the general frame in which you operate. The second step is to use the language, to program on your own. You do this best by challenging yourself with small tasks (e.g. exercises) and by playing with the language (explorative programming). The next step is to acquire best practices and use them in the programs again. And finally, you can look into exhaustive language references, learning the ins and outs of the language.

Now, how do you like to lean? I still like to use books, as they give me the best structured knowledge about a subject and it feels good to finish a book - its something that you achieve. Breaking my approach down t books gives me the following list.

  • Syntax Reference: Start with a book that teaches you the languages syntax. I highly recommend to check Oreilly's Pocket Reference Library or Books from Apress; They are short, less than 120 pages, and you can work through them on a single evening. But do not just read the book, but take each example and type it into your computer. Feel free to experiment with the language if you want to try out different things. Reference: Modern C Quick Syntax Reference
  • Language Tutorials: Language Tutorials are weighty complete books, often written for beginners that teach things from bottom up. We will use these books to get language practice. Therefore, head straight to the exercise section in each chapter, and start coding. If you cannot complete the exercise, go back into the capture and read what is new or familiar for you. After working through this book, you have a solid background in the language. Tip: If you already know the application domain in which you want to use the language, pick a book with corresponding exercises
  • Best Practices Book: While working on your project, grab this type of book. They will help you to learn how to best use the programming language, and they will also help you to get up-to-data with the latest language changes. Many programming languages are updated with new versions and new language features. Tutorial books are typically older, but Best Practices books bring you up to date.
  • Language References: The final type of book is an additional addon. If you really like to learn the ins and outs of a language, take a language reference. These books detail every aspect of the language, including detailed explanations of the types, memory management, and extensive descriptions of the core language libraries. Use this book either on its own, or dip into it when you are struggling with a particular problem in your application

My Approach to Learning C

Lets see this approach in practice. When learning C, I used these books.

  • Syntax Reference
  • Language Tutorials
    • Modern C: Written in 2019, it contains the latest standard. This book addresses beginner and experienced programmers as well. Its well structured and peaks behind the Scenes of C to teach you the language.
    • Beginning C: The 6th edition of this books is written in 2020. It’s a comprehensive book targeted at beginners, showing and explaining programs over several pages to provide a deep understanding.
  • Best Practices
    • 21st Century C: A treasure trove of information. Written informingly and entertaining at the same time, this book brings your C usage up to date. Because of its long history, many C code is often complicated and verbose - this book teaches you how to become better!
  • Language Reference
    • C in a Nutshell: A classic from 2005 that explains every detail of C, including 200 pages for every standard library function.

Conclusion

This article covered the essential question "How to learn a new programming language?". For me, this question is answered by a 4-step approach: familiarity, practice, application development, best practice & reference learning. First, you start to type small and medium programs, often just repeating example code. You become familiar with the language. Then you practice, you choose a set of exercises, ideally from the target domain that you want to program in. When completing this, you know the language and can solve problems. Then you continue with application development, starting with a self-directed goal. In addition, you get up-to-date knowledge about best practices, and apply them in your project. These phases can be accompanied by carefully chosen learning material - I prefer books, but online tutorials and videos can be beneficial too.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player