我已经搜索了这个警告,并且每个人在他们的代码中都有一些错误,但这是一个非常意外的事情,我无法弄清楚.我们确实期望strlen(x)是一个整数,但这个警告告诉我什么?strlen怎么可能不是int?
In function ‘fn_product’:
line85:3:warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘size_t’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)
我在fn_product中的代码 -
char *fn_product (char x[],char y[]){
if (strlen(x)==1) // line85
printf("\nlength of string--%d\n", strlen(x));
/*other code*/
}
Run Code Online (Sandbox Code Playgroud)
不应该strlen(x)为int.为什么它的格式为size_t?
Car*_*rum 31
你查看了手册页吗? strlen(3)回报size_t.使用%zu打印.
正如下面的评论中所提到的,clang有时有助于找到更好的错误消息.clang对这种情况的警告非常棒,实际上:
example.c:6:14: warning: format specifies type 'unsigned int' but the argument
has type 'size_t' (aka 'unsigned long') [-Wformat]
printf("%u\n", strlen("abcde"));
~^ ~~~~~~~~~~~~~~~
%zu
1 warning generated.
Run Code Online (Sandbox Code Playgroud)