宏中的__VA_ARGS__是什么意思?

gan*_*bra 4 c gcc variadic-functions variadic-macros

/* Debugging */
#ifdef DEBUG_THRU_UART0
#   define DEBUG(...)  printString (__VA_ARGS__)
#else
void dummyFunc(void);
#   define DEBUG(...)  dummyFunc()   
#endif
Run Code Online (Sandbox Code Playgroud)

我已经在C编程的不同标头中看到了这种表示法,我基本上理解了它在传递参数,但是我不明白这种“三点表示法”是什么意思?

有人可以举例说明它还是提供有关VA Args的链接?

The*_*dis 5

点与__VA_ARGS__可变参数宏一起被调用

调用宏时,其参数列表中的所有标记 (包括任何逗号)都将 成为变量参数。此标记序列将替换宏体内出现的标识符VA_ARGS

资料来源,我的大胆强调。

用法示例:

#ifdef DEBUG_THRU_UART0
#   define DEBUG(...)  printString (__VA_ARGS__)
#else
void dummyFunc(void);
#   define DEBUG(...)  dummyFunc()   
#endif
DEBUG(1,2,3); //calls printString(1,2,3) or dummyFunc() depending on
              //-DDEBUG_THRU_UART0 compiler define was given or not, when compiling.
Run Code Online (Sandbox Code Playgroud)


nos*_*nos 5

这是一个杂色的宏。这意味着您可以使用任意数量的参数来调用它。这三个...类似于C 中的变量函数中使用的相同构造

这意味着您可以像这样使用宏

DEBUG("foo", "bar", "baz");
Run Code Online (Sandbox Code Playgroud)

或带有任意数量的参数。

__VA_ARGS__再次引用宏本身中的变量参数。

#define DEBUG(...)  printString (__VA_ARGS__)
               ^                     ^
               +-----<-refers to ----+
Run Code Online (Sandbox Code Playgroud)

因此 DEBUG("foo", "bar", "baz");将被替换为printString ("foo", "bar", "baz")