C语言字符串与字符数组

[code="C"]
#include "stdafx.h"
#include "string.h"

int main(int argc, char* argv[])
{
//char b[5] = {"hello"}; //编译时报错
char b[6] = {"hello"};
//或
//char b = {"hello"}; //正确
char a[5] = {'h', 'e', 'l', 'l', 'o'};
printf("hello:%s\n", b);
printf("b length is : %d\n", strlen(b));
printf("a length is : %d\n", strlen(a));
return 0;
}
[/code]
此时的输出如下
[img]http://dl.iteye.com/upload/attachment/569870/79a69291-c02d-38e8-b68c-b7408d48ab1f.jpg[/img]
如果将代码
[code="C"]
char a[5] = {'h', 'e', 'l', 'l', 'o'};
[/code]
修改为
[code="C"]
char a[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
[/code]
则输出为
[img]http://dl.iteye.com/upload/attachment/569874/aeefe7bd-b4a5-3995-96f5-4c4d23ba20b2.jpg[/img]

问题:
1.为什么第一次的执行“[color=red]printf("a length is : %d\n", strlen(a));[/color]”的输出结果是“[color=red][b]13[/b][/color]”?
2.对于'\0'是不是字符串会自动在后面加上而字符数组中则不会加上,那么字符数组中何时会出现'[color=red]\0[/color]'字符?

楼上有几个错误,字符串数组有时不会填\0,当数组大小等于字符串大小的时候没有位置添加\0,也就是说b[n>5]="hello"的情况时才会添加\0,所以b[6]时strlen()正确,因为strlen以“\0"判断,这点要和sizeof区分
而出现11的情况,你要懂得a,b的内存分配,其实是a连接在了没有\0结尾的b上得结果,可以查看a,b的地址printf("%X %X",&a,&b)来判断;

字符串是和数组不是一个概念,字符串肯定加,但是数组不会呀。。。。除非定义的时候初始化

字符串他默认后面会帮你加'\0',计算strlen的时候,他是从首地址开始hello中的h开始,一直碰到第一个'\0'才返回了。计算之间的字符个数。不同的电脑可能算出来a的长度不尽相同,跟内存有关。。好久没用C了。。 :(