浅谈C语言字符串结束符''

如果你希望你的字符串以’‘结束,那么你可以这样做:

1 char str[]={"hello"};//①字符串赋值
2 char str[]={'h','e','l','l','o','0'};//②人为添加
3 char str[6]={'h','e','l','l','o'};//③故意给数组预留一个空位

注:当出现以下情况时,会发生''丢失

1 char str[5]={"hello"};//①数组长度不够
2 char str[]={'h','e','l','l','o'};//②不指定数组长度,把每个字符单独用引号括起来

使用函数strlen()求某个字符串的长度时是不包括结尾标志符''的,但当你用sizeof()求某个字符串占用的内存空间时,结尾字符''是被包括在里面的。

参考:

  1.http://www.cnblogs.com/kaituorensheng/archive/2013/12/09/3464462.html

  2.https://zhidao.baidu.com/question/42473019.html

原文地址:https://www.cnblogs.com/stefango/p/9329405.html