</pre><pre class="cpp" name="code">#include<iostream>
int main()
{
using namespace std;
char dog[7]={'b','e','c','a','u',' ','2'};
cout<<dog;
//测试是否能输出中间还有空字符的字符串
cin.get();
return 0;
}
//Tips:可以用以下的代码避免以上情况
#include<iostream>
int main()
{
using namespace std;
char bird[7]="Mr. Cheeps";
cout<<bird;
//测试是否能输出中间还有空字符的字符串
cin.get();
return 0;
}
//Tips:strlen()为字符串长度 sizeof(name1)为数组的实际长度;字符数组会被\0截取
#include<iostream>
#include<cstring>
int main()
{
using namespace std;
const int Size=15;
char name1 [Size];
char name2 [Size]="C++owboy";
cout<<"Howdy! I'm"<<name2;
cout<<"! What's your name?\n";
cin>>name1;
cout<<"Well, "<<name1<<",your name has";
cout<<strlen(name1)<<" letters and is stored\n";
cout<<"in an array of "<<sizeof(name1)<<" bytes\n";
cout<<"your initial is "<<name1[0]<<".\n";
name2[3]='\0';
cout<<"Here are the first 3 characters of my name : ";
cout<<name2<<endl;
cin.get();
return 0;
}