2014-09-26 67 views
0
/* Debugging */ 
#ifdef DEBUG_THRU_UART0 
# define DEBUG(...) printString (__VA_ARGS__) 
#else 
void dummyFunc(void); 
# define DEBUG(...) dummyFunc() 
#endif 

我在C编程的不同头文件中看到了这种符号,我基本上明白它是传递参数,但我不明白这个“三点符号”被称为什么?宏中的__VA_ARGS__是什么意思?

有人可以解释它与例子或提供链接也关于VA阿格斯?

回答

4

该点称为与__VA_ARGS__在一起,复杂的宏

当宏被调用时,所有参数列表令牌[...],包括任何逗号, 成为可变参数。这个令牌序列将其所在的宏体替换为 标识符VA_ARGS

source,我的大胆重点。

使用的样本:

#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. 
1

这是一个varadic宏。这意味着你可以用任意数量的参数来调用它。三... 是类似于C

varadic function使用的相同结构这意味着你可以使用宏这样

DEBUG("foo", "bar", "baz"); 

或与任何数量的参数。

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

#define DEBUG(...) printString (__VA_ARGS__) 
      ^     ^
       +-----<-refers to ----+ 

所以DEBUG("foo", "bar", "baz");将与printString ("foo", "bar", "baz")

被替换