next up previous contents
Next: 參數化巨集 Up: C 預處理器 Previous: 預定義巨集   Contents

預處理運算子

  1. Macro Continuation (\):延續巨集至第二行。
    #define  message_for(a, b)  \
        printf(#a " and " #b ": We love you!\n")
    
  2. Stringize (#):轉換巨集參數為字串
    #include <stdio.h>
    
    #define  message_for(a, b)  \
        printf(#a " and " #b ": We love you!\n")
    
    int main(void)
    {
       message_for(Carole, Debra);
       return 0;
    }
    
  3. Token Pasting (##):組合兩個參數。
    #include <stdio.h>
    
    #define tokenpaster(n) printf ("token" #n " = %d", token##n)
    
    int main(void)
    {
       int token34 = 40;
       
       tokenpaster(34);
       return 0;
    }
    
  4. defined():回傳括號內的表示式是否被定義。
    #include <stdio.h>
    
    #if !defined (MESSAGE)
       #define MESSAGE "You wish!"
    #endif
    
    int main(void)
    {
       printf("Here is the message: %s\n", MESSAGE);  
       return 0;
    }
    



2015-01-16