3

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)

Mo Sanei
  • 445
  • 6
  • 22
  • Reserved names include names that begin with an underscore followed by a capital letter (`_X`) and names that contain two consecutive underscores (`__x`, `x__`). – Pete Becker Dec 04 '13 at 17:08

4 Answers4

7

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?"

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

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.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

The library implementation should only use reserved names such as these, so they don't conflict with any names used in user code. Of course, that only works if the user knows the rules and avoids using reserved names themselves.

For (probably) more information than you want, see What are the rules about using an underscore in a C++ identifier?.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

If we look at the Rationale for International Standard—Programming Languages—C it covers this topic in 7.1.3 Reserved identifiers and says (emphasis mine):

Also reserved for the implementor are all external identifiers beginning with an underscore, and all other identifiers beginning with an underscore followed by a capital letter or an underscore. This gives a name space for writing the numerous behind-the-scenes non-external macros and functions a library needs to do its job properly.

This gives the library implementors a set of names they can use safely without fear of clashing with code that uses the implementation. The same identifiers are reserved in C++ as well.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740