Pudron 2020-02-12 11:39 采纳率: 0%
浏览 433

使用C11的_Generic时弹出警告,怎么回事?

代码:

#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的支持不够?

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-02-12 12:24
    关注
    评论

报告相同问题?