是什么(char)的0和'\\ 0'之间的区别?用C [英] What is the difference between (char)0 and '\0'? in C
问题描述
什么是两者的区别:(字符)0
和'\\ 0'
表示终止空字符在字符数组?
What is the difference between using (char)0
and '\0'
to denote the terminating null character in a character array?
推荐答案
在字符文字反斜线符号允许你指定使用字符本身的字符的数值来代替。因此,'\\ 1
[*]指,其数值为1的字符'\\ 2
办法,其数值为2的字符等几乎。由于℃的怪癖,性格文字居然有键入 INT
,确实 INT
用来处理其他字符背景太,如龟etc
的返回值。因此,'\\ 1
表示数值为int,其数值为1的字符,等等。
The backslash notation in a character literal allows you to specify the numeric value of a character instead of using the character itself. So '\1'
[*] means "the character whose numeric value is 1", '\2'
means "the character whose numeric value is 2", etc. Almost. Due to a quirk of C, character literals actually have type int
, and indeed int
is used to handle characters in other contexts too, such as the return value of fgetc
. So '\1'
means "the numeric value as an int, of the character whose numeric value is 1", and so on.
由于字符是的数值在C,实际的是,其数值为1的字符的(char)的1
,而在'\\ 1
对编译器没有影响 - '\\ 1
具有相同类型和值 1
在C.所以更需要在字符串文字反斜线符号比在字符文字,插入非打印字符不要吨有自己的逃生code。
Since characters are numeric values in C, "the character whose numeric value is 1" actually is (char)1
, and the extra decoration in '\1'
has no effect on the compiler - '\1'
has the same type and value as 1
in C. So the backslash notation is more needed in string literals than it is in character literals, for inserting non-printable characters that don't have their own escape code.
我个人preFER写 0
时,我的意思是0,让隐式转换做好自己的事情。一些人发现,很难理解。当与这些人一起工作,是有帮助的写'\\ 0'
当你的意思是值为0的字符,也就是在你希望你的<$ C $例说C> 0 很快将隐式转换为字符
键入。同样的,它可以帮助写 NULL
时,你的意思是一个空指针常量, 0.0
时,你的意思值双0,依此类推。
Personally, I prefer to write 0
when I mean 0, and let implicit conversions do their thing. Some people find that very difficult to understand. When working with those people, it's helpful to write '\0'
when you mean a character with value 0, that is to say in cases where you expect your 0
is soon going to implicitly convert to char
type. Similarly, it can help to write NULL
when you mean a null pointer constant, 0.0
when you mean a double with value 0, and so on.
无论它使给编译器的任何差异,以及它是否需要一个铸造,取决于上下文。由于'\\ 0'
具有完全相同的类型和值 0
,需要将其转换为字符
在完全相同的情况下。因此,'\\ 0'
和(字符)0
的类型不同,对于完全等同前pressions即可无论是考虑(char)的'\\ 0'
VS (字符)0
或'\\ 0
VS 0
。 NULL
具有实现定义的类型 - 有时需要将其转换为指针类型,因为它可能有整型。 0.0
的类型双击
,所以从 0
肯定是不同的。尽管如此,浮动F = 1.0;
与浮动F = 1;
和浮动˚F = 1.0F
,而 1.0 / I
,其中 I
是一个int,通常有从不同的值 1 / I
。
Whether it makes any difference to the compiler, and whether it needs a cast, depends on context. Since '\0'
has exactly the same type and value as 0
, it needs to be cast to char
in exactly the same circumstances. So '\0'
and (char)0
differ in type, for exactly equivalent expressions you can either consider (char)'\0'
vs (char)0
, or '\0'
vs 0
. NULL
has implementation-defined type -- sometimes it needs to be cast to a pointer type, since it may have integer type. 0.0
has type double
, so is certainly different from 0
. Still, float f = 1.0;
is identical to float f = 1;
and float f = 1.0f
, whereas 1.0 / i
, where i
is an int, usually has a different value from 1 / i
.
所以,任何一般规则是否使用'\\ 0'
或 0
纯粹是为了方便读者您code的 - 这是所有相同的编译器。挑选无论你(和你的同事)preFER的外观,或者定义一个宏 ASCII_NUL
。
So, any general rule whether to use '\0'
or 0
is purely for the convenience of readers of your code - it's all the same to the compiler. Pick whichever you (and your colleagues) prefer the look of, or perhaps define a macro ASCII_NUL
.
[*]或'\\ 01'
- 因为反斜杠引入了的八的数量,而不是小数,它有时聪明,使这个位通过确保它更明显的开始以0使为0,1,2,当然没有什么区别。我说有时,因为反斜线只能跟着3个八进制数字,所以你可以不写 \\ 0101
而不是的\\ 101
,要提醒的是它是一个八进制值读者。这一切都非常尴尬,并导致更多的装饰: \\ X41
为大写的A,因此,你可以写'\\ X0'
0,如果你想要的。
[*] or '\01'
- since the backslash introduces an octal number, not decimal, it's sometimes wise to make this a bit more obvious by ensuring it starts with a 0. Makes no difference for 0, 1, 2 of course. I say "sometimes", because backslash can only be followed by 3 octal digits, so you can't write \0101
instead of \101
, to remind the reader that it's an octal value. It's all quite awkward, and leads to even more decoration: \x41
for a capital A, and you could therefore write '\x0'
for 0 if you want.
这篇关于是什么(char)的0和'\\ 0'之间的区别?用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!