
示例程序:
#include <stdio.h>
#include? <stdlib.h>
#define DEBUG(fmt, ...) fprintf (stderr, fmt, __VA_ARGS__)
int process (int i1, int i2)
{
int val;
#ifdef DEBUG
DEBUG ("process (%i, %i)\n", i1, i2);
#endif
val = i1 * i2;
#ifdef DEBUG
DEBUG ("return %i\n", val);
#endif
return val;
}
int main (int argc, char *argv[])
{
int arg1 = 0, arg2 = 0;
if (argc > 1)
arg1 = atoi (argv[1]);
if (argc == 3)
arg2 = atoi (argv[2]);
#ifdef DEBUG
DEBUG ("processed %i arguments\n", argc - 1);
DEBUG ("arg1 = %i, arg2 = %i\n", arg1, arg2);
#endif
printf ("%d\n", process (arg1, arg2));
return 0;
}
寫成__VA_ARGS__,使用宏時,可以用一個和多個參數(shù)。
如果要適用零個到多個參數(shù),應當寫成##__VA_ARGS__。