What would be the pros and cons of using C++ over C for uC (AVR) programming ?
The correct question is not "C or C++" (with a one-bit binary answer) but "which C++ features to add to my C (and which C features to discard as a consequence)" (which has a whole lot of bits, which will probably vary with the particular project and chip).
There are some C++ features that you should defintely use because they correct 'mistakes' in C, and have no code or run-time overhead, like:
- bool type with true and false values (C has no single 'true' value)
- passing parameters by reference (instead of by pointer)
- using new/delete instead of malloc/free
- for( int i = 0; i < n; i++ )
- declare a variable immediately before the (first) statement that uses it
- local arrays that have a calculated size
Note that some of these features are making it into the newer C versions.
Some other features should probably be avoided on microcontrollers because they have a (sometimes big) overhead:
- run-time type information (RTTI)
- exceptions (IMO they jury is still out on this one)
- std::cout with its wonderfull << operators (this is not a language but a library issue)
Other features can be a big advantage, but will take some time to learn and apply properly, like
- virtual functions
So, to summarize: if you have the chance to use a C++ compiler by all means use it, and start using C++ features one by one as you find good use for them. Don't feel obliged to swallow all of C++ in one go.
In general C allows you to write code that compiles into a smaller/faster binary than C++.
C++ is a super-set of C. It gives you the powerful features of object oriented programming, which allows better reuse/scaling/etc. and becomes more important as the size and complexity of your software solution grows. This comes at the cost of higher overhead, both in the size of the generated binary, in the amount of required memory (RAM) for execution and in speed of execution.
Microcontrollers (uCs) usually run slower than full size microprocessor, and have limited code storage and execution memory. All of this makes C more suited for programming uCs. You will also find that most drivers/libraries/existing code for uCs (provided by vendors and community) are written in C. Although C libraries can be used in C++ programs, if you want to contribute back to the community, C is preferred for uCs.
Well, all you need is in C. Any C++ code uses standard C to accomplish tasks on microcontrollers. C++ might help you write "nicer", portable code, with classes and methods if you want to promote your work to hobbyists (See Arduino project), at the cost of greater overhead. But the embedded systems industry has survived this long on C, it just depends on your requirements. The AVR-GCC project has aliases for all registers and bits named in the datasheet, so the code is pretty readable.
...and if I may add, from perspective of primarily electronics hobbyist vs programming, fewer lines of code not only saves precious/limited memory space on uC but requires less current overhead,battery requirments(capacity, weight, size) and generates less heat...the latter can cause chip performance problems. All the above vital if we are to use thin, light smartphones which are to reliably provide many functions and services. Thus, software issues and hardware needs/limitations are linked. Humbly submitted.....
protected by Kortuk Oct 18 '12 at 14:03
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?