Back when I was an engineering student and a budding young engineer, it wasn’t unusual to open (yes, physically open) the latest edition of Embedded Systems magazine and find articles that were contributing to the raging war between C and C++. Each side would present their cases as to why one language was better than the other. The C developers usually argued that C++ produced bloated and inefficient code while the C++ developers would argue that C++ could do everything C could, only better. Over the last several years, the rhetoric seems to have died away, leaving me to wonder “Is the war between C and C++ over?” and if so “Who won?”.
In order to try and get an answer, I thought the first place to look would be to see if there has been any change in the general popularity between using C and C++. One of my favorite sources for this type of comparison is the TIOBE Index. The TIOBE Index has been tracking the popularity of programming languages in general since around the year 2000, providing 20+ years of popularity data. If you look at the TIOBE Index from February 2020, shown below, you can see that except for a small period between 2003 and 2005, the popularity between C and C++ really has changed that much. In fact, if anything it looks like C++ has actually lost momentum when compared to C!
This might seem to suggest that C has won, but how do things look specifically within the embedded systems industry? If we go back and look at the embedded.com embedded industry surveys and examine the programming language usage data for C and C++, we find the following:
In all honesty, the above comparison to me generally looks pretty flat. Sure, you could say that the general trend since 2005 has been that C is trending up and C++ down, but the if I were to calculate the standard deviation in the measurements that trend is just in the noise. We could also say in the last five years C++ has been trending up and C has been trending down, but again the same issue.
So how does C and C++ seem to compare within industry? As I look out at general trends as it pertains to using C and C++, I feel that there are several worth noting. First, companies that sell microcontrollers and microprocessors generally are still providing their clients with software stacks that are written in C. I don’t see ST Microelectronics, Cypress, NXP, Renesas, Silicon Labs, and so on providing C++ software stacks to engineers, they provide all their code and examples in C. Second, when I start to look at commercial and open source software packages for RTOS’s, bootloaders, file systems, etc, the majority all seem to be written in C.
This quick look through industry might lead me to believe that C has won, but the answer unfortunately is not that simple. For example, look at popular software stacks such as Arduino and mbed. These software frameworks are completely written in C++ and are gaining more and more popularity with time. In fact, I’m seeing a trend where mbed is being leveraged more and more by the chip manufacturers and not just by hobbyists or electrical engineers looking to get something done quickly.
While the verbal and written war between C and C++ seems to have died down, C++ might actually be starting to gain some ground in embedded systems. There are several reasons why I believe that C++ is slowly growing in popularity which include:
- Continued improvements in the C++ standards
- Continued improvements in C++ compiler technology
- How easily C can be wrapped and used within C++ applications
- Access to classes, templates and other object-oriented constructs that are not included in C
- Availability of useful C++ resources such as:
- Real-Time C++ by Chris Kormanyos
- Effective Modern C++ by Scott Meyers
C++ has continued to evolve over the past decade, especially with major changes that occurred in C++ 11 and C++ 14. While C has been improved, for the most part the improvements have not been supported well and most teams working with C still use C99 as their tried and true compiler. I think a lot of the old arguments for why C++ doesn’t work in embedded just don’t apply anymore. These arguments often revolved around how C++ bloated code, was inefficient and just couldn’t access hardware as well as C. These just aren’t true anymore and may not have even been valid back then.
When C++ is used, care certainly does need to be taken. Developers can’t use every standard library feature, or they will potentially get inefficient, bloated code. They also need to be careful that any dynamic memory allocations are done during start-up so that they don’t run into memory issues like heap fragmentation. These are issues that even C developers need to be careful about, so they don’t preclude the use of C++.
I don’t think the war between C and C++ is over, but I do think that it has moved from a verbal and written battle to a battle of market share. At the end of the day, C and C++ will continue to be used in embedded systems for decades to come, locked in an eternal battle for dominance. With the growing capabilities of compilers and microcontrollers, along with a careful use of C++ features, there’s no reason that C++ should be immediately ruled out or excluded from being used in embedded development.
Jacob Beningo is an embedded software consultant who currently works with clients in more than a dozen countries to dramatically transform their businesses by improving product quality, cost and time to market. He has published more than 200 articles on embedded software development techniques, is a sought-after speaker and technical trainer, and holds three degrees which include a Masters of Engineering from the University of Michigan. Feel free to contact him at firstname.lastname@example.org, at his website, and sign-up for his monthly Embedded Bytes Newsletter.