Вопрос

On Apple's opensource website, the entry for stdarg.h contains the following:

#ifndef _STDARG_H
#ifndef _ANSI_STDARG_H_
#ifndef __need___va_list
#define _STDARG_H
#define _ANSI_STDARG_H_
#endif /* not __need___va_list */
#undef __need___va_list

What do the #define statements do if there's nothing following their first argument?

Это было полезно?

Решение

There are sort of three possible "values" for an identifier in the preprocessor:

  1. Undefined: we don't know about this name.
  2. Defined, but empty: we know about this name, but it has no value.
  3. Defined, with value: we know about this name, and it has a value.

The second, defined but empty, is often used for conditional compilation, where the test is simply for the definedness, but not the value, of an identifier:

#ifdef __cplusplus
  // here we know we are C++, and we do not care about which version
#endif

#if __cplusplus >= 199711L
  // here we know we have a specific version or later
#endif

#ifndef __cplusplus // or #if !defined(__cplusplus)
  // here we know we are not C++
#endif

That's an example with a name that if it is defined will have a value. But there are others, like NDEBUG, which are usually defined with no value at all (-DNDEBUG on the compiler command line, usually).

Другие советы

They define a macro which expands to nothing. It's not very useful if you intended it to be used as a macro, but it's very useful when combined with #ifdef and friends—you can, for example, use it to create an include guard, so when you #include a file multiple times, the guarded contents are included only once.

You define something like:

#define _ANSI_STDARG_H_

so that, later you can check for:

#ifdef _ANSI_STDARG_H_
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top