Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microcontrollers using C or C++ [duplicate]

Possible Duplicate:
Is there any reason to use C instead of C++ for embedded development?

I'm very curious about this: Why is it that when we deal with microcontrollers, they prefer C instead of C++? Based on my researches, C and Assembly language is the usual programming language for these devices. I only know C++ and Assembly Language. So in this case, should I start learning C or stick with Assembly language and if so, what compiler should I use because I only know the Turbo Assembler.

Thanks and more power! :)

like image 392
Ju-chan Avatar asked Dec 18 '11 10:12

Ju-chan


People also ask

Do microcontrollers use C or C++?

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.

Do microcontrollers use C?

all MCUs have a C compiler, the basic C language does not support every instruction on every processor.

What language is used in microcontrollers?

C or C++ are frequently used in microcontrollers and in embedded devices that use real operating systems. Those systems also demand the speed and efficiency that C and C++ provide. You'll also find C and C++ in several other embedded systems.

How do you copy a microcontroller?

The chip cloning process is not too difficult. The idea of cloning or copying a microcontroller chip is to extract out the machine code from the original microcontroller and write the same codes to a new microcontroller chip. The machine code is also sometimes known as the hex code.


3 Answers

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.

like image 70
David Brown Avatar answered Oct 16 '22 10:10

David Brown


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.

like image 23
Pete Wilson Avatar answered Oct 16 '22 10:10

Pete Wilson


It's just the availability of resources, really, as explained by the other posters. By the time you've compiled in a couple virtual method tables and a couple dozen object pointers, that's all the RAM gone from a simple uC!

That said, I prefer C++ on today's 32-bit controllers with 8K upwards of RAM, plenty of flash, complex embedded peripherals and multitasking libs. After decades of OO, using plain C is nightmarish for anything non-trivial.

I currently use NXP ARM chips & Rowley Crossworks, (IDE, uses gcc). I only use C for lib interfaces and assembler for some drivers, all the rest is C++.

like image 20
Martin James Avatar answered Oct 16 '22 08:10

Martin James