5 Reasons to start using C++ over C

More than a decade ago there was a constant debate and anticipation as to when C++ would finally eclipse C and become the dominant embedded systems programming language. To this day, C still remains the dominant language and doesn’t seem to being anywhere anytime soon. Given the advances in microcontroller hardware and more importantly, the toolchains and the optimizations that are now available with them, it may be time to start transitioning from using C to using C++. Here are several reasons why developers should start considering using C++ in their embedded systems.

Reason #1 – Use modern object oriented programming techniques

The C programming language is a procedural language that was invented almost 50 years ago. It’s a great language but it lacks everything that a modern programming language has such as
• Encapsulation
• Inheritance
• Polymorphism
C developers can sometimes emulate these foundational object oriented features but it’s always forced and requires more effort. A modern language naturally uses these techniques and if used properly can improve code readability, reuse and portability. In today’s complex systems, it sure would be nice to use classes and objects that can then be reused from one application to the next.

Reason #2 – Compiler and toolchain support for microcontrollers

Over the last few years, the support for C++ in compilers and toolchains in the microcontroller space has been a heavy focus for tool providers. Examine a few commercial and open source compilers and you’ll quickly discover that the latest C++ standard, C++14, is fully supported by the compiler. Examine those compilers for the latest C standard, C11, and you’ll be lucky to find a compiler that supports even a few of the latest features.

Beyond just compiler support, microcontroller manufacturers are starting to include hooks in their own tools to be able to easily develop C++ applications. There seems to be a quiet storm brewing where C++ could quickly become the dominate language in the embedded space.

Reason #3 – Active standards committee

The C++ standard is updated far more frequently than the C standard. The C standard was updated in 1999 and 2011. The C++ standard has been updated four times already with another version expected out in 2017 and every three from then. The updates aren’t necessarily just improving C++ but updating features and adding new features to stay current with the changes that are happening in industry. Despite all the issues, ambiguities and known issues with C, changes, updates and clarifications come at a snail’s pace if at all.

Reason #4 – Performance and code size

The greatest sticking point in the old debates between whether C or C++ should be used in an embedded system came down to performance and code size. The complaint was always that C++ code was larger and performed worse than C code. In today’s development environment, modern compilers and their optimizers are very good. Once again, a developer might need to experiment a little or limit their C++ language use to a subset that can guarantee performance but let’s be honest, we have to do the same thing with C anyways!

Reason #5 – Object oriented programming is taught everywhere

Everywhere, object oriented programming techniques are taught. University students learn either Java or C++ in their computer science classes. Software design more naturally adheres to object oriented thinking rather than procedural processes. New engineers are more likely to pick up and become productive faster using C++ than C because it naturally fits into what they have been taught during their four or five years in college. C will seem an old language that they have no way to relate to and that they will need to learn and we all know that C is a very quirky language. Those quirks take years to learn and master.

Conclusions

The C programming language isn’t going anywhere anytime soon; However, the tools available to developers to start using C++ and beginning the transition are available. C++ offers developers an opportunity to start using object oriented methodologies while at the same time, if necessary, continue to use legacy C code. There are many reasons to start using C++, and plenty to continue using C, but don’t be surprised in the next few years when more and more example code starts showing up in C++.

Share >