Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does everything in library headers start with an underscore?

I don't want to provoke a discussion or anything; I'm just curious if there is any specific reason why undersores are so commonly found at the beginning of names in C/C++ library headers. (for example _x, _y, __f(), etc)

like image 415
Mo Sanei Avatar asked Sep 18 '25 09:09

Mo Sanei


2 Answers

Both C and C++ reserve such names for the implementation, to avoid conflicts with non-implementation code.

This guarantees the implementation a "safe" space for its internal symbols that cannot be broken by conflicting third-party code, in a compliant program.

So it makes sense for the implementations to make use of that.


[C99: 7.1.3], [C++11: 17.6.4.3.2/1], "What are the rules about using an underscore in a C++ identifier?"

like image 183
Lightness Races in Orbit Avatar answered Sep 20 '25 23:09

Lightness Races in Orbit


From GNU manual

In addition to the names documented in this manual, reserved names include all external identifiers (global functions and variables) that begin with an underscore (‘_’) and all identifiers regardless of use that begin with either two underscores or an underscore followed by a capital letter are reserved names. This is so that the library and header files can define functions, variables, and macros for internal purposes without risk of conflict with names in user programs.

Also ISO 9899:2011 says that:

7.1.3 Reserved identifiers

Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

like image 24
Rahul Tripathi Avatar answered Sep 20 '25 22:09

Rahul Tripathi