Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 1.95 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Variable arguements in macro using ellipsis
  2. void test(char *var2)
  3. {
  4.  printf("%sn",var2);
  5. }
  6.  
  7. #define PRINT_STRING(...) ( if (!var1) test(var2) )
  8.  
  9. int main(int argc, _TCHAR argv[]) {
  10.  
  11. PRINT_STRING(TRUE);
  12. PRINT_STRING(FALSE,"Hello, World!");
  13. return 0;
  14. }
  15.        
  16. void print_string( bool should_print, ... )
  17. {
  18.     if( should_print )
  19.     {
  20.         va_list argp;
  21.         va_start( argp, should_print);
  22.         char *string = va_arg(argp, char *);
  23.         if( string) printf("%s", string );
  24.         va_end( argp );
  25.  
  26.     }
  27. }
  28.        
  29. #define PRINT_STRING( should , args... ) print_string( should , ##args )
  30.        
  31. #include <stdbool.h>
  32. #define PRINT_STRING0(X, Y) do { if (X && Y) test(Y); } while(false)
  33. #define PRINT_STRING1(X, Y, ...) PRINT_STRING0(X, Y)
  34. #define PRINT_STRING(...) PRINT_STRING1(__VA_ARGS__, 0, 0)
  35.        
  36. #include <stdio.h>
  37.  
  38. #define NUM_ARGS__(X,
  39.                       N64,N63,N62,N61,N60,
  40.   N59,N58,N57,N56,N55,N54,N53,N52,N51,N50,
  41.   N49,N48,N47,N46,N45,N44,N43,N42,N41,N40,
  42.   N39,N38,N37,N36,N35,N34,N33,N32,N31,N30,
  43.   N29,N28,N27,N26,N25,N24,N23,N22,N21,N20,
  44.   N19,N18,N17,N16,N15,N14,N13,N12,N11,N10,
  45.   N09,N08,N07,N06,N05,N04,N03,N02,N01,  N, ...) N
  46.  
  47. #define NUM_ARGS(...)
  48.   NUM_ARGS__(0, __VA_ARGS__,
  49.                  64,63,62,61,60,
  50.   59,58,57,56,55,54,53,52,51,50,
  51.   49,48,47,46,45,44,43,42,41,40,
  52.   39,38,37,36,35,34,33,32,31,30,
  53.   29,28,27,26,25,24,23,22,21,20,
  54.   19,18,17,16,15,14,13,12,11,10,
  55.    9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  56.  
  57. #define PRINT_STRING_1(var)
  58.   { if (!(var)) {} }
  59.  
  60. #define PRINT_STRING_2(var, ...)
  61.   { if (!(var)) test(__VA_ARGS__); }
  62.  
  63. #define PRINT_STRINGN__(N, ...)
  64.   PRINT_STRING_##N(__VA_ARGS__)
  65.  
  66. #define PRINT_STRINGN(N, ...)
  67.   PRINT_STRINGN__(N, __VA_ARGS__)
  68.  
  69. #define PRINT_STRING(...)
  70.   PRINT_STRINGN(NUM_ARGS(__VA_ARGS__), __VA_ARGS__)
  71.  
  72. void test(char* var2)
  73. {
  74.   printf("%sn", var2);
  75. }
  76.  
  77. int main(void)
  78. {
  79.   PRINT_STRING(1);
  80.   PRINT_STRING(0, "Hello, World!");
  81.   PRINT_STRING(1, "You can't see me!");
  82.   return 0;
  83. }
  84.        
  85. Hello, World!