I'm looking to write what I would imagine is a fairly common macro. I want to emulate the repeated "-v" options on many POSIX programs by defining a bunch of macros of the following form:
#define V1(str, ...) if(optv >= 1){printf("%s: "str,prog,__VA_ARGS__);}
int main(int argc, char* argv[])
{
// ... stuff ...
int i = 1;
V1("This contains a variable: %d\n",i);
}
// Output:
// ./program: This contains a variable: 1
where optv
counts the number of "-v" options found on the command line and prog
contains the program name (neither shown). This works well, but the problem is that I have to use a variable. V1("Output")
will generate a compiler error. I could always use V1("Output%s","")
but there should be a cleaner solution.