函数strlen()和sizeof的区别:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define SENTENCE “It is a cat”

int main()

{

    char name[40];

    gets(name);

    printf(“%s\n”, name);

    printf(“The size of name is %zd %d\nThe size of  SENTENCE is %zd %d”,strlen(name), sizeof(name), strlen(SENTENCE), sizeof(SENTENCE));//请注意strlen()和sizeof的区别!

return 0;

}

strlen()函数是计算实际字符串的长度,而sizeof不但计算了字符串的大小,还记算了字符串末尾不可见的空字符!

 

版权声明:本文为Xiangzhong-com原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Xiangzhong-com/p/9096178.html