C语言教程:字符串strlen()函数

2018-10-0708:08:06编程语言入门到精通Comments4,250 views字数 366阅读模式

strlen()函数返回给定字符串的长度,它不计算空字符'\0'文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6440.html

使用示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6440.html

创建一个源文件:string_strlen.c,其代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6440.html

#include <stdio.h>  
void main()  
{  
   char ch[20]={'y', 'i', 'i', 'b', 'a', 'i', '.', 'c', 'o', 'm', '\0'};
   char doname[]="www.yiibai.com";  
   printf("Length of string is: %d",strlen(ch));  
   printf("Length of doname is: %d",strlen(doname));  
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6440.html

Length of string is: 10
Length of doname is: 14
Shell

可以看到第一个输出的结果为:10,这是因为它不把 '\0'计算在内。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6440.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6440.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6440.html

Comment

匿名网友 填写信息

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

确定