函数strlen()和sizeof的区别

时间:2021-02-23 06:21:49

函数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的区别

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