代码:
#include<stdio.h>
#define GEN(x) _Generic((x),int:printf("int:%d\n",x),double:printf("double:%lf\n",x),default:printf("default\n"))
int main(){
GEN(123);
}
编译:
gcc -Wall -std=c11 test.c -o test
平台:
Win10+mingw-w64
警告:
test.c: In function 'main':
test.c:2:68: warning: format '%lf' expects argument of type 'double', but argument 2 has type 'int' [-Wformat=]
#define GEN(x) _Generic((x),int:printf("int:%d\n",x),double:printf("double:%lf\n",x),default:printf("default\n"))
^~~~~~~~~~~~~~
test.c:4:6:
GEN(123);
~~~
test.c:4:2: note: in expansion of macro 'GEN'
GEN(123);
^~~
test.c:2:68: warning: format '%lf' expects argument of type 'double', but argument 2 has type 'int' [-Wformat=]
#define GEN(x) _Generic((x),int:printf("int:%d\n",x),double:printf("double:%lf\n",x),default:printf("default\n"))
^~~~~~~~~~~~~~
test.c:4:6:
GEN(123);
~~~
test.c:4:2: note: in expansion of macro 'GEN'
GEN(123);
^~~
程序能正常执行,去掉-Wall后就不会弹警告了,难道gcc对c11的支持不够?