字符串终止 - 焦C = 0对焦炭C ='\\ 0' [英] String termination - char c=0 vs char c='\0'
问题描述
在终止字符串,在我看来,这在逻辑上字符C = 0
等同于的char c ='\\ 0'
,因为空(ASCII 0)字节是 0
,但通常人们倾向于做'\\ 0'
来代替。这是纯粹出于preference,或者它应该是一个更好的惯例?
When terminating a string, it seems to me that logically char c=0
is equivalent to char c='\0'
, since the "null" (ASCII 0) byte is 0
, but usually people tend to do '\0'
instead. Is this purely out of preference or should it be a better "practice"?
什么是preferred选择?
What is the preferred choice?
编辑: K&安培; R 的 是的:字符常量'\\ 0
重新presents值为零的字符,空字符。'\\ 0'
经常写,而不是 0
来强调一些前pression的性格本质,但数值只是 0
。
K&R says: "The character constant '\0'
represents the character with value zero, the null character. '\0'
is often written instead of 0
to emphasize the character nature of some expression, but the numeric value is just 0
.
推荐答案
<一个href=\"http://en.wikipedia.org/wiki/Ascii#ASCII_control_$c$c_chart\">http://en.wikipedia.org/wiki/Ascii#ASCII_control_$c$c_chart
Binary Oct Dec Hex Abbr Unicode Control char C Escape code Name
0000000 000 0 00 NUL ␀ ^@ \0 Null character
有什么区别,但更地道的是'\\ 0'
。
There's no difference, but the more idiomatic one is '\0'
.
也许把它放下为的char c = 0;
可能意味着你打算用它作为一个数字(如柜台),虽然我个人不会来跨code这样。 '\\ 0'
是毫不含糊的。
Perhaps putting it down as char c = 0;
could mean that you intend to use it as a number (e.g. a counter), although I've personally not come across code like that. '\0'
is unambiguous.
这篇关于字符串终止 - 焦C = 0对焦炭C ='\\ 0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!