没有参数的#define指令有什么作用?

Chr*_*ton 2 c header-files c-preprocessor preprocessor-directive

在Apple的开源网站上,stdarg.h条目包含以下内容:

#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
Run Code Online (Sandbox Code Playgroud)

如果第一个参数后面没有任何内容,那么#define语句会做什么?

Joh*_*nck 6

预处理器中的标识符有三种可能的"值":

  1. 未定义:我们不知道这个名字.
  2. 定义但是空:我们知道这个名字,但它没有价值.
  3. 定义,有价值:我们知道这个名字,它有一个价值.

第二个,已定义但为空,通常用于条件编译,其中测试仅用于标识符的定义,而不是值:

#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
Run Code Online (Sandbox Code Playgroud)

这是一个名称的示例,如果它被定义将具有值.但是还有其他的,比如NDEBUG,它们通常根本没有定义(-DNDEBUG通常在编译器命令行上).

  • 它是C预处理器的一部分.你可以在这里阅读`defined()`:http://gcc.gnu.org/onlinedocs/cpp/Defined.html (2认同)