What is the difference between using (char)0 and '\0' to denote the terminating null character in a character array?
What is the difference between using (char)0 and ‘\0’ to denote the terminating null
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 typeint, and indeedintis used to handle characters in other contexts too, such as the return value offgetc. So'\1'means “the numeric value as an int, of the character whose numeric value is 1”, and so on.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 as1in 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.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 your0is soon going to implicitly convert tochartype. Similarly, it can help to writeNULLwhen you mean a null pointer constant,0.0when you mean a double with value 0, and so on.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 as0, it needs to be cast tocharin 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'vs0.NULLhas implementation-defined type — sometimes it needs to be cast to a pointer type, since it may have integer type.0.0has typedouble, so is certainly different from0. Still,float f = 1.0;is identical tofloat f = 1;andfloat f = 1.0f, whereas1.0 / i, whereiis an int, usually has a different value from1 / i.So, any general rule whether to use
'\0'or0is 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 macroASCII_NUL.[*] 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\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.