sizeof 跟strlen

sizeof 和strlen

当计算字符串的大小时,sizeof和strlen的区别:

sizeof 包括字符结束标识符,strlen不包括。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	int a[2] = {0,1};
	char b[7] = "hellor";
	

	int i = sizeof(a);
	int j = sizeof(a)/sizeof(a[0]);

	int m = strlen(b);
	int n = sizeof(b);

	printf("%d,%d,%d,%d\n", i,j,m,n);
	return 0;
}

sizeof 跟strlen

strlen不可用于其他类型的数据;

而sizeof可以。