函数strlen()和sizeof的区别

函数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不但计算了字符串的大小,还记算了字符串末尾不可见的空字符!

 

猜你喜欢

转载自www.cnblogs.com/Xiangzhong-com/p/9096178.html