//我的代码是
#include <stdio.h>
int main() {
char txt[] = "xyz";
printf("%d", strlen(txt));
return 0;
}
//错误是 strlen 没有在此范围内声明
//它应该工作我的代码是正确的
https://en.cppreference.com/w/c/string/byte/strlen 说:
在标题中定义
<string.h>
附言它还说返回类型是
size_t
,它是无符号的,并且 https://en.cppreference.com/w/c/io/fprintf 说 size_t
的 printf 说明符是 z
,所以格式字符串应该是"%zu"
.
strlen()
的标题检查信息here.
#include <stdio.h>
#include <string.h> // The header you were missing
int main(void) {
char txt[] = "xyz";
printf("%ld", strlen(txt));
return 0;
}