// *** 以前 ***
//C99:每种类型一个函数,调用方必须选对;
//对 double 调用 abs 会悄悄丢掉小数部分
int wholeOld = abs(-7);
double rightOld = fabs(-2.5);
double wrongOld = abs(-2.5); //double 先被变成了 int -2
//wrongOld is 2
printf("wrongOld is %g\n", wrongOld);
// *** 在 C11 版本中 ***
//_Generic 在编译时按表达式的类型选择分支;
//<tgmath.h> 在 C99 中也做同样的事,但只限于库内部
#define absolute(x) _Generic((x), int: abs, long: labs, double: fabs)(x)
#define typeName(x) _Generic((x), int: "int", double: "double", \
char *: "char *", default: "other")
double right = absolute(-2.5);
int whole = absolute(-7);
//right is 2.5
//whole is 7
//typeName(right) is "double"
printf("right is %g\n", right);
printf("whole is %d\n", whole);
printf("typeName(right) is %s\n", typeName(right));