Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it permissible for global, static identifiers to begin with a single _?

Tags:

c

In other words, may static (=file-scoped) globals begin with exactly one underscore without creating the possibility of nameclashes with the C implementation?

https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html says that "... reserved names include all external identifiers (global functions and variables) that begin with an underscore (‘_’) ..." (emphasis mine).

I've noticed the Linux kernel starts functions that probably shouldn't be called directly with double underscores.

I find this concept useful, and I find _(_)something slightly easier to read than something_ when it comes to distinguishing "private" identifiers, but I understand (?) double underscore invite name clashes with the C implementation.

Are global, static _variables and _functions safe?

like image 930
PSkocik Avatar asked Aug 17 '16 13:08

PSkocik


2 Answers

Standards reserves prefixes with an underscore1.


1 (Quoted from: ISO/IEC 9899:201x 7.1.3 Reserved identifiers 1)
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 135
2501 Avatar answered Sep 22 '22 17:09

2501


It is not safe. Depending on architecture your code may require some relocations even for static objects. As result their names must be visible to linker as well as names of CRT symbols. And this may cause clashes.

like image 21
Sergio Avatar answered Sep 23 '22 17:09

Sergio