strlen()函数 数组长度 C C++

1.

#include <iostream>

#include <string.h>
using namespace std;
int main()
{
char a[]="I am happy";
cout<<strlen(a);
return 0;

}

输出结果:10

此时数组a的实际长度是11,应该包括'\0'在内;而strlen()函数求得的长度只是数组中有效字符的长度,不包括'\0'在内 

2.

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a[10]="I am happy";//此时编译不通过,数组a的实际长度是11,方括号内应该填入大于等于11的数
cout<<strlen(a);
return 0;
}