가변인자를 사용하는 매크로 함수를 만드는 방법...
Support for Variadic macros is now available. Consider:
#include <stdio.h> // In C89/90, you need multiple macros with "funny" names to do this. // Anyway, in this example, the arguments passed (those represented // by the ...) replace the __VA_ARGS__ when the substitution occurs. #define DoLogFile(...) fprintf(stderr, __VA_ARGS__) // Variadic macros also help out if you want to effectively // pass one argument that contains commas #define MakeName(...) #__VA_ARGS__ // Of course, the macro can also take other args, so long as // the ... is last #define output(FILEptr, ...) fprintf(FILEptr, __VA_ARGS__) // This is not right, since this is NOT a variadic macro #define mac(arg) foo(arg, __VA_ARGS__) int main() { int bound = 99; int current_index = 88; int linenumber = 77; DoLogFile("Detected array out of bounds\n"); DoLogFile("Bound = %d, current index =%d\n", bound, current_index); DoLogFile("Missing semicolon on line %d\n", linenumber); output(stderr, "Detected array out of bounds\n"); char *p; p = MakeName(Mary); p = MakeName(Doe, John); p = MakeName(Martin Luther King, Jr.); // Combo two variadic macros DoLogFile("MakeName=%s\n", MakeName(This is a test, yes it is)); }
가변인자를 사용하는 매크로 함수를 이용하면, 손쉽게 디버깅용 함수를 만들어 사용할 수 있다.