嵌入式-C语言基础:字符串strlen和sizeof的区别
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
4556