Since moving to firmware development three years back, I’ve been using only C. The reason most people prefer C to C++ in programming embedded system is, IMHO, there are not much benefits we can earn from C++. Even worse, by writing firmware with C++, we have more chances to make mistakes and culprits, that end up with bigger excitable(ROM or RAM) size and low performance. This is mostly the result of misuse of C++, not because C++ itself is bad programming language.
C++ is superset of C. C++ offers new features which extends C and make better C. Bjarne Stroustrup who is father of C++ says in his book, ‘The C++ Programming Language’: The C++ programming language is designed to
- be a better C
- support data abstraction
- support object-oriented programming
In many cases it’s not impossible to write C++ code faster than C with even smaller excitable size. But to achieve this goal, we should be well aware of all pros and cons of C++ features.
These are additional features which C++ provides on top of C.
- Exception handling
- C++ Standard Library (STL)
Many small embedded system is designed to monitor and control HW. We can make Arduino to read the temperature using ADC converter and display it to 7 segment LED. We may consider to design the software to take advantage of C++, such as creating CTemp class as well as CDisaply to abstract 7 segment LED. We would expect to extend CDisplay to support HDMI or VGA in some day. Well, this may be OK if we have enough memory. Arduino Uno has only 2 KB SRAM and 32 KB flash memory with 16 MHz clock speed. It even doesn’t have OS. Function is getting called in ISR context. We probably don’t want to design the software this way for Arduino Uno. Blindly calling cout instead of printf would increase excitable size and give negative impact on performance.
In summary, C is probably good enough or better for embedded system as well as program for bigger system in case we don’t need of C++ features. If we have enough memory and clear reason to need C++ features, there’s no reason not to go with C++.
The C++ Programming Language, 2nd Ed, Bjarne Stroustrup
Object-orientation in C – http://stackoverflow.com/questions/415452/object-orientation-in-c
When to use C over C++, and C++ over C – http://programmers.stackexchange.com/questions/113295/when-to-use-c-over-c-and-c-over-c
Arduino Uno – https://www.arduino.cc/en/Main/ArduinoBoardUno