3

I have read all the questions I can find about this, but nobody has yet addressed my confusion.

If I understand correctly, the use of a double underscore at the beginning of a method or variable name in library code is simply a namespace convention. It allows the operating system code to avoid clashing with any application code.

Why, then, does my /usr/include/string.h contain, for example, the following function declaration;

extern char *strcpy (char *__restrict __dest, __const char *__restrict __src)
    __THROW __nonnull ((1, 2));

Ignoring the __THROW and __nonnull ((1, 2)); parts, I have the following questions;

  • Why do they use double-underscore for the local variable names __dest and __src, when surely these are only ever reachable by the implementation (presumably in string.c or similar), and can never clash with my code. (as an aside, I never really understood why forward declarations needed parameter names, shouldn't the types alone be enough?)

  • Why is the function name not __strcpy? Does string.h not count as part of the operating system?

  • Why are the words const and restrict altered to use double-underscore? Aren't they already reserved words in the C99 (or earlier?) standard?

Clearly I've got my wires crossed somewhere along the line, if someone could clear this up for me that would be great.

lynks
  • 5,599
  • 6
  • 23
  • 42
  • 1
    The accepted answer for http://stackoverflow.com/questions/1449181/what-does-double-underscore-const-mean-in-c covers at least parts of your question. – Michael Apr 03 '13 at 13:52
  • @Michael thanks, this was one of the ones I was piecing information together from, but I will re-read it. – lynks Apr 03 '13 at 13:54

1 Answers1

3
  • The parameter names are with __ such that they don't clash with macros that a program might have defined. Another option would have been to omit the parameter names completely from the declaration.
  • It is strcpy because that is the symbol (of the C library) that is declared here
  • The const and restrict keywords are protected in case somebody uses a compiler that doesn't implement them, yet.
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Ahh so parameter names can be omissed from a forward declaration? And When would I see a function with a name such as `__myfunction`? – lynks Apr 03 '13 at 13:55
  • @lynks, yes the parameters names can be omitted. Functions as `__myfunction` would be "private" functions of the C library implementation that you are not supposed to use directly but that for one reason or another must be visible as external functions. – Jens Gustedt Apr 03 '13 at 14:03
  • Thanks, I shall continue to use these things until they click. – lynks Apr 03 '13 at 14:05