Hi,

I am a newbie, can someone please clarify the difference between
"\0" and '\0' and 0 as far as C++ is concerned?? Why can't they be used inter-changeably ??

Recommended Answers

All 11 Replies

The first one is an empty string, the second one is a single character 0 identical to the third one.

To me, when doing C/C++:

0 would digit zero, that is, a numerical value.

'0' could be the character capital oh or the character zero. For example:
char word[10] = "Oxford";
char number[10] = "01234";

Depending on typeface used 'O' may look exactly like '0' making it difficult to tell them apart out of context.

'\0' is the null character used to terminate strings in C/C++.

"\0" is an empty string.

Thanks Lerner!!

To me, when doing C/C++:

0 would digit zero, that is, a numerical value.

'0' could be the character capital oh or the character zero. For example:
char word[10] = "Oxford";
char number[10] = "01234";

Depending on typeface used 'O' may look exactly like '0' making it difficult to tell them apart out of context.

'\0' is the null character used to terminate strings in C/C++.

"\0" is an empty string.

The first one is an empty string, the second one is a single character 0 identical to the third one.

Thanks Ancient Dragon!!

'\0' is the null character used to terminate strings in C/C++.

which is 0. Why? because the backlash '\' tells the compiler to interpret the next character litterally, it is one of the several escape characters in the c language.

I always hesitate a while before engaging someone more learned than I in (even polite) refutation but sometimes I can't help myself. Usually I learn something from the process of humiliation at least. So here goes.

If \0 is the same as 0 then why isn't \t the same as t or \n the same as n? I know \ is the "escape operator/character" but I thought it meant to not interpret the next char literally. That is, \t means t isn't the letter t but the tab character and \n means n isn't the letter n but the newline character, etc. So, by extension, \0 isn't the letter 0 but the null character and putting single quotes around \0 just makes it explicitly understood that \0 is an instance of a character type.

To my understanding 0 (but not '0' or 'O' or \0 or '\0') equates (usually) to NULL (or maybe it's the other way around), but not to \0. But then I've been wrong before, and I'm sure I'll be wrong again in the future, even if I'm not wrong this time.

For example I have no problem theoretically doing either of these:

int * ptr = NULL;
int * ptr2 = 0;

though I find the second example to be a little strange and usually have to do a double take when I see it in code I haven't personally written.

But, if:
char word[3];
and:
word[0] = 'h';
word[1] = 'i';

I would have trouble, in theory, terminating the char array like this:
word[2] = 0;
whereas this seems fine:
word[2] = '\0';
as would this:
word[2] = \0;

I guess I'll have to try it out on the compiler at home when I get there later, if I'm not trying too hard to pull the foot out of my mouth at that time.

>If \0 is the same as 0 then why isn't \t the same as t or \n the same as n?
AD wasn't being quite clear when he said "literally". '\0' isn't a special escape sequence like '\t' or '\n', it's a manual escape sequence where you define the integral value for the character. In this case, it's an octal constant with the value of 0. You can say '\1' and create an octal constant with the value of 1, or a hexadecimal constant '\x4E' for the letter 'N'.

'\t' and friends are less flexible because they map only to one character with an unspecified value and a specific task. For example, '\t' might map to '\x9' in ASCII.

>To my understanding 0 (but not '0' or 'O' or \0 or '\0') equates
>(usually) to NULL (or maybe it's the other way around), but not to \0.
0, NULL, and '\0' will produce the same integral value in C++. In C, 0 and '\0' will produce the same value, but NULL might be implicitly cast to a pointer.

>as would this:
>word[2] = \0;
That's actually a syntax error. You can only use an escape sequence within a string or character literal.

>>I would have trouble, in theory, terminating the char array like this:
>>word[2] = 0;

Theory doesn't always hold water. Coding the above is perfectly legal in both C and C++. I know of no compiler that will produce either errors or warnings on that line.

See, now I've learned lots of new things today (quess how I chose my screen name?).

I just compiled/ran my sample code snippets and am happy to confirm that AD was correct in all respects, as usual, though I would gladly have bet a beer/cola/lunch at McDonald's to the contrary earlier. Thank you for not rubbing it in too hard.

I also want to thank Narue for her explanations; very informative, as usual.

See, now I've learned lots of new things today (quess how I chose my screen name?).

I just compiled/ran my sample code snippets and am happy to confirm that AD was correct in all respects, as usual, though I would gladly have bet a beer/cola/lunch at McDonald's to the contrary earlier. Thank you for not rubbing it in too hard.

I also want to thank Narue for her explanations; very informative, as usual.

Great. We all learn new things occasionally. Its when you stop learning that you want to start worrying :)

Well Thanks to all of you that was quite informative!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.