For decades there has been a debate among embedded software engineers as to whether they should be using C or C++. For the most part, software provided by MCU manufacturers is provided in C...
For decades there has been a debate among embedded software engineers as to whether they should be using C or C++. For the most part, software provided by microcontroller manufacturers is provided in C and in fact, 56% of embedded software is written in C according to the 2019 Embedded Market Survey. However, C++ has slowly been gaining popularity and approximately 23% of new embedded software projects are written in C++. With C approaching its 50th anniversary, it may be time to start transitioning from C to C++. Here are three reasons why developers should consider making the change.
Reason #1: Improved architectural implementation
The C programming language does provide developers with encapsulation and some basic polymorphism capabilities through function pointers. However, C is really a procedural language and does not provide mechanisms such as inheritance or composition without extra effort. This can make the overall architectural design of the system less ideal, which can then affect how the software will be reused and maintained and even limit its flexibility.
C++ provides developers with the ability to implement truly object-oriented design patterns, develop real abstractions and implement design patterns that can not only be reused from one application to the next but also improve an architectures flexibility. This allows the software to be reused, which in turn can result in faster product development, shorter customization times for client applications among many other advantages.
Reason #2: C++ is an evolving modern language
In today’s complex and innovative development environment, developers need not just cutting-edge hardware solutions but also cutting-edge software and language solutions. This means that the languages used to write software need to be up to date to provide developers the latest tools need to implement their systems. The last update to the C language was in 2018, which sounds like it has been updated recently. However, if you look at the standard, there were no new features added and the only changes were to fix issues from the previous release which was in 2011! Yes, it took seven years to put out a revised standard that fixed the issues from that last one!
Looking at C++, the standards committee is committed to update the standard every three years! The next revision is expected this year, 2020, with the last version being released in 2017. Unlike with C, the new revisions are not just bug fixes, but instead add new features and functionality to the language so that it can be current and provide the tools necessary to compete with other languages like Python. The new standards even remove deprecated functionality such as trigraphs.
Reason #3: C++ is a richer language
Most of the embedded software I have written for microcontrollers has used the C programming language up until maybe 3-4 years ago. C++ has always offered a richer feature set than C, but it seemed around that time that the compilers had reached a point where the compiled code was equivalent or better. That was when I started to dive into C++ and discover how rich the language had become and how much more there was for me to learn about the language.
C++ allows developers to take advantage of features such as:
These are just a few examples and there is so much more! All these tools make life programming an embedded system easier.
C++ is a modern and evolving language where C seems to be static and evolving much slower. In fact, C is becoming the proverbial dinosaur in an age when programming languages are rapidly evolving. As developers face ever increasing complexity within their systems, they need a language that allows them to be flexible and maximize reuse. Many complaints from the “old days” just no longer apply to C++ especially given how efficient C++ compilers have become. If there was ever a time to start transitioning to C++, this may very well be it.
Over the next several articles, we will look closer at C++ and how developers can leverage it to design and build drivers for microcontrollers.
— Jacob Beningo is an embedded software consultant, advisor and educator who currently works with clients in more than a dozen countries to dramatically transform their software, systems and processes.