Random pet peeve: '\0'

Pages: 123... 7
I don't like when I see code like this:

1
2
3
4
5
foo = '\0';

// or

if(foo != '\0')


I was going to write a rant to explain the reasons why I don't like it... but I am getting extremely sleepy so I'll just say it annoys me and I wish people wouldn't do it.

If you want to assign the integral value of 0, just assign 0. Don't assign a character with an escaped octal value of 0. That's just absurd.
1
2
3
4
5
6
size_t StrSize(char* str)
{
    size_t i = 0;
    for( ; str[i] != '\0'; ++i) { }
    return i;
}

Does your rant extend to C strings?
If you want to assign the integral value of 0, just assign 0. Don't assign a character with an escaped octal value of 0. That's just absurd.


Are you the kind of person who would use 0 instead of NULL?

We don't have a nullchr thing, so some of us will use the next best thing, '\0'.
1
2
3
4
5
6
size_t StrSize(char* str)
{
    size_t i = 0;
    for( ; str[i] != 0; ++i) { }
    return i;
};
Last edited on
For C++, MSDN lists \0 as a special escape sequence (not just 0 in octal), as does cppreference.com.

http://msdn.microsoft.com/en-us/library/6aw8xdf2%28v=vs.80%29.aspx
http://en.cppreference.com/w/cpp/language/escape
Disch doesn't like something, everyone stop doing it!
@Catfish3: I would use 0 instead of NULL in C++03 because I don't like Windows.h

Also, std::string isn't null-terminated so I don't see a purpose of writing an algorithm for only null-terminated strings.
stewbond wrote:
Does your rant extend to C strings?


Yes:
 
for( ; str[i]; ++i) { }


Catfish3 wrote:
Are you the kind of person who would use 0 instead of NULL?


Considering NULL is a macro... yes... absolutely.

Though I do use nullptr when null'ing pointers. Though I don't null pointers very often.

Catfish3 wrote:
We don't have a nullchr thing, so some of us will use the next best thing, '\0'


The only time that would make sense is if you have to differentiate between int and char versions of function overloads/templates based on the parameter.

But in the case of templates, I would rather just explicitly name the type in the template params rather than use '\0'. And if you have a function overload whose only difference is char vs. int (and their behavior isn't interchangable).... then your code has bigger problems.

Zhuge wrote:
For C++, MSDN lists \0 as a special escape sequence (not just 0 in octal), as does cppreference.com.


That's retarded. It isn't a special escape sequence. It's just a common octal escape sequence.

I can all but guarantee that no compiler anywhere treats it as a special escape sequence.

This is actually why this is such a pet peeve of mine. It's taught to be some kind of special escape sequence when it's not.
Last edited on
> I would use 0 instead of NULL in C++03 because I don't like Windows.h
¿?

"hello\0world"
Not sure what you're trying to say ne555.
¿How is `Windows.h' related to NULL?
For Windows compilers and Windows headers, NULL is only defined in Windows.h, and not including Windows.h means that NULL is never defined (not even in any of the C/C++ standard library headers). That's how messed up Windows compilers are.
@LB I have NULL defined in stdio.h
Like I said, not with Windows & Windows compilers. At least not with my setup.
"Windows compilers"? Does MinGW count? Or do you mean MSVC exclusively?

Can you compile this?

1
2
3
4
5
6
#include <cstddef>

int main()
{
    void *pv = NULL;
}

VS2012 can.
For C++, MSDN lists \0 as a special escape sequence (not just 0 in octal), as does cppreference.com.


The standard does not.

       Table 7 — Escape sequences
new-line         NL(LF)   \n
horizontal tab   HT       \t
vertical tab     VT       \v
backspace        BS       \b
carriage return  CR       \r
form feed        FF       \f
alert            BEL      \a
backslash        \        \\
question mark    ?        \?
single quote     ’        \’
double quote     "        \"
octal number     ooo      \ooo
hex number       hhh      \xhhh


Although, the standard does use '\0' to refer to the terminating character of C-style strings in several places.
VS2012 can.

Yeah but you should use nullptr in that case.
@Catfish3 MSVC, and it doesn't compile in my build environment.

That's not the only thing - the <c*****> headers don't even wrap anything in the std:: namespace like the standard mandates. There's macro guards for if you wanted to disable putting them in the std namespace, but they're pointless because they guard nothing (literally just blank lines between them).
@LB What version of VS, it compiles in 2010 and 2012 for me.
Pages: 123... 7