Skip to content

Instantly share code, notes, and snippets.

@reterVision
Created March 27, 2013 00:37
Show Gist options
  • Save reterVision/5250613 to your computer and use it in GitHub Desktop.
Save reterVision/5250613 to your computer and use it in GitHub Desktop.
__typeof__ example in GCC
#include <stdio.h>
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define min(a, b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _b : _a; })
#define max(a, b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
int main(int argc, char** argv)
{
int a = 5;
int b = 3;
printf("Smaller value (not safe): %d\n", MIN(a, b));
printf("a: %d, b: %d\n", a, b);
printf("Smaller after ++ (safe): %d\n", min(a++, ++b));
printf("a: %d, b: %d\n", a, b);
printf("Smaller after ++ (not safe): %d\n", MIN(a++, b++));
printf("a: %d, b: %d\n", a, b);
int c = 7;
int d = 8;
printf("Larger value: %d\n", max(c, d));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment