C语言 c中正确的字符串终止符是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16627588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is the correct string terminator in c
提问by DesirePRG
As I know the string terminating character in c is '\0'.
据我所知,c 中的字符串终止字符是 '\0'。
can we use '0' as the terminating character too? when I assign 0 to a specific index in a char array, and then use printf, it prints only upto that specific index.
我们也可以使用'0'作为终止符吗?当我将 0 分配给 char 数组中的特定索引,然后使用 printf 时,它仅打印到该特定索引。
hence, are both ways equivalent? is the null character equal to the literal 0?
因此,这两种方式是等价的吗?空字符是否等于文字 0?
回答by Theodoros Chatzigiannakis
The only value that can be used as a null terminator is the numerical value 0.
唯一可以用作空终止符的值是数值 0。
0isthe numerical value 0.'\0'is alsoanother way of representing the numerical value 0 in your code'0'is notthe numerical value 0 (it's the digitzero) and cannotbe used as a terminator.
0是数值 0。'\0'也是在代码中表示数值 0 的另一种方式'0'不是数值 0(它是数字零)并且不能用作终止符。
All strings literals implicitly contain the null terminator after their last visible character. In other cases, it may or may not be there automatically (depending on how the string was constructed), but you have to be sure that in every case the null terminator is there.
所有字符串字面量都在其最后一个可见字符后隐式包含空终止符。在其他情况下,它可能会或可能不会自动存在(取决于字符串的构造方式),但您必须确保在每种情况下都存在空终止符。
回答by kotlomoy
- is the null character equal to the literal 0?
- 空字符是否等于文字 0?
Yes.
是的。
The null character (also null terminator), abbreviated NUL, is a control character with the value zero.
空字符(也称为空终止符),缩写为 NUL,是一个值为 0 的控制字符。
- can we use '0' as the terminating character too?
- 我们也可以使用'0'作为终止符吗?
No. '0' is a character which has value 0x30. It is not null character and not equal to 0.
不,'0' 是一个值为 0x30 的字符。它不是空字符且不等于 0。
See this link: http://www.asciitable.com/
请参阅此链接:http: //www.asciitable.com/
回答by Bathsheba
'\0' is a convenient way of entering byte 0. It's called escaping. '0' is the character zero; very different. All strings in c are terminated with byte 0.
'\0' 是一种输入字节 0 的便捷方式。它被称为转义。'0' 是字符零;非常不一样。c 中的所有字符串都以字节 0 结尾。
回答by Andrew W
In C, strings are arrays of char's terminated with a '\0'. When writing a char with single quotes, as in '\0' you are reffering to the asci value of that char. Thus the expression: return '\0' == 0; would return true.
在 C 中,字符串是以 '\0' 结尾的字符数组。使用单引号编写字符时,如 '\0' 中,您指的是该字符的 asci 值。因此表达式: return '\0' == 0; 会返回true。

