Hacker News new | ask | show | jobs
by catfood 4605 days ago
Might be obvious to some of you, but I use this to check what my macros expand out into to make sure the syntax is valid.

  #include <stdio.h>

  #define atoa(x) #x

  #define mdb(x) printf("%s\n", atoa(x))

  #define swap(type, x, y) do {type temp = *(x); *(x) = *(y); *(y) = temp;} while(0)

  int main(int argc, char *argv[])

  {

    int cat[2] = {33, 44};

    printf("atoa(cat): %s\n", atoa(cat)); // "cat" 
	
    mdb(swap(int, cat, cat+1)); // expands the macro inside to text 
	
    swap(int, cat, cat+1); // the macro itself 
	
    printf("cat[0]: %d\n", cat[0]); // 44 
	
    return 0;
  }