Someone please tell me the difference between 0 and '\0' and NULL.

I thought I knew it! 0 and NULL is the same thing "Essentially".. '\0' is a null terminator for a string..

It's just sometimes when I convert a string to char array and do some encryption on it, I see TWO null terminating characters.. wth? The second one should never be there and should never get reached because the string terminates as it hits the first.. Thing is.. WHY does this second NULL Terminator appear out of NO where? Now here is where the problem lies in my mind.

I can convert all the chars in the chararray (INCLUDING the NULL Terminator) to ASCII codes and they all convert perfectly fine into the (Int) values.. Converts vice-verse into (char) as well.. So no problem there..

Now when doing the same with Hex.. It leaves out the NULL terminator. Aka char to hex to char.. When it hits the Hex part, it is 1 char less and thus the output chararray is also 1 char less.. (The original minus the Null terminator).

Now Why does ASCII have codes for NULL Terminating characters but Hex does not? Hex has the value 0.. should it not use that?

Edited 4 Years Ago by triumphost: n/a

http://codepad.org/edEYoes9

Wow.. What in the world kinda sorcery is this?! Probably confused me even more now.. WHY?! Why does it print 0 for you and for me in console, it prints nothing? I have that exact code.. if I do cout<<hex<<'\0'; it will be blank whereas cout<<int<<'\0'; prints a value.. question is though.. Are they really all the same? That's odd.. why does converting to hex before my encryption work whereas just doing pure ascii key codes mess it up.

As I understood it 0 is the same as '\0' and NULL is defined as (void*)0. As an ASCII character though '\0' is used to terrminate a string.

NULL is defined differently in c++ -- its defined as 0, while in C its defined as (void*)0. '\0' is the same as 0. I never use NULL for anything other than initializing pointers.

Edited 4 Years Ago by Ancient Dragon: n/a

There is no difference unless you're using a class that detects the type of the data and does something different with it depending on that type. For code clarity, you should use 0 when talking about a number, '\0' when talking about a string-terminator, and NULL when talking about a pointer.

Edited 4 Years Ago by Tumlee: n/a

The difference is purely semantics (and type-related), but that doesn't make the distinction a trivial one. Just like in English, we mean different things when we say "nothing", "no-one", or "nowhere", which correspond, analogously, in C++ to 0 , '\0' , and NULL (which was actually replaced by nullptr now). Just like in English, you won't say "this argument goes 'nothing'." or "I went to the mall, but at the end, I bought 'no-one'.", or "there was 'nowhere' left at the meeting when I arrived.". In C++, it's the same thing, each of these expressions are valid and meaningful in different contexts. The fact that they might have (and usually, do have) the same value to the computer (who doesn't care about semantics or context) is mostly irrelevant (unless you're actually doing the kind of low-level bit-tweaking code that requires you to know that). Just like the fact that "nothing", "no-one" and "nowhere", or any other expression to mark the absence of something, could all be mathematically interpreted as zero (or nil, or null, depending on your particular English dialect), but that doesn't make them all the same or equivalent, because, to us, humans, semantics and contexts are things that matter a lot!

Arguably, that's the most important thing that programming languages do, i.e., provide semantics and contexts, and tools (and rules) to organize them and obey them. We need that to be able to construct good software designs. So, don't say, because different things mean the same to a computer, that they are the same. No, they're not the same to us, and that's what matters.

Edited 4 Years Ago by mike_2000_17: n/a

The difference is purely semantics (and type-related), but that doesn't make the distinction a trivial one. Just like in English, we mean different things when we say "nothing", "no-one", or "nowhere", which correspond, analogously, in C++ to 0 , '\0' , and NULL (which was actually replaced by nullptr now). Just like in English, you won't say "this argument goes 'nothing'." or "I went to the mall, but at the end, I bought 'no-one'.", or "there was 'nowhere' left at the meeting when I arrived.". In C++, it's the same thing, each of these expressions are valid and meaningful in different contexts. The fact that they might have (and usually, do have) the same value to the computer (who doesn't care about semantics or context) is mostly irrelevant (unless you're actually doing the kind of low-level bit-tweaking code that requires you to know that). Just like the fact that "nothing", "no-one" and "nowhere", or any other expression to mark the absence of something, could all be mathematically interpreted as zero (or nil, or null, depending on your particular English dialect), but that doesn't make them all the same or equivalent, because, to us, humans, semantics and contexts are things that matter a lot!

Arguably, that's the most important thing that programming languages do, i.e., provide semantics and contexts, and tools (and rules) to organize them and obey them. We need that to be able to construct good software designs. So, don't say, because different things mean the same to a computer, that they are the same. No, they're not the same to us, and that's what matters.

TL;DR In a situation where you have a choice, use the one that best fits the context

This article has been dead for over six months. Start a new discussion instead.