2

This question pertains to C. There have been many questions already on the topic of double underscore variables, e.g.,
What are the rules about using an underscore in a C++ identifier?
Why do people use __(double underscore) so much in C++
Meaning of double underscore in the beginning

As I understand thus far, __myvar is reserved for use by the compiler. However, I am unsure where to find the precise meaning for such variables, and how to use them.

For example, I am studying this block of code:

#define fatalError(...) { char str[1000]; sprintf(str, __VA_ARGS__); printf( "%s (%s in %s, line %d)\n", str, __func__, __FILE__, __LINE__); exit(EXIT_FAILURE); }

For instance, it's not clear what __VA_ARGS__ refers to. Since it's reserved by the compiler, and I'm using gcc, should I be searching for __VA_ARGS__ in gcc documentation?

Community
  • 1
  • 1
DDC
  • 115
  • 4

2 Answers2

2

It's a variadic macros. It allows the function to take variable ammout of arguments. You can read more about functions with variable arguments here.

Vladyslav
  • 786
  • 4
  • 19
  • 1
    Thanks Seprum - I'm assuming this is gcc-specific, i.e. if I tried compiling this on a Microsoft compiler, it wouldn't work, right? – DDC Apr 13 '16 at 23:51
1

There is no general rule for determining the meaning of an identifier (not necessarily a variable name) whose name starts with two underscores (or with an underscore and an uppercase letter; those are also reserved to the implementation).

Some such identifiers are defined by the C standard. Examples are __FILE__, __LINE__, __STDC__, __STDC_VERSION__ (all predefined macros) and __func__ (not a macro but an implicitly declared identifier in each function).

You can find a draft of the C standard at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf. Note that the PDF encodes double underscores in an odd way so they're displayed properly, so a search for __STDC__ will likely fail. You can search for _STDC_ instead.

Any such identifiers not mentioned in the standard may be defined by the implementation (either the compiler or the runtime library). Consult your implementation's documentation. Be aware that some such identifiers might not be documented if the implementation uses them only for its own internal purposes.

As for __VA_ARGS__, that's defined by the C standard, in section 6.10.3.1. It's used in variadic macros (macros that can accept a variable number of arguments). It's also documented in the gcc manual.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thanks, Keith, for the thorough explanation. Even though the gcc manual is specifically on gcc, a large part applies to other popular compilers, too, right? I'm reading the part on the preprocessor and it seems the behavior is similar for the Microsoft Visual Studio compiler. – DDC Apr 14 '16 at 00:18
  • 1
    @DDC: The section of the gcc manual that discusses `__VA_ARGS__` clearly says that it's an ISO C standard feature. gcc has another mechanism for variadic macros with a different syntax; it predates C99. Some compilers (clang, for example) are deliberately compatible with gcc; MSVS is not. – Keith Thompson Apr 14 '16 at 00:20