Go Down

Topic: C++ in embedded (Read 7302 times) previous topic - next topic


Sep 17, 2012, 08:25 pm Last Edit: Sep 17, 2012, 08:28 pm by pico Reason: 1

your efforts to learn OO and Arduino at the same time will lead to endless frustration.

Actually, this raises another important practical issue for C vs C++ that crops up a lot here in these forums: Because C++ is such a large, and sometimes quite unwieldy language, the beginner can easily become completely overwhelmed and lost, not seeing the woods for the trees.

C, being a small and compact language, is much quicker to learn, and I would recommend anyone not familiar with either language to start by reading K&R, and only after a bit of success with plain old C, to move onto to the relatively vast terrains of C++.

And thanks for your vote! :-)
WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)


A well-written and structured pure C program for an embedded project will probably be just as maintainable and manageable as its well-written C++ counterpart.
I'm currently writing/porting an Arduino-style API to LPCs, because in LPC XPresso land the C compiler is free but the C++ compiler costs $256 I'm doing it entirely in C.

I think it's practical to write good OO-style code in C and I think that's what I'm doing, I would love some of the C++ features but that can't be helped in this case :(

Rob Gray aka the GRAYnomad www.robgray.com


Pico , very well done inclusive answer .

Many thanks
I will stick to C , bare C for embedded .



If I am not wrong the C++ is barely used in embedded
programming , C is the facto language.

So I want to ask : what is the real advantage of C++ classes
which is the main difference to C ?

'C' is only used by diehards. There's no advantage to using a C compiler instead of a C++ compiler unless the C++ compiler for your platform is rubbish (which barely happens these days as they all use GCC).

From the language point of view, objects can usually organize your work much better.

Example: Imagine you have 5 RGB LEDs connected, each with its own color and pin mapping. Each of those is clearly an object and will map nicely to a C++ struct.

You can program it in C with structs but every function to manipulate them will need a pointer to the LED's struct. Guess what? This is exactly what C++ is doing anyway, but neater.

Embedded programming is probably going to use an intermediate level between C and C++. It isn't going to use the full facilities of C++ (like STL, namespaces, RAII, etc.). You'll probably have more global variables than a purist would like, etc. That's fair enough because the programs being written are small.

If you restrict yourself to pure C then you lose that ability to choose a level. Things like LED objects are C++ but they make perfect sense. Why wouldn't you allow yourself to use them?

Advanced Arduino


Why wouldn't you allow yourself to use them?

We all do, but we have more than enough water under the bridge to decide when its a good idea or not. Someone picking up and Arduino and a learn C++ book at the same time is in a very different situation though -

why does my program crash when i use new ?

is Arduino C++ the string class kills my program ?

etc etc etc

Duane B

Read this
then watch this


Go Up