參數中有"..."其表示參數個數不定,"arg..."表示格式化參數列表。比如說printf一類的格式化輸出函數。
使用範例:
/*
* test.c
*
*
* Created by jackie on 2006/8/6.
* Copyright 2006 __MyCompanyName__. All rights reserved.
*
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PRINTD(func, f, a...) \
do { \
printf("%s", func); \
printf(f, ## a); \
} while(0)
#define PRINTS(f, a...) PRINTD(name_suspend, f, ## a)
#define DEFtest "DEFtest"
#define DEFtest1 "DEFtest1"
//使用時機 :
//如果函數的定義(實作)內容一樣,僅數個地方的變數定義不一樣時使用此種方式
#define FUNC(func_name) \
int func##func_name(int x) \
{ \
PRINTD("\nhello ","%s %d", DEF##func_name, x); \
return x; \
} \
#define test
#define test1
FUNC(test);
FUNC(test1);
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
PRINTD("hello", "%s", " jackie");
functest(0);
functest1(1);
return 0;
}
執行結果 :
Hello, World!
hello jackie
hello DEFtest 0
hello DEFtest1 1
macro_test has exited with status 0.