Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 14 Mar 2012 18:56:22 -0500 Message-ID: <4F613025.6244@mindspring.com> Date: Wed, 14 Mar 2012 18:56:21 -0500 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: A very simple parser with scanf & C References: <88940b22-00a2-4d6e-81e1-51214b073a96@fe21g2000vbb.googlegroups.com> <0.f03c4aaff4700164ddab.20110924003423BST.8762kivnnk.fsf@bsb.me.uk> <0.f62ca533fe3fac1321b9.20110924024253BST.87zkhuu34y.fsf@bsb.me.uk> <0.4c392657a43f99297620.20110924144538BST.87fwjmt5od.fsf@bsb.me.uk> <0.a5ee7a90b39e01baef80.20110924222135BST.87litdskkg.fsf@bsb.me.uk> <87d3eoy3p8.fsf@bazspaz.fatphil.org> <4F202866.5BEA@mindspring.com> <4F28C3E1.2C41@mindspring.com> <4F5BB70E.84B@mindspring.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 78 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.219.217 X-Trace: sv3-YFW820uRROppytWLuJxqC9cSIgkqo6Vb7ywma/xecvBIFQpLcVNelY1ZSPvQ3mwNudq50gSmi+r2WUP!9AsNogniMEM+FHh6Zv+MOQNQIWiBkfOop+MCnSAKjDRrKMxmUIqB6MFc1atDm8lj2BuwuKnQhvYX!xrJPjUXtjO4= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4597 Xref: csiph.com comp.lang.c:18788 Tim Rentsch wrote: > > pete writes: > > > Tim Rentsch wrote: > >> > >> pete writes: > >> > >> > Tim Rentsch wrote: > >> >> > >> >> pete writes: > >> >> > >> >> > Tim Rentsch wrote: > >> >> > > >> >> >> [snip] > >> > > >> > If you're going to say for {char c;} > >> > > >> >>> isalpha( * (unsigned char*) &c ) > >> >>> > >> >>> rather than > >> >>> > >> >>> isalpha( (unsigned char ) c ) > >> > > >> > then I think we should consider a case where > >> > ( * (unsigned char*) &c ) > >> > is different from > >> > ( (unsigned char ) c ) > >> > and the only case I can think of, > >> > is where (c) is signed, negative and not twos complement. > >> > >> Right (ignoring padding bits in char, which no sane > >> implementation has), although actually there is one other > >> case, namely a two's complement trap representation. Not > >> very likely perhaps but it is allowed, so for the sake of > >> completeness I think it should be mentioned. (And now it > >> has been...) > > > > Considering that file operations are at the root of this thread: > > > > If char is signed, and char c is assigned a value of (-1), as in > > char c; > > c = -1; > > > > and if (c) is written to a file using fputc, > > then the value written into the file will be ((unsigned char)-1). > > > > If that same byte is read back from the same file using fgetc, > > then the value returned by fgetc will be > > ((int)(unsigned char)-1) > > which is equal to ((unsigned char)c) > > regardless of the representation of negative integers. > > > > The return value of fgetc will be unequal to > > ( * (unsigned char*) &c ) > > when negative integers are either sign and magnitude > > or one's complement. > > That's right, of course. Which form should be used depends > on where the bytes came from in the first place -- if they > had been written with fwrite(), for example, then the type > punning form, rather than the ordinary converting form, is > probably a better choice. And conversely. > > I suspect this is all a moot point anyway, since the values > that might be affected most likely will all have the same > characteristics vis-a-vis the functions. So I > will just say thank you for an interesting discussion. As far as the ctype functions go, if you want use isalpha to give you some idea of whether or not putchar(c) is going to output an alpha, then isalpha((unsigned char)c) is the way to do it, because the value returned by putchar(c) compares equal to ((unsigned char)c). -- pete