[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]'字符?