’\0’是判定字符数组结束的标识,表示这串字符到结尾了;
注意:在字符数组中’\0’是占一个位置的!
例如 定义char c[6]=“hello”,而在内存中字符数组 c 则是"hello\0";
即’\0’在数组中占有空间却不被我们看到;
所以如果一个长度为10的字符串要放在字符数组里的话就应该把字符数组的长度定义为11;
代码示例:
#include<stdio.h>
int main()
{
char s[6]="hello";
int i=0;
while(s[i]!='\0')
{
printf("%c ",s[i]);
i++;
}
printf("\n");
}
1
2
3
4
5
6
7
8
9
10
11
输出结果:
h e l l o
Press any key to continue
————————————————
版权声明:本文为CSDN博主「June·D」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dark_cy/article/details/83515270