c - strlen()值如何自动改变?

标签 c string strlen

我写了这段代码-

char s1[10] = "he",  s2[20] = "she", s3[30], s4[30];

printf("%d %d", strlen(s2) + strlen(s3), strlen(s4));

输出-

6 9

然后我写了这段代码 -

char s1[10] = "he",  s2[20] = "she", s3[30], s4[30];

printf("%d %d", strlen(s3), strlen(s4));

通过删除strlen(s1) +

输出-

0 3

我的问题是字符串 s4 的长度如何变化?

最佳答案

s3s4 未初始化,也不是 '\0' 终止的 C 字符串,这就是 strlen( ) 期望。因此您无法将它们传递给 strlen()

您看到的是undefined behaviour .

关于c - strlen()值如何自动改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34422120/

相关文章:

c++ - 使用局部全局变量在单独的线程中运行 C 函数

c - 如何将一个 64 位数字拆分为八个 8 位值?

ruby - 将字符串值转换为哈希

c++ - mblen 和 strlen 有什么区别?

c# - 为什么 C 语言在 if 语句中需要围绕简单条件的括号?

C:将可变长度的二维数组转换为一维字符串

c++ - 如何将一个对象的引用传递给另一个对象并仍然修改第一个对象?

string - R 替换字符串中的单词

c++ - 在 C++ 中的 memcpy 之后得到 strlen

c - 为什么 strlen() 函数给出了错误的值