Re: size_t

Discussion in 'C Programming' started by Dan Pop, Jun 24, 2003.

  1. Dan Pop

    Dan Pop Guest

    In <> "Arthur J. O'Dwyer" <> writes:


    >On Mon, 23 Jun 2003, Peter Nilsson wrote:
    >>
    >> Kalle Olavi Niemitalo <> wrote...
    >> > "Arthur J. O'Dwyer" writes:
    >> >
    >> > > int c;
    >> > > while ((c=getchar()) != EOF) {
    >> > > if (isalpha((unsigned char)c)) ...
    >> > > }
    >> > >
    >> > > doesn't invoke undefined behavior for any user input,
    >> > > even the ones where c is, say, a (non-EOF) negative number.

    >>
    >> The (unsigned char) cast fixes nothing since c is derived from
    >> getchar().

    >
    >I.e., the cast is not needed, because fgetc() returns "an unsigned char
    >converted to an int". I had not known that; thanks. However, I don't
    >see any similar guarantee for the chars returned by fgets(), so just
    >make an appropriate substitution, or tell me I'm wrong again. :)


    fgets() doesn't return any chars, it returns a pointer.

    >> > I don't think getchar() can return a non-EOF negative number,
    >> > unless INT_MAX < UCHAR_MAX which AFAIK doesn't occur on any
    >> > hosted implementation.

    >
    >You're correct. But in the case of fgets() and friends, unless I'm
    >mistaken, there's nothing prohibiting the user from inputting negative
    >values directly (by whatever means his system does input), if 'char' is
    >a signed type on his implementation.
    >
    >Does it say anywhere that fgets() must act as if each input character was
    >read via fgetc()?


    Yes (see the quote in my other post), but this doesn't solve the problem
    if plain char is signed and cannot represent the value returned by fgetc.

    When calling <ctype.h> functions on characters from a string, either use a
    pointer to unsigned char to access the string or use an explicit cast
    to unsigned char on the char value. In theory, the former method is
    more reliable (it is not perfectly clear what happens with byte values
    that cannot be represented as char values), in practice both work.

    The way the string was initialised simply does not matter in the context
    of this discussion. Well, if it was initialised with a string literal
    containing only characters from the basic source character set, no cast
    is needed, because all the character values are guaranteed to be positive.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jun 24, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howard Hinnant

    Re: size_t ... standards

    Howard Hinnant, Jun 29, 2003, in forum: C++
    Replies:
    5
    Views:
    3,554
    Jim Fischer
    Jun 30, 2003
  2. Howard Hinnant

    Re: size_t ... standards

    Howard Hinnant, Jun 29, 2003, in forum: C++
    Replies:
    0
    Views:
    832
    Howard Hinnant
    Jun 29, 2003
  3. Chris \( Val \)

    Re: for(size_t a=begin();a!=end();++a){}

    Chris \( Val \), Jul 13, 2003, in forum: C++
    Replies:
    2
    Views:
    348
    John Harrison
    Jul 14, 2003
  4. Alex Vinokur
    Replies:
    9
    Views:
    773
    James Kanze
    Oct 13, 2008
  5. Alex Vinokur
    Replies:
    1
    Views:
    565
Loading...

Share This Page