Question

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.

Was it helpful?

Solution

  • 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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top