首页 > 其他分享 >嵌入式-C语言基础:字符串strlen和sizeof的区别

嵌入式-C语言基础:字符串strlen和sizeof的区别

时间:2022-11-01 21:12:38浏览次数:46  
标签:cdata1 C语言 printf 字符串 长度 sizeof strlen

strlen表示的实际的字符串长度,不会把字符串结束符'\0'计算进去,而sizeof则不是实际的字符串长度,它会把字符串的结束标识符'\0'也包含进去。

#include<stdio.h>

int main()
{
    char cdata1[125]="hello";
    int len1=strlen(cdata1);
    char cdata2[]="hello";
    int len2=strlen(cdata2);
    printf("cdata1实际长度=%d\n",len1);
    printf("cdata2实际长度=%d\n",len2);
    len1=sizeof(cdata1);
    len2=sizeof(cdata1);
    printf("cdata1长度=%d\n",len1);
    printf("cdata2长度=%d\n",len2);
    return 0;
}

输出结果:

cdata1实际长度=5
cdata2实际长度=5
cdata1长度=125
cdata2长度=125

标签:cdata1,C语言,printf,字符串,长度,sizeof,strlen
From: https://www.cnblogs.com/zxtang/p/16849166.html

相关文章