Created
March 27, 2013 00:37
-
-
Save reterVision/5250613 to your computer and use it in GitHub Desktop.
__typeof__ example in GCC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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