### 字符數組
~~~
char str1[10] = {'h', 'e', 'l', 'l', 'o'}; //后面的元素是 0 0 0 0 0
printf("%s\n", str1); //%s會接受字符串結束標志'\0'之前的所有字符,在ascii碼中就是數字0
~~~
```
hello
```
~~~
char str1[10] = {'h', 'e', 'l', 'l', 'o'};
for (int i = 0; i < sizeof(str1) / sizeof(char); ++i) {
printf("%c", str1[i]);
}
~~~
可以看到打印出的是ascii碼中的'\0'就是空字符.

~~~
char str1[10] = {'h', 'e', 'l', 'l', 'o'};
for (int i = 0; i < sizeof(str1) / sizeof(char); ++i) {
printf("%d\n", str1[i]);
}
~~~
可以看到打印的都是ascii碼
```
104
101
108
108
111
0
0
0
0
0
```
### 字符串
~~~
char str2[10] = "hello";
printf("%s\n", str2);
for (int i = 0; i < sizeof(str2) / sizeof(char); ++i) {
printf("%c\n", str2[i]);
}
~~~

### 字符串不指定長度
~~~
char str2[] = "hello";
printf("%s\n", str2);
printf("%d\n", sizeof(str2)); //6個長度
for (int i = 0; i < sizeof(str2) / sizeof(char); ++i) {
printf("%c\n", str2[i]);
}
~~~
可以看到程序會自動的幫我們加上'\0'

~~~
char str3[] = {'h', 'e', 'l', 'l', 'o'};
printf("%d\n", sizeof(str3)); //5個長度
for (int i = 0; i < sizeof(str3) / sizeof(char); ++i) {
printf("%c\n", str3[i]);
}
~~~
```
5
h
e
l
l
o
```
### 手動加上\0
~~~
char str3[] = "he\0llo";
printf("%d\n", sizeof(str3));
printf("%s\n", str3);
~~~
遇到\0就結束了
```
7
he
```
### 字符數組用%s輸出
~~~
char str3[] = {'h', 'e', 'l', 'l', 'o'};
printf("%s\n", str3);
printf("%d", sizeof(str3));
~~~
老師演示的是后面會出現亂碼,因為用%s輸出,必須要找到\0才算結束. 但是這里并沒有. 可能是因為標準變了. **千萬不要這么來使用**.
```
hello
5
```
### scanf 的問題
**scanf()函數接收輸入數據時,遇以下情況結束一個數據的輸入**
① 遇空格、“回車”、“跳格”鍵。
② 遇寬度結束。
③ 遇非法輸入
~~~
char str[100];
scanf("%s",str);
printf("%s",str);
~~~
```
hello world
hello //只輸出了hello
```
這樣才可以
~~~
char str[100];
scanf("%[^\n]",str);
printf("%s",str);
~~~
```
hello world
hello world
```
### 字符串追加
~~~
char str1[] = "hello";
char str2[] = "world";
char str3[100];
int index = 0;
while (str1[index] != '\0') {
str3[index] = str1[index];
index++;
}
while (str2[index - 5] != '\0') {
str3[index] = str2[index - 5];
index++;
}
~~~
### 字符串和字符數組在內存中不同的存儲方式