在 dev c++ 中我的库丢失了如何安装它

问题描述 投票:0回答:2

//我的代码是

#include <stdio.h>
int main() {
char txt[] = "xyz";
printf("%d", strlen(txt));
return 0;
}

//错误是 strlen 没有在此范围内声明

//它应该工作我的代码是正确的

c strlen
2个回答
0
投票

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"
.


0
投票

问题

  • 你错过了strlen()
    标题检查信息here.
  • strlen 的返回类型是 size_t 的返回类型而不是 int 所以使用 %ld 作为格式

修正:

#include <stdio.h>
#include <string.h>  // The header you were missing
int main(void) {
    char txt[] = "xyz";
    printf("%ld", strlen(txt));
    return 0;
}

© www.soinside.com 2019 - 2024. All rights reserved.