What is the best language for programming microcontrollers?

  • Context:
  • Thread starter Thread starter KonaGorrila
  • Start date Start date
  • Tags Tags
    Language Programming
Click For Summary

Discussion Overview

The discussion revolves around the best programming language for microcontroller applications, specifically comparing C, C++, and potentially other languages. Participants explore the benefits and drawbacks of each language in the context of sensor applications and embedded systems.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Exploratory

Main Points Raised

  • Some participants suggest that the choice of programming language depends on the toolset available for the microcontroller, with many embedded systems using a combination of C and assembly for efficiency.
  • Others argue that while C++ offers advantages for large systems and team development, it may introduce unnecessary overhead and is not typically suited for microcontroller software.
  • A participant points out that C++ is a multiparadigm language, allowing for various programming styles beyond just object-oriented programming, and its use in embedded systems is increasing due to tool availability.
  • Concerns are raised about C++ compilers for embedded systems being inconsistent, leading to potential issues with memory management and software bloat.
  • One participant mentions that while C is easier to learn, C++ can facilitate better organization of code and maintenance in the long run, especially for complex applications.
  • There are considerations regarding safety-related applications, where certain C++ features may be restricted, impacting the choice of language.

Areas of Agreement / Disagreement

Participants express differing opinions on the suitability of C versus C++ for microcontroller programming, with no consensus reached on which language is definitively better. The discussion remains unresolved regarding the best approach for specific applications.

Contextual Notes

Participants highlight limitations related to compiler support for C++ in embedded systems, potential memory management issues, and the need for assembly in certain circumstances. There are also considerations regarding safety standards that may affect language choice.

KonaGorrila
Messages
12
Reaction score
0
I am wondering what programming language, whether it is C, C++, or other, would be best for programming micro controllers. What are the benefits of either one. I understand C is easier to learn but does that actually make it better? I need to use these micro controllers for sensor applications if this helps.
 
Technology news on Phys.org
Depends on the toolset that is offered for the microcontroller. Most of the embedded systems I've worked on are written in a combination of C and assembly (mostly for stuff like interrupt handling. context switches by a kernel, ... ).
 
  • Like
Likes   Reactions: QuantumQuest, FactChecker, KonaGorrila and 1 other person
I believe we are using microchip mplab, Thanks for your response!
 
C++ is an object orientated language which greatly simplifies coding of large complex systems,
also it facilitates team development of software.
That is very unlikely to be the case for microcontroller software,
and it could even introduce unnecessary processing overheads and memory use.
Ideally microcontroller software could be most efficient if written in the native machine language of the processor.
However vanilla C is usually good enough with perhaps some critical routines written in assembly.
 
Last edited:
  • Like
Likes   Reactions: FactChecker
rootone said:
C++ is an object orientated language which greatly simplifies coding of large complex systems,
also it facilitates team development of software.
That is very unlikely to be the case for microcontroller software,
and it could even introduce unnecessary processing overheads and memory use.
Ideally microcontroller software could be most efficient if written in the native machine language of the processor.
However vanilla C is usually good enough with perhaps some critical routines written in assembly.

This is wrong. C++ is a multiparadigm language and has been for a long time. Consider the STL. That is not an object-oriented design. Some or none of the traditional OOP techniques may be used freely together with procedural and generic programming. There is increasing support for a functional programming style. It does not force the shoehorning of a design into class hierarchies like certain other well-known languages, and that style was left behind by people keeping pace with the language in the 1990s.

The use of C++ in embedded systems is increasing. As other answers identified, this has everything to do with availability of tools.

Unnecessary processing overheads and memory use, including fragmentation, have nothing to do with the language and everything to do with the available compilers and design techniques used. One is not forced to use every feature of a language, instead a general purpose language allows a wide range of problems to solved by selecting subsets of features. For example, in hard realtime applications encountered in embedded systems, exceptions are often inappropriate. The obvious solution is simply not to use them.
 
C is usually used for things like that because C++ compilers for embedded systems can be hit or miss, they will also rarely implement anything that they don't need, so you can probably expect std::string to work, but forget boost. C++ also tends to bloat software because it has to do little things like allocating vtables or doing memory alignment. I also prefer to manage my registers manually in some circumstance, so more often than not, you will still have to write some assembly.
 
  • Like
Likes   Reactions: QuantumQuest
KonaGorrila said:
I am wondering what programming language, whether it is C, C++, or other, would be best for programming micro controllers. What are the benefits of either one. I understand C is easier to learn but does that actually make it better? I need to use these micro controllers for sensor applications if this helps.
If C++ is available, it would be the best choice. It is not necessary to use all of the C++ language features or libraries. Simply having the "this" pointer will make it hugely easier to break up large functions (>200 lines) into meaningful pieces. And, of course, you have generally better support for OO design, encapsulation, etc.
If your application is safety-related, there may be restrictions. For example, automotive applications are generally forbidden from calling a function through a variable - so v-tables (virtual functions) would not be allowed.

Since C is nearly a subset of C++, it follows that it is easier to learn C. But in the long run, it is easier to develop in C++. And with experience, it will be easier to test and maintain your object-oriented code.
 
  • Like
Likes   Reactions: KonaGorrila

Similar threads

Replies
86
Views
3K
  • · Replies 25 ·
Replies
25
Views
1K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 133 ·
5
Replies
133
Views
12K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 397 ·
14
Replies
397
Views
21K
  • · Replies 54 ·
2
Replies
54
Views
5K
Replies
16
Views
3K
Replies
38
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K