Microcontrollers using C or C++

Some C++ features like exceptions and virtual functions can add overhead to your program which is undesirable in highly resource constrained environments. This reduces the demand for C++ compilers on such platforms. It is also much more difficult to implement a C++ compiler than a C compiler. This difficulty plus lack of demand makes it so many micro-controllers only have C compilers available for them.

I would learn C for your micro-controller programming. It is not difficult to learn C after learning C++ and will be much easier to code in than assembly.


It is merely historical accident and practice (by old-time Luddites like me) that ucontrollers "prefer" ASM and C. If your compiler can compile C++ into ucontroller code, there's no theoretical reason that I know of why you should not use C++.

To me, it's much easier and more natural to use ASM and C but you can use whichever you prefer so long as your compiler (and linker, if you use it) can do the right thing; and your ucontroller has enough memory to accomodate the (perhaps bigger) compiled C++ code.


C is more low-level and does just exactly what you say. It is more adapted to low-resources environments such as micro-controllers.

C++ has some features which requires additional resources (such as OOP, exception, and so on).

Moreover the micro-controller does not have the same features as your computer's CPU. It could for example not support dynamic library loading and even for static libraries you're limited in size as your chip doesn't have many memory.

Usually, micro-controllers expose special input/output library, and the stdlib is not always available.

What you need is a cross-compiler for your micro-controller specifically. Then you can write your program in C and ASM.

If the chip supports it, you can re-compile the stdlib to use the standard C features, and then you can eventually (once again if the chip has enough resources) build a C++ cross-compiler and then the STL. Then you will be able to build C++ program on your chip, but the program will weight much more than the original C program.