首页 > 编程知识 正文

c++strlen函数用法,c语言基础知识 strlen函数用法

时间:2023-05-04 02:54:22 阅读:243807 作者:2570

C语言strlen()函数:返回字符串的长度

头文件:#include <string.h>

strlen()函数用来计算字符串的长度,其原型为:unsigned int strlen (char *s);  s为指定的字符串

eg:

#include<stdio.h>#include<string.h>int main(){ char *str1 = "http://see.xidian.edu.cn/cpp/u/shipin/"; char str2[100] = "http://see.xidian.edu.cn/cpp/u/shipin_liming/"; char str3[5] = "12345"; printf("strlen(str1)=%d, sizeof(str1)=%dn", strlen(str1), sizeof(str1)); printf("strlen(str2)=%d, sizeof(str2)=%dn", strlen(str2), sizeof(str2)); printf("strlen(str3)=%d, sizeof(str3)=%dn", strlen(str3), sizeof(str3)); return 0;}
运行结果:
strlen(str1)=38, sizeof(str1)=4
strlen(str1)=45, sizeof(str1)=100
strlen(str1)=53, sizeof(str1)=5

如果字符的个数等于字符数组的大小,那么strlen()的返回值就无法确定了,例如
    char str[6] = "abcxyz";
strlen(str)的返回值将是不确定的。因为str的结尾不是0,strlen()会继续向后检索,直到遇到'',而这些区域的内容是不确定的。

转载于:https://www.cnblogs.com/xiaodingmu/p/6279723.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。