-1
$\begingroup$

I want to build robots, and right now I aim to work with Arduino boards I know that they are compatible with c and c++, so was wondering which language is better for robotics in general?

I know how to write in java, and the fact that c++ is object oriented makes it look like a better choice for me

does c have any advantages over c++?

$\endgroup$
2
  • 1
    $\begingroup$ stick with C for embedded systems and C++ for robotics in general. $\endgroup$ – CroCo Dec 6 '15 at 16:36
  • 1
    $\begingroup$ any answer would be opinion based. Here is mine. The C language has very little overhead except library calls and is very fast. The C++ language has lots of 'hidden' overhead, is slow to perform context switches ( in comparison to C ) and is not nearly as fast as C. Also C++ uses a lot more memory. C++ can be much quicker to code. So which to use depends on things like memory available. time frame to write/debug the code, available execution time $\endgroup$ – user3629249 Dec 6 '15 at 20:21
1
$\begingroup$

As the comments indicate, the answer is really based on what you want to do with the programs you write. There are a lot of benefits to the object oriented capabilities of C++. But the inheritance, overloading, etc., does come with a performance penalty. C++ compilers optimize quite well, and for most tasks you will be fine with that - especially since you already know java. But if you need to do fast, low-level tasks, you will never beat the efficiency of pure C unless you write assembly code directly.

Do you want to write low-level communication, data acquisition, or control code? Then choose C. Do you want to write user applications that run on top of the low-level driver stuff? Then C++ would be easier for a java programmer.

$\endgroup$
0
$\begingroup$

Most microcontrollers have good C++ compilers now.

Most C idioms can be written in C++.

Templates, function overloading, and structure member functions allow one to write modular, readable code without classes.

Interfacing to other libraries can be done with extern "C".

Much high-level software (image processing, ROS, etc) is written in C++.

If you pick one systems language for robotics work, pick C++.

If you are working close to the metal, you must learn and understand what C++ is doing behind the scenes.

The only advantage C has is that there's lots of legacy code using C, especially OS code, and occasionally there is obscure or old hardware without a C++ compiler.

$\endgroup$
1
  • $\begingroup$ One important difference between the two languages (in the context of low-level programming) is that C++ executes "hidden code". Constructors and destructors are an example, as well as overloaded operators. Of course, there is nothing you can't know about, but it makes it quite easy to accidentally call functions you don't need, or shouldn't call in a particular context. $\endgroup$ – Shahbaz Apr 11 '17 at 20:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.