在Dev C++中,我的库丢失了,如何安装它?

huangapple go评论83阅读模式
英文:

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 &lt;stdio.h&gt;
int main() {
char txt[] = &quot;xyz&quot;;
printf(&quot;%d&quot;, 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 说:
> 定义在头文件 &lt;string.h&gt;

P.S. 它还指出返回类型是 size_t,这是无符号的,而 https://en.cppreference.com/w/c/io/fprintfsize_t 的 printf 格式符是 z,所以格式字符串应该是 &quot;%zu&quot;

英文:

https://en.cppreference.com/w/c/string/byte/strlen says:
> Defined in header &lt;string.h&gt;

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 &quot;%zu&quot;.

答案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 &lt;stdio.h&gt;
#include &lt;string.h&gt;  // The header you were missing
int main(void) {
    char txt[] = &quot;xyz&quot;;
    printf(&quot;%zu&quot;, strlen(txt));
    return 0;
}

huangapple
  • 本文由 发表于 2023年2月27日 11:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定