#include /* Swap a and b using the "typeof" extension. */ #define SWAP(a,b) { \ typeof (a) c; \ c = a; \ a = b; \ b = c; \ } /* Example: swap integers. */ static void swap_int_example () { int x = 99; int y = 100; printf ("x = %d, y = %d\n", x, y); SWAP (x, y); printf ("x = %d, y = %d\n", x, y); } /* Example: swap structures. */ static void swap_struct_example () { struct ex { char * c; int d; }; struct ex x = { "monkey", 42, }; struct ex y = { "anteater", 99, }; printf ("%s %d %s %d\n", x.c, x.d, y.c, y.d); SWAP (x, y); printf ("%s %d %s %d\n", x.c, x.d, y.c, y.d); } int main () { swap_int_example (); swap_struct_example (); return 0; }