\0 is the way the NULL termanator is expressed in C and its various offspring. \0 must be appended to the end of all strings (character arrays, in actuality), or bad things will happen.

Something I found hard to grasp is that both of those chars- '\' and '0'- count as one char. Because of this, char arrays must be declared with one more spot than they really have.

And yes, you are very savvy.

Just to clarify anotherone a little, it is called (according to the ascii manpage) a C program escape. The null escape corresponds to the ASCII value 0, in other words '/0' == 0 for those rare few of you coders that aren't ASCII savvy (unlike danthaman). Interesting side note: null is the default contents of the memory addresses of unassigned pointers. Anyhow, trying to write to memory address null results in a Segfault or GPF (or some weird bomb thing for you Mac users). Reading a character array as anotherone described results pretty much in the same thing. No worries though, by default ( at least on my linux box) every fourth character or so is a null in an array, and C's pretty good at sticking them on when it needs to.

\0 is the C escape sequence for a string terminator.

To further clarify here, a character array is really just a list of numbers representing characters in ASCII. For instance, '32' is a space. In C, a string's end is marked by assigning that character position the value 0. '\0' is, as XCthulhu noted, an escape sequence.

char c = '\0';

and

char c = 0;

are totaly equivalent in every way. \0 does NOT insert a "\", then a "0" into the character stream. You need the extra spot for the zero-value, not because you're also inserting a slash.

Also, null is not the default content of a memory address of an unassigned pointer. Null is equivalent to zero, while unassigned, non-zeroed memory is considered random. It isn't, of course. For instance, you see far more zeroes than you would in random distribution, which probably accounts for XCthulhu's 'every fourth character' thing (no OS, to my knoweldge, randomly zeroes unallocated memory for no specific reason).
On a Sun computer, you can type this null character by pressing the blank key near the top left of the keyboard. If you are typing into an xterm, you will probably see the notation ^@ appear under your cursor. ^@ (Ctrl-@, or C-@ to emacs users) is another representation of the same character -- ASCII zero, or NUL.

(My boss and I "discovered" this last week when we realized that neither of us had any idea why Sun keyboards had a blank key. We both felt a bit like idiots, since we'd both worked on Suns before -- and it really is rather obvious that a blank key next to ESC should generate NUL.)

Log in or register to write something here or to contact authors.