• Tips and Tricks
  • Why do programmers use so many underscores in front of names?
2020/10/15 05:38:38
WeHongKongers
I observe that, in the embedded world, programmers use many underscore _ _ in front of function names, literals names, etc.
 
What are their reasons?
 
I find this naming style very ugly.
 
2020/10/15 06:19:59
Mysil
Hi,
macro symbols and function names starting with  one or two underscore characters, _...  or __...
are mainly those that are provided by and reserved for use by the 'system', that generally means the C compiler, and it's supporting libraries.
 
You should Not start symbols or function names with underscore for those names that you create yourself.
This is to reduce the possibility of confusion, by the same name beeing used for different purposes.
 
Read a C language textbook, or XC compiler User Guide, about: Reserved keywords and symbols.
 
Names starting with double underscore, like __builtin_...  or __delay_ms(10);
are extensions to the standard C language,
that are provided by the compiler, mostly for use with the actual microcontroller in use.
 
In embedded applications we often deal with hardware peripherals or specific processor properties,
that are supported by extensions to the basic C programming language. 
Some developers may prefer to wrap any and all code that deal with hardware dependencies,
into a interface layer that translate into your library or utility design.
Such that in application code, there will be no need to use any symbol or function starting with a underscore _.
 
    Mysil
 
 
 
 
2020/10/15 06:21:08
LdB_ECM
You need to be more clear about where you saw it but most likely you are looking at portable code that does name mangling.
https://en.wikipedia.org/wiki/Name_mangling
 
The key point is this
The mangling scheme was established by Microsoft and has been informally followed by other compilers including Digital Mars, Borland, and GNU GCC when compiling code for the Windows platforms. The scheme even applies to other languages, such as Pascal, D, Delphi, Fortran, and C#. This allows subroutines written in those languages to call, or be called by, existing Windows libraries using a calling convention different from their default.

 
It's fairly common to write and emulate complex code on windows before porting into the embedded target. The mangling also makes it easier to switch around between pthreads and RTOS systems.
2020/10/15 07:25:06
WeHongKongers
WOWAH!!!  A real long story for such a _ _!!!  Thanks to both of you!
2020/10/15 09:52:35
1and0
HKPhysicist
WOWAH!!!  A real long story for such a _ _!!!  Thanks to both of you!

The short story is ... names containing double underscore or beginning with an underscore and a capital letter are reserved for implementation (compiler, standard library) and should not be used by the developers.
© 2021 APG vNext Commercial Version 4.5

Use My Existing Forum Account