c++ strlen函数的用法是什么

c++
1076
2024/2/29 13:18:25
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

strlen函数用于获取字符串的长度,即字符串中字符的个数(不包括字符串末尾的空字符’\0’)。其用法如下:

#include <cstring>

size_t strlen(const char* str);

其中,参数str是要获取长度的字符串,返回值是该字符串中字符的个数。示例用法如下:

#include <iostream>
#include <cstring>

int main() {
    const char* str = "Hello, World!";
    size_t length = strlen(str);
    
    std::cout << "Length of the string: " << length << std::endl;
    
    return 0;
}

上述代码会输出字符串"Hello, World!"的长度,即13。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: c++中goto语句使用失效怎么解决