What’s your favorite, C or C++? Which one is better? Especially in embedded system. Why?

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.[2] 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.

  • Classes
  • Templates
  • Streams
  • 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.[5] 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++.

[1]The C++ Programming Language, 2nd Ed, Bjarne Stroustrup
[2]http://unthought.net/c++/c_vs_c++.html
[3]Object-orientation in C – http://stackoverflow.com/questions/415452/object-orientation-in-c
[4]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
[5]Arduino Uno – https://www.arduino.cc/en/Main/ArduinoBoardUno

Advertisements
This entry was posted in C, C++, Technical Interview and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s