4 ms·
As it happens, '\0' translates to 0. Since they are composed of characters, C programmers tend to do the c == '\0' check instead of the shorted c == 0 check, j
by Xurinos 16y ago
As it happens, '\0' translates to 0. Since they are composed of characters, C programmers tend to do the c == '\0' check instead of the shorted c == 0 check, just to make sure everyone knows we are still treating the char like a character in a string; it is just style.
char c = 65
is equivalent to and just as legal as
char c = 'A'
So yes, C strings ARE zero-terminated. In fact, it is a bit weird to say they are null-terminated. It is technically true, given that NULL often translates to 0, but NULL is a pointer value in the same way that zero is a number.
For some reason, I vaguely remember some security hack in one OS where NULL went to a special place in memory that was marked as invalid (1, 63, or something), so in that case, NULL was not defined as 0. Since the memory is vague, though, don't quote me on it.
- infinite8s 16y agoWhat people really mean is NUL terminated (NUL is the name of the '\0' character - ASCII value 0x0000). But it probably just mangled some time ago and people write it as NULL.
- nevinera 16y ago>it is a bit weird to say they are null-terminated Yeah, it would be. They're NUL-terminated.
- Xurinos 16y agoUnfortunately, "null-terminated", although incorrect, is widely used. :( http://googlefight.com/index.php?lang=en_GB&word1=%22null-terminated%22&word2=%22nul-terminated%22 http://googlefight.com/index.php?lang=en_GB&word1=%22nul... I hate all the variations of nothing. undef, null, NULL, NIL, NUL...
- tedunangst 16y agoHow can "null-terminated" be incorrect when that is the language used by the C standard?
- Xurinos 16y agoI stand corrected. I looked at my old ASCII tables. This is a silly semantic argument. http://www.asciitable.com/ http://www.asciitable.com/ The character shorthand is "NUL", but the full name is "null". I still stand by my (silly semantic) point that the use of NULL is overloaded here. NULL is a pointer value, not a character value. The string does not terminate at a pointer; it terminates with a magic character. This has, in fact, bitten me in the past where '\0' was a legitimate member of my string, and I have to write my own accessors.
- nevinera 16y agoif '\0' was a legitimate member, it was not a string, it was a character/byte array, probably of fixed size.
- nuxi 16y agoI'm not convinced that NUL vs. C's "null character" is just a silly semantic argument. NUL is an ASCII abbreviation and the implementors probably wanted to avoid being tied to a particular character encoding - there is no mention of NUL in the C standard. As for the NULL, it needn't be a pointer value, the standard permits it being "an integral constant expression with the value 0", so in certain implementations it can be the same as '\0'.
- bnoordhuis 16y agoSorry, but I can't help to nitpick: 65 is not an 'A' on systems that use EBCDIC, like z/OS and OS/400. It's not even a valid character as such, I think.