Reputation: 141877
I wrote this function in C, which is meant to iterate through a string to the next non-white-space character:
char * iterate_through_whitespace(unsigned char * i){
while(*i && *(i++) <= 32);
return i-1;
}
It seems to work quite well, but I'm wondering if it is safe to assume that the *i
will be evaluated to false in the situation that *i == '\0'
, and it won't iterate beyond the end of a string. It works well on my computer, but I'm wondering if it will behave the same when compiled on other machines.
Upvotes: 21
Views: 739
Reputation: 2965
I find the other answers inadequate because they do not provide a direct answer to the question in the title.
Is '\0' guaranteed to be 0?
No, the integer value of the construction '\0'
is not guaranteed to be 0 by the C standard.
Regarding the null character, all we know is that (C99 p.17, C11 p.22)
[a] byte with all bits set to 0, called the null character, shall exist in the basic execution set.
and that (C99 p. 61, C11 p.69)
[t]he construction '\0' is commonly used to represent the null character.
Emphasis on "commonly used". There is no guarantee.
Upvotes: 0
Reputation: 215387
In C, '\0'
has the exact same value and type as 0
. There is no reason to ever write '\0'
except to uglify your code. \0
might however be useful inside double quotes to make strings with embedded null bytes.
Upvotes: 3
Reputation: 138171
The ASCII standard dictates that the NUL character is encoded as the byte 0
. Unless you stop working with encodings that are backwards compatible with ASCII, nothing should go wrong.
Upvotes: 0
Reputation: 263497
Yes -- but in my opinion it's better style to be more explicit:
while (*i != '\0' && ...
But the comparison to 32
is hardly the best approach. 32
happens to be the ASCII/Unicode code for the space character, but C doesn't guarantee any particular character set -- and there are plenty of control characters with values less than 32 that aren't whitespace.
Use the isspace()
function.
(And I'd never name a pointer i
.)
Upvotes: 14
Reputation: 182664
The standard says:
A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string.
Upvotes: 16