C语言 (char)0 和 '\0' 有什么区别?在 C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7578632/
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 difference between (char)0 and '\0'? in C
提问by Sathya
What is the difference between using (char)0and '\0'to denote the terminating null character in a character array?
using(char)0和'\0'to 表示字符数组中的终止空字符有什么区别?
采纳答案by Steve Jessop
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 intis 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.
字符文字中的反斜杠表示法允许您指定字符的数值,而不是使用字符本身。所以'\1'[*] 表示“数值为 1'\2'的字符”,表示“数值为 2 的字符”,等等。由于 C 的一个怪癖,字符文字实际上具有 type int,并且确实int也用于处理其他上下文中的字符,例如fgetc. 所以'\1'意思是“数值为 1 的字符的整数形式的数值”,依此类推。
Since characters arenumeric 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 1in 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.
由于字符在 C 中是数值,“数值为 1 的字符”实际上是(char)1,并且额外的修饰'\1'对编译器没有影响 -'\1'与1C 中的类型和值相同。因此反斜杠符号在字符串文字而不是字符文字,用于插入没有自己的转义码的不可打印字符。
Personally, I prefer to write 0when 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 0is soon going to implicitly convert to chartype. Similarly, it can help to write NULLwhen you mean a null pointer constant, 0.0when you mean a double with value 0, and so on.
就我个人而言,我更喜欢0在我的意思是 0 时写,让隐式转换来做他们的事情。有些人觉得这很难理解。与这些人一起工作时,写下'\0'您的意思是值为 0 的字符会很有帮助,也就是说,在您预计0很快将隐式转换为char类型的情况下。类似地,NULL当您指的是空指针常量时,0.0当您指的是值为 0 的双精度值时,它会有所帮助,依此类推。
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 charin exactly the same circumstances. So '\0'and (char)0differ in type, for exactly equivalent expressions you can either consider (char)'\0'vs (char)0, or '\0'vs 0. NULLhas implementation-defined type -- sometimes it needs to be cast to a pointer type, since it may have integer type. 0.0has 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 iis an int, usually has a different value from 1 / i.
它是否对编译器有任何影响,以及它是否需要强制转换,取决于上下文。由于与'\0'具有完全相同的类型和值0,因此需要char在完全相同的情况下将其强制转换为 。因此'\0',(char)0类型不同,对于完全等效的表达式,您可以考虑(char)'\0'vs(char)0或'\0'vs 0。NULL具有实现定义的类型——有时需要将其转换为指针类型,因为它可能具有整数类型。0.0有类型double,所以肯定不同于0. 尽管如此,float f = 1.0;与float f = 1;and相同float f = 1.0f,而1.0 / i,其中i是 int,通常与 具有不同的值1 / i。
So, any general rule whether to use '\0'or 0is 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.
因此,无论是使用'\0'还是0纯粹为了方便您的代码读者的任何一般规则- 对于编译器来说都是一样的。选择您(和您的同事)喜欢的外观,或者定义一个宏ASCII_NUL。
[*] or '\01'- since the backslash introduces an octalnumber, 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 \0101instead of \101, to remind the reader that it's an octal value. It's all quite awkward, and leads to even more decoration: \x41for a capital A, and you could therefore write '\x0'for 0 if you want.
[*] 或'\01'- 由于反斜杠引入了一个八进制数,而不是十进制数,因此有时明智的做法是确保它以 0 开头,从而使其更加明显。当然,0、1、2 没有区别。我说“有时”,因为反斜杠后面只能跟 3 个八进制数字,所以不能用\0101代替\101, 来提醒读者它是一个八进制值。这一切都非常尴尬,并导致更多的装饰:\x41对于大写 A,因此'\x0'如果您愿意,您可以写为 0。
回答by Matthew Flaschen
They're both a 0, but (char) 0is a char, while '\0'is (unintuitively) an int. This type difference should not usually affect your program if the value is 0.
它们都是 0,但是(char) 0是一个字符,而'\0'(不直观地)是一个int. 如果值为 0,这种类型差异通常不会影响您的程序。
I prefer '\0', since that is the constant intended for that.
我更喜欢'\0',因为这是为此而设计的常量。
回答by Lundin
Zero can mean a lot of different things in C. You should use the null character '\0', because then there can be no confusion over that your intention is string termination.
零在 C 中可能意味着很多不同的东西。您应该使用空字符“\0”,因为这样就不会混淆您的意图是字符串终止。
If you set a char to 0, that can mean null termination, but it can also mean that you are just using the char as an 8-bit integer for calculations, rather than as part of a string. This can be confused further if you are also using pointers in the same code and compare them against zero, which is then a null pointer.
如果将 char 设置为 0,则可能意味着空终止,但也可能意味着您只是将 char 作为 8 位整数用于计算,而不是作为字符串的一部分。如果您还在同一代码中使用指针并将它们与零进行比较,则这可能会进一步混淆,零是空指针。
回答by Deep
Character array means You are taking String so I advise you to use '\0'at the end of character Array after reading all the Characters of from user because 0is simple Character but '\0'is NULLwhich indicates end of String.
字符数组意味着你正在服用的字符串,所以我建议你使用'\0'从用户阅读所有的字符,因为在之后的字符数组的结尾0是简单的人物,但'\0'就是NULL这表明字符串的结尾。

