Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #16554

Re: A very simple parser with scanf & C

Message-ID <4F28C3E1.2C41@mindspring.com> (permalink)
Date 2012-01-31 23:47 -0500
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: A very simple parser with scanf & C
References (15 earlier) <j5o09d$eul$1@dont-email.me> <ln39fk2r28.fsf@nuthaus.mib.org> <kfn39b4g3py.fsf@x-alumni2.alumni.caltech.edu> <4F202866.5BEA@mindspring.com> <kfnpqdz1kwh.fsf@x-alumni2.alumni.caltech.edu>

Show all headers | View raw


Tim Rentsch wrote:
> 
> pete <pfiland@mindspring.com> writes:
> 
> > Tim Rentsch wrote:
> >
> >> My understanding is that a cast to (unsigned char) may not
> >> give exactly the right behavior.  In particular, if we have
> >> a variable 'char c;' then to call a function like 'isalpha()'
> >> we need to do
> >>
> >>     isalpha( * (unsigned char*) &c )
> >>
> >> rather than
> >>
> >>     isalpha(   (unsigned char )  c )
> >>
> >> to get exactly the same behavior as happens for getchar(), etc.
> >>
> >> Of course, on most current machines these two methods give
> >> the same results, but as a matter of correctness the first
> >> form is the better one.  Right?
> >
> >
> > I don't think so.
> > If you were to output the value of (c) using fputc,
> > then the return value of the function call would be
> >  ((int)(unsigned char)c).
> >
> > Th value of (c) "converted to unsigned char" is ((unsigned char)c).
> 
> That's right but the parameter c is of type 'int', not type
> char.  Where did this 'int' value come from?  Presumably it
> came from (among other places) an input function like 'fgetc()'
> (see below).
> 
> > N869
> > 4.9.7.3 The fputc function
> > Synopsis
> >          #include <stdio.h>
> >          int fputc(int c, FILE *stream);
> > Description
> >    The fputc function writes the character specified by c
> >    (converted to an unsigned char ) ...
> 
> I no longer use N869, but looking at C99 (the wording is basically
> unchanged from C90) for fgetc(), the description says fgetc() reads
> the next character "as an 'unsigned char' converted to an 'int'"
> (the single quotes designate C program text).  Note especially
> the first word there, "as".
> 
> I take this phrase to mean the character object is read _as_ an
> 'unsigned char' (eg, through an 'unsigned char *' pointer), rather
> than being _converted_ to 'unsigned char' (eg, through a 'char *'
> pointer and then casting that value to 'unsigned char').
> 
> Otherwise, this sentence would have said it reads the next
> character "as a 'char' converted to an 'unsigned char' converted
> to an 'int'.  But it doesn't say that.
> 
> Presumably the "ctype.h" functions are meant to work correctly
> on values returned by fgetc(), etc.
> 
> > Also, isalpha also has to be able to work with arguments
> > which may be constant expressions that don't have addresses.
> >
> > Given
> >
> > #define NEG_A   ('A' - 1 - (unsigned char)-1)
> >
> > then isalpha ((unsigned char)NEG_A) should return nonzero
> >
> > and putchar((unsigned char)NEG_A) should return ('A')
> 
> That's true but irrelevant to the point under discussion -- the
> expression here doesn't involve characters with negative values.
> The example I gave (with a variable 'char c') was just an example
> for when there is a variable present.  More generally, if you want a
> function like 'isalpha()' but which takes a 'char' valued argument,
> this can (and, I would argue, should) be done as follows
> 
>   #define char_isalpha(c) \
>     isalpha( (int) (union {char c; unsigned char u;}){ c }.u )
> 
> to match the description of how fgetc() works.

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.


-- 
pete

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A very simple parser with scanf & C pozz <pozzugno@gmail.com> - 2011-09-23 07:03 -0700
  Re: A very simple parser with scanf & C Nobody <nobody@nowhere.com> - 2011-09-23 18:28 +0100
  Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 18:29 +0100
    Re: A very simple parser with scanf & C pozz <pozzugno@gmail.com> - 2011-09-23 23:16 +0200
      Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-24 00:09 +0100
        Re: A very simple parser with scanf & C pozz <pozzugno@gmail.com> - 2011-09-24 09:32 +0200
  Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-23 21:24 +0100
    Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 22:00 +0100
      Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-24 00:04 +0100
        Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-24 00:34 +0100
          Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-24 01:00 +0100
            Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-24 02:42 +0100
              Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-24 12:13 +0100
                Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-24 12:25 +0100
                Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-24 15:05 +0100
                Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-24 14:45 +0100
                Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-24 17:45 +0100
                Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-24 22:21 +0100
                Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-25 00:52 +0100
                Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-25 01:54 +0100
                Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-25 02:51 +0100
                Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-25 12:22 +0100
                Re: A very simple parser with scanf & C Gene <gene.ressler@gmail.com> - 2011-09-25 01:35 -0700
                Re: A very simple parser with scanf & C pete <pfiland@mindspring.com> - 2011-09-25 08:41 -0400
                Re: A very simple parser with scanf & C Gene <gene.ressler@gmail.com> - 2011-09-25 11:23 -0700
                Re: A very simple parser with scanf & C Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-25 19:41 +0300
                Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-25 19:45 +0100
                Re: A very simple parser with scanf & C Keith Thompson <kst-u@mib.org> - 2011-09-25 12:10 -0700
                Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-25 20:43 +0100
                Re: A very simple parser with scanf & C James Kuyper <jameskuyper@verizon.net> - 2011-09-25 15:55 -0400
                Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-26 11:07 +0100
                Re: A very simple parser with scanf & C Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-26 11:40 -0400
                Re: A very simple parser with scanf & C Keith Thompson <kst-u@mib.org> - 2011-09-26 12:22 -0700
                Re: A very simple parser with scanf & C pacman@kosh.dhis.org (Alan Curry) - 2011-09-26 21:19 +0000
                Re: A very simple parser with scanf & C Keith Thompson <kst-u@mib.org> - 2011-09-26 16:11 -0700
                Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-26 23:26 +0100
                Re: A very simple parser with scanf & C Keith Thompson <kst-u@mib.org> - 2011-09-26 12:18 -0700
                Re: A very simple parser with scanf & C Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-27 02:27 +0300
                Re: A very simple parser with scanf & C Keith Thompson <kst-u@mib.org> - 2011-09-27 10:06 -0700
                Re: A very simple parser with scanf & C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-27 21:10 +0100
                Re: A very simple parser with scanf & C Seebs <usenet-nospam@seebs.net> - 2011-09-26 22:59 +0000
                Re: A very simple parser with scanf & C Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-09-30 05:18 -0700
                Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-30 17:06 +0100
                Re: A very simple parser with scanf & C Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-25 18:05 -0400
                Re: A very simple parser with scanf & C Keith Thompson <kst-u@mib.org> - 2011-09-25 15:30 -0700
                Re: A very simple parser with scanf & C Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 14:01 -0800
                Re: A very simple parser with scanf & C pete <pfiland@mindspring.com> - 2012-01-25 11:05 -0500
                Re: A very simple parser with scanf & C Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 16:01 -0800
                Re: A very simple parser with scanf & C pete <pfiland@mindspring.com> - 2012-01-31 23:47 -0500
                Re: A very simple parser with scanf & C Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 09:21 -0800
                Re: A very simple parser with scanf & C pete <pfiland@mindspring.com> - 2012-03-10 15:18 -0500
                Re: A very simple parser with scanf & C Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-13 15:49 -0700
                Re: A very simple parser with scanf & C pete <pfiland@mindspring.com> - 2012-03-14 18:56 -0500
                Re: A very simple parser with scanf & C Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-15 17:37 -0700
                Re: A very simple parser with scanf & C pete <pfiland@mindspring.com> - 2012-03-17 00:24 -0500
                Re: A very simple parser with scanf & C Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-21 05:21 -0700
    Re: A very simple parser with scanf & C pozz <pozzugno@gmail.com> - 2011-09-23 23:23 +0200
      Re: A very simple parser with scanf & C "BartC" <bc@freeuk.com> - 2011-09-23 23:53 +0100
  Re: A very simple parser with scanf & C Rui Maciel <rui.maciel@gmail.com> - 2011-09-25 14:07 +0100

csiph-web