// 比较 strlen(str)和 sizeof(str)的不同

来源:互联网 发布:如何玩好刘邦知乎 编辑:程序博客网 时间:2024/05/20 03:40
#include <STDIO.H>
#include "string.h"


int main()
{
int x = 0;
char str[10] ; 
// strlen()以'\0',作为结束标志,故strlen(str)不确定
x = strlen(str); 
printf("strlen of str is: %d \n",x);
x = sizeof(str); 
//而sizeof(str)==10
printf("sizeof of str is: %d \n",x);


char str2[] = "0123456789"; // 会自动添加一个结束符'\0'
// strlen()以'\0',作为结束标志,故strlen(str2)==10
x = strlen(str2); 
printf("strlen of str2 is: %d \n",x);
x = sizeof(str2); 
//而sizeof()则会把'\0'也计算在内,sizeof(str2)==10
printf("sizeof of str2 is: %d \n",x);
return 0;
}