2014-09-26 68 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")

被替換