Why is that?

W

William L. Bahn

ozbear said:
Hmmm... dunno where the '\' went but indeed it was a typo.
Using the unadorned 0 is a better idea, at least in this case.
Thanks for the correction.

This can be argued both ways:

IF the goal is to write code based on defined behavior (as
opposed to defined implementation), then mychar = 0 should be
avoided because it relies on a detailed knowledge of the
implementation requirements for the NUL character.

HOWEVER, by using that knowledge (which the next person reading
the code may not have) an easy to make and hard to find logic
error can be avoided.
 
A

Arthur J. O'Dwyer

This can be argued both ways:

IF the goal is to write code based on defined behavior (as
opposed to defined implementation), then mychar = 0 should be
avoided because it relies on a detailed knowledge of the
implementation requirements for the NUL character.

HOWEVER, by using that knowledge (which the next person reading
the code may not have) an easy to make and hard to find logic
error can be avoided.

I don't think you understand. The integer constants 0, '\0', 0x0,
00000, and (1/42) are all equivalent, by definition. It doesn't
matter what implementation you're using. Zero is zero, according
to the Standard.

So the only question is, when do you prefer to use which "flavor"
of zero? And that's purely a style decision.

For the record, I've never encountered the '\0' <==> '0' bug Dan
mentioned, but I can definitely see how it could be a problem. But
no bigger problem than mistyping '(int)123' for '(int)213' or something.
I'm not changing my religion over it. ;)

-Arthur
 
D

Dan Pop

In said:
This can be argued both ways:

IF the goal is to write code based on defined behavior (as
opposed to defined implementation), then mychar = 0 should be
avoided because it relies on a detailed knowledge of the
implementation requirements for the NUL character.

Please enlighten us about the differences between 0 and '\0'.
Ditto about the implementation requirements for the null character.

In my naivety, I thought that 0 and '\0' have the same type and
value on any conforming implementation and that the null character
is fully specified by the language definition. What am I missing?

Dan
 
D

Dan Pop

For the record, I've never encountered the '\0' <==> '0' bug Dan
mentioned, but I can definitely see how it could be a problem. But

I mentioned it in a post including code posted by Oz that contained
an instance of that very bug!

Dan
 
O

ozbear

I mentioned it in a post including code posted by Oz that contained
an instance of that very bug!

Indeed....an example of an attempt at "clarity" doing exactly the
opposite of what was intended.

Time to grep for '0'... :O)

Oz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top