6

I am trying to understand when a developer needs to define a C variable with preceding '_'. What is the reason for it?

For example:

uint32_t __xyz_ = 0;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1041588
  • 79
  • 1
  • 3
  • 6
    You'll have to ask the author – David Heffernan Nov 11 '11 at 11:31
  • Welcome to StackOverflow! The first thing to realise is that this website is *not* a forum. It's a question/answer site. Come with a good question, and be prepared to accept an answer if you find one satisfactory. – Kerrek SB Nov 11 '11 at 11:32
  • Possible duplicate of [Why does C have keywords starting with underscore](https://stackoverflow.com/questions/56211904/why-does-c-have-keywords-starting-with-underscore) – klutt Jun 17 '19 at 18:17

3 Answers3

12

Maybe this helps, from C99, 7.1.3 ("Reserved 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.

Moral: For ordinary user code, it's probably best not to start identifiers with an underscore.

(On a related note, I think you should also stay clear from naming types with a trailing _t, which is reserved for standard types.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • The `_t` reservation is from POSIX isn't it? – Flexo Nov 11 '11 at 11:42
  • @awoodland: I'm not 100% sure. I'd appreciate if someone could say where this rule comes from. That said, `*_t` types are so ubiquitous in the C standard that it's probably good to stick to that policy. – Kerrek SB Nov 11 '11 at 11:47
  • 1
    they key bit is the "any header" `_t` in the second table of: http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html – Flexo Nov 11 '11 at 11:53
  • 4
    And corollary: a developer needs to define reserved names only when implementing the C language (and libraries). So if you're not contributing to a compiler or stdlib, that's not you. – Steve Jessop Nov 11 '11 at 11:59
  • What does the second bullet point mean? Can we use those? – user129393192 Jun 09 '23 at 06:27
10

It is a trick used in the header files of C implementations for global symbols, in order to prevent eventual conflicts with other symbols defined by the user.

Since C lacks a namespace feature, this is a rudimentary approach to avoid name collisions with the user.

Declaring such symbols in your own header and source files is not encouraged because it can introduce naming conflicts between your code and the C implementation. Even if that doesn't produce a conflict on your current implementation, you are still prone to strange conflicts across different/future implementations, since they are free to use other symbols prefixed with underscores.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • 1
    It's a reserved name, reserved for the implementation. Sounds like a good way of seeing a strange problem in the future. – Flexo Nov 11 '11 at 11:43
  • @awoodland: I fail to see a justification for the downvote. I'm not encouraging anyone to adopt this naming convention in their own header files. – Blagovest Buyukliev Nov 11 '11 at 11:51
  • I disagreed with the "in order to prevent conflicts" and "a rudimentary approach to avoid name collisions" - it doesn't prevent conflicts at all, in fact it introduces a whole new class of possible conflicts with a perfectly legal implementation. I would prefer to see a clarification along the lines of "a flawed attempt at preventing conflicts" to make that explicit. Currently it reads like a weak recommendation. – Flexo Nov 11 '11 at 11:58
  • Upvoted now although some of the single underscore identifiers can also be reserved depending on where they're used. (you had a double underscore in the example at the end) – Flexo Nov 11 '11 at 12:27
0

whether its C or not, the leading underscore provides the programmer a status indication so he does not have to go look it up. In PHP, or any object oriented language where we deal with tens of thousands of properties and methods written by 1000's of authors, seeing an underscore prefix removes the need to go dig through the class andlook up whether its declared private, or protected or public. thats an immense time saver. the practice started before C, i am sure...

James Danforth
  • 781
  • 7
  • 7