Engineers use the C computer language for a range of embedded systems. But experts say C++ can be a better option. I've gathered some tips from experts, such as my colleague Tino Pyssysalo, on when C++ can work especially well in an embedded system.
In this article:
Embedded C++ is a dialect of C++ that engineers developed in the late 1990s for embedded systems. Embedded engineers seldom use the language anymore. They do use C++ in embedded systems a lot.
Embedded engineers developed "embedded C++" for embedded systems with as low as 4kB of RAM memory and a microcontroller processor. The language tried to keep valuable C++ concepts while removing features that increased memory requirements and reduced the processor’s efficiency.
Embedded C++ attempted to keep the most helpful aspects of C++ for embedded systems while eliminating those that caused undeterministic CPU cycles or boosted memory requirements. Embedded C++, for instance, retained how C++ uses classes as a blueprint to create objects for object-oriented programming.
However, embedded C++ removed features that embedded engineers use today. Those elements include multiple inheritance, templates, exceptions, virtual base classes, and runtime type identification.
C++ works well for embedded programming because it sits close to the system hardware. C++ has pretty much everything that C does, but much more: an array of higher-level language features, including object-oriented programming and type-safe cast that helps to avoid unpleasant memory access errors.
Engineers will find that C++ has some disadvantages for embedded systems. The language is complex and difficult to learn. Memory allocations in C++ can also cause problems if engineers don't understand it.
Here are details on some disadvantages:
Advantages of C++ |
Disadvantages of C++ |
Its object-oriented programming, templates, and other tools make it easier to build reusable parts of code |
It is a complex language that’s difficult to learn |
Easily portable from one device to another |
Maintaining code can be challenging |
Deep C++ standard library provides engineers with tools to more easily create optimal code |
Can run more slowly—only if certain features are enabled that don’t need to be |
Extremely stable language |
Programmers must control memory manually, which can cause errors, bugs, and even crashes |
Once engineers learn C++, it’s easier to learn Python, Java, JavaScript, and others |
|
C++ is used widely, has a large support community, and updated often |
|
C++ has good tools for embedded systems with GUIs |
|
C++ is being frequently standardized |
Experts say myths have often existed about using C++ for embedded systems. Myths include that C++ performs too slowly. That’s not true with properly written C++ code.
Here are some C++ myths and what’s the real story:
Myth: C++ produces bloated machine code, and C++ operates too slowly for embedded systems
Reality: Engineers can avoid bloated machine code with C++, and the language can run as quickly and efficiently as C.
Beningo explains, “Engineers can create code bloat and have performance issues if they're not careful. There are capabilities in the language that if you don't use a subset of it, you can really destroy your performance in an embedded system."
The crucial point is to avoid using some C++ features that can cause that bloat and aren't necessary in embedded systems, Beningo stresses.
Myth: C++, as a whole, doesn't work well in embedded systems.
Reality: C++ can work very well in many embedded systems, Beningo says, "as long as the developers know what they are doing and carefully monitor performance and memory usage."
Some experts believe C++ is often better for embedded systems. Others believe C is a better choice. Most pros say your choice of language depends on the system and how you use the language.
Beningo believes C++ is often better in many embedded systems. "You can leverage reuse, scalability, polymorphism, inheritance to design and build more scalable solutions."
"There is no absolute truth,” Qt's Pyssysalo points out. “It really depends on the use case and what you are developing."
Learn more about the wide range of programming languages that engineers can use in various embedded systems. You can also get insights and advice on creating an effective embedded system design process.
Beningo believes C++ can work as well as or better than C in a wide range of embedded systems. C++ language's advantages are the templates, standard libraries, and similar tools that make it valuable in complex systems that engineers may want to rebuild across other formats and product lines. "If you're looking at a complex system and you say: We're going to scale it, we're going to use it across multiple product domains ... then you really should be using C++," he adds.
Both Beningo and Pyssysalo say engineers should learn both C and C++ if they want to work extensively in embedded systems.
"You need to know both: 80% of (embedded) systems are still written in C, so you have to know it,” Beningo advises. “But the remaining 20% percent is mostly C++."
Pyssysalo says he would definitely focus on learning C++ because when an engineer learns C++, they’re also learning most of C. "C++ is a superset of C. In learning C++, you have to know the C concepts,” he shares. “In the coming years, as engineers look to move among companies that produce embedded systems, knowing C++ will provide better markets for you to apply to a new job."
You can learn more about the range of skills an embedded engineer should learn for career success.
Experts say whether C++ is the right language for your embedded project depends on the details of your embedded system and what it needs to perform well.
Pyssysalo explains that the smallest microcontrollers with minimal memory in embedded systems often use C. However, embedded systems with powerful processors and larger memory limits—especially those with a GUI—are better candidates for C++.
Here’s an overview of where the various programming languages excel and fall short based on need. You can read a detailed comparison of the top 6 embedded programming software languages, including C and C++.
C | C++ | Java | Python / MicroPython |
JavaScript/ HTML5/CSS |
Rust | |
Strengths |
Embedded, bare-metal, IoT | Embedded, bare-metal, standalone apps, IoT |
Web, cloud | Cloud, data science | Web | Embedded, bare-metal |
Ease of development | ★★★ | ★★ | ★★★ | ★★★★★ | ★★ | ★★★★ |
Expressive power | ★ | ★★★★ | ★★ | ★★★★★ | ★★★ | ★★★★ |
Ease of maintenance | ★★★★ | ★★★★ | ★★★★ | ★★★ | ★ | ★★★★ |
Longevity | ★★★★★ | ★★★★ | ★★★ | ★★★ | ★ | ★ |
Runtime efficiency | ★★★★★ | ★★★★★ | ★★★ | ★★ | ★ | ★★★★ |
Library/module availability | ★★★★ | ★★★★ | ★★★ | ★★★★ | ★★★★ | ★★ |
Low-level interface | ★★★★★ | ★★★★★ | ★★ | ★★★ | ★ | ★★★★ |
Connectivity support | ★★ | ★★★/★★★★★ 1 | ★★★★ | ★★★★★ | ★★★★★ | ★★ |
Graphics support | ★★★★ | ★★★★★ | ★★★ |
★★★ |
★★★★★ | ★ |
Developer community |
★★★ |
★★★ | ★/★★★★2 | ★★★★ | ★★★★★ | ★★ |
★ is the lowest ranking while ★★★★★ is the highest.
1 Provides high level of connectivity and networking support when platform libraries included
2 Java Developer community exclusive of Android (low) and inclusive of Android (high)
Every embedded programming language has its advantages and disadvantages. And each language has different capabilities when it comes to handling the GUI requirements in some embedded systems. If your system needs a GUI, it's essential to understand a programming language's ability to make that work well.
Engineers who use C++ in embedded systems know that a major advantage of choosing that language is its extensive standard library. Qt includes a comprehensive set of C++ library classes to help you build an embedded system. It also offers a range of other features that make it the best solution to build embedded systems for connected devices with 3D and 2D user interfaces.
You can learn more about how to easily create a GUI for embedded devices using the declarative language QML and Qt Quick. Qt’s embedded product planning and requirements guide helps engineers understand how to approach building an embedded device. The handbook includes a detailed comparison of some of the top embedded software languages. You’ll also find a comparison table to help you choose the best technologies and tools to build your user interface.