cnul

Is '\0' guaranteed to be 0?


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.


Solution

  • 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.