1

I want to define a macro accepting either 1 or 2 parameters. Both the parameters should be different type. How to use ellipsis and read the arguments passed?

Below is the sample:

void test(char *var2) 
{ 
 printf("%s\n",var2); 
} 

#define PRINT_STRING(...) ( if (!var1) test(var2) ) 

int main(int argc, _TCHAR argv[]) {

PRINT_STRING(TRUE); 
PRINT_STRING(FALSE,"Hello, World!"); 
return 0;
} 
4

4 回答 4

4

This is known as a Variadic macro.

于 2012-07-16T23:49:22.150 回答
2

If your compiler supports __VA_ARGS__, you can do it like this:

#include <stdio.h>

#define NUM_ARGS__(X, \
                      N64,N63,N62,N61,N60, \
  N59,N58,N57,N56,N55,N54,N53,N52,N51,N50, \
  N49,N48,N47,N46,N45,N44,N43,N42,N41,N40, \
  N39,N38,N37,N36,N35,N34,N33,N32,N31,N30, \
  N29,N28,N27,N26,N25,N24,N23,N22,N21,N20, \
  N19,N18,N17,N16,N15,N14,N13,N12,N11,N10, \
  N09,N08,N07,N06,N05,N04,N03,N02,N01,  N, ...) N

#define NUM_ARGS(...) \
  NUM_ARGS__(0, __VA_ARGS__, \
                 64,63,62,61,60, \
  59,58,57,56,55,54,53,52,51,50, \
  49,48,47,46,45,44,43,42,41,40, \
  39,38,37,36,35,34,33,32,31,30, \
  29,28,27,26,25,24,23,22,21,20, \
  19,18,17,16,15,14,13,12,11,10, \
   9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

#define PRINT_STRING_1(var) \
  { if (!(var)) {} }

#define PRINT_STRING_2(var, ...) \
  { if (!(var)) test(__VA_ARGS__); }

#define PRINT_STRINGN__(N, ...) \
  PRINT_STRING_##N(__VA_ARGS__)

#define PRINT_STRINGN(N, ...) \
  PRINT_STRINGN__(N, __VA_ARGS__)

#define PRINT_STRING(...) \
  PRINT_STRINGN(NUM_ARGS(__VA_ARGS__), __VA_ARGS__) 

void test(char* var2) 
{ 
  printf("%s\n", var2); 
} 

int main(void)
{
  PRINT_STRING(1);
  PRINT_STRING(0, "Hello, World!");
  PRINT_STRING(1, "You can't see me!");
  return 0;
}

Output:

Hello, World!
于 2012-07-17T06:56:51.407 回答
2

To do something like that you'd have to implement a series of macros, something like this

#include <stdbool.h>
#define PRINT_STRING0(X, Y) do { if (X && Y) test(Y); } while(false) 
#define PRINT_STRING1(X, Y, ...) PRINT_STRING0(X, Y)
#define PRINT_STRING(...) PRINT_STRING1(__VA_ARGS__, 0, 0)

The last of them (user interface) adds a second or third argument of 0. PRINT_STRING1 then ignores all arguments that are more than 2. And PRINT_STRING0 then does the work.

Some more remarks:

  • be careful that a macro like you want to program it here can be place syntactically just as any other statement. In the example here the do { } while(false) does that trick
  • since C99 has Boolean type and constants, these are _Bool or bool and false and true
于 2012-07-17T07:02:07.623 回答
0

Don't use a macro to do that, use a variadic function!

void print_string( bool should_print, ... )
{
    if( should_print )
    {
        va_list argp;
        va_start( argp, should_print);
        char *string = va_arg(argp, char *);
        if( string) printf("%s", string );
        va_end( argp );

    }
}

But be very careful when using these sorts of things, because va_arg doesn't check if you really have a 2nd argument.

Also, to use a macro that accepts one or 2 arguments, use the GCC trick (at the very bottom of this page):

#define PRINT_STRING( should , args... ) print_string( should , ##args )

(note the spaces between the commas)

于 2012-07-17T06:36:28.850 回答