英文:
in dev c++ my library is missing how to install it
问题
//my code is
#include <stdio.h>
int main() {
char txt[] = "xyz";
printf("%d", strlen(txt));
return 0;
}
//error is strlen is not declared in this scope
//it should work my code is correct
英文:
//my code is
#include <stdio.h>
int main() {
char txt[] = "xyz";
printf("%d", strlen(txt));
return 0;
}
//error is strlen is not declared in this scope
//it should work my code is correct
答案1
得分: 2
https://en.cppreference.com/w/c/string/byte/strlen 说:
> 定义在头文件 <string.h>
P.S. 它还指出返回类型是 size_t
,这是无符号的,而 https://en.cppreference.com/w/c/io/fprintf 说 size_t
的 printf 格式符是 z
,所以格式字符串应该是 "%zu"
。
英文:
https://en.cppreference.com/w/c/string/byte/strlen says:
> Defined in header <string.h>
P.S. It also says that the return type is size_t
, which is unsigned, and https://en.cppreference.com/w/c/io/fprintf says that the printf specifier for size_t
is z
, so the format string should be "%zu"
.
答案2
得分: 2
问题
- 你忘记了包含
<string.h>
头文件以使用strlen()
函数。更多信息请查看这里。 strlen
的返回类型是size_t
而不是int
,因此应该使用%zu
来格式化输出。
修复:
#include <stdio.h>
#include <string.h> // 你遗漏的头文件
int main(void) {
char txt[] = "xyz";
printf("%zu", strlen(txt));
return 0;
}
英文:
issues
- You're missing the <string.h> header for
strlen()
. More info here. - The return type of strlen is that of size_t not int so use %zu for the format
fix:
#include <stdio.h>
#include <string.h> // The header you were missing
int main(void) {
char txt[] = "xyz";
printf("%zu", strlen(txt));
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论