一维字符数组
一维字符数组初始化
逐个元素初始化(不推荐)
char arr[5] = {'h', 'e', 'l', 'l', 'o'};
以字符串方式初始化(推荐)
char arr[6] = "hello";
以上两种区别
sizeof测字符数组
char arr1[16] = "";
cout << sizeof(arr1) << endl; //16
char arr2[] = "hello";
cout << sizeof(arr2) << endl; //6
字符数组遍历
循环方式逐个遍历
char arr[6] = "hello";
int i = 0;
for(i = 0; i<6; i++)
{
cout << arr[i];
}
使用cout直接输出字符串 遇到'\0'结束
char arr1[] = "hello";
cout << arr1; //hello
char arr2[] = "hel\0lo";
cout << arr2; //hel
键盘获取字符串
cin获取字符串 遇到空格或回车结束
char str[16] = "";
cin >> str; //hello world
cout << str; //hello
cin.getline获取带空格的字符串
char str[16] = "";
cin.getline(str, sizeof(str)); //hello c++
cout << str; //hello c++
标签:字符,cout,08,一唯,数组,char,str,hello
From: https://www.cnblogs.com/mzx233/p/17723124.html