标签:
/* * * gcc -std=c11 -o gen1 gen1.c * * */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define GENERAL_ABS(x) _Generic((x), int:abs, float:fabsf, double:fabs)(x) static void GenericTest(void) { printf("int abs: %d\n", GENERAL_ABS(-12)); printf("float abs: %f\n", GENERAL_ABS(-12.04f)); printf("double abs: %f\n", GENERAL_ABS(-13.09876)); int a = 10; int b = 0, c = 0; _Generic(a + 0.1f, int:b, float:c, default:b)++; printf("b = %d, c = %d\n", b, c); _Generic(a += 1.1f, int:b, float:c, default:b)++; printf("a = %d, b = %d, c = %d\n", a, b, c); } int main(void) { GenericTest(); return 0; }
标签:
原文地址:http://my.oschina.net/u/2245781/blog/466674