From: Austin Ziegler Date: 2006-02-16T14:03:33+09:00 Subject: Re: boolean annoyance On 2/8/06, Claudio Jeker wrote:> On Thu, Feb 09, 2006 at 09:15:54AM +0900, Austin Ziegler wrote:> > On 08/02/06, Claudio Jeker wrote:> > >> It's actually the most illogic part of languages like C that treat 0> > >> as false. Believe me, having spent most of my programming life using> > >> C/C++ and being used to that, I make mistakes regularly whilst writing> > >> Ruby code.> > >>> > >> 0 is an integer and quite often a valid value. C's treatment of 0 as> > >> false is convenient in some situations but horribly inconvenient in> > >> others. Because if I'm expecting an integer and zero is a legal> > >> value, then you have to start playing around in your conditionals...> > >> "Okay, today any -1 is false"... or even at different levels than> > >> that. Part of the problem is that C doesn't have an actual NULL...> > >> NULL is just defined as zero. Overlap, explosions, crash and burn...> > > If you are inspecting a integer against nothing end everything is just> > > legal, why are you inspecting it?> >> > Sorry, but that doesn't work. If you have something that returns an> > integer value -- consider strtol(3). This can return *any* valid integer> > value, but if you get a 0, LONG_MAX, or LONG_MIN, you have to *then*> > check errno to see if the conversion was, in fact, successful. (And the> > conversion could be unsuccessful for any number of reasons.)> This is not correct. strtol(3) does not return 0 in case of an error. It> sets errno to ERANGE and returns LONG_MAX or LONG_MIN. It is correct. I had looked at the man page for strtol(3) beforeposting. Trying to convert a non-numeric value (say, ":") or using abase of 40 will result in 0 with a result of EINVAL (although it isoptional to have EINVAL in case of a non-conversion result).Therefore, you *cannot* trust a result of 0 and must always checkerrno if you get 0, LONG_MAX, or LONG_MIN. Please consider that I do know what I'm talking about. strtol(3) would have been better if passed the target value as aparameter and return a success or failure code *or* accepted asuccess/failure code as a parameter and returned the result. -austin--Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca