现在的位置: 首页 > 综合 > 正文

char[]:strlen和sizeof的区别

2018年04月03日 ⁄ 综合 ⁄ 共 361字 ⁄ 字号 评论关闭

C code:

  

// char[]:strlen和sizeof的区别

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{
	char str[] = "Hello man";
	printf("str = %s\nstrlen(str) = %d\nsizeof(str) = %d\n", str, strlen(str), sizeof(str));

	return 0;
}

运行结果:

str = Hello man
strlen(str) = 9
sizeof(str) = 10
Press any key to continue

 

as we can see,  char array's length is 9, but it occupied 10 spaces(bytes)(include the last end-tag: '\0')

抱歉!评论已关闭.