Re: casting to unsigned char for is*() and to*() functions



Eric Sosman wrote:
>
.... snip ...
>
> If `c' is an `int' obtained from something like getc(),
> just pass it along without casting. getc() and its ilk
> already return either EOF or a non-negative `unsigned char'
> value, which is what toupper() et al. require.

I think the point is that getc and friends do not return a char,
they all return an int. So unless the OP makes the beginners
mistake of storing that value in a char, all is correct without any
special effort. Thus the prototype for filling a char array is:

while (EOF != (ch = getc(...))) {
/* make tests on ch */
/* optionally store ch in a char array */
}

and the tests on the intermediate storage ch needs no special care,
provided that is of type int.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


.



Relevant Pages

  • Re: I have a problem in my study
    ... the OP had declared c as int - and getc() does not return char but int. ... Read your book what getc returns - an char wided to int or EOF. ...
    (comp.lang.c)
  • Re: casting to unsigned char for is*() and to*() functions
    ... - `char' had been unsigned from the git-go. ... - getc() and co. had not been designed to cater to the ... `char' to `int'. ... you'd get character values with a lot of high-order sign bits ...
    (comp.lang.c)
  • Re: StrongARM processor EOF different?
    ... > loop breaks correctly at EOF, but the same code for the StrongARM ... As others have said, getc() returns an int, and for EOF it returns an ... int value less than 0 and not equal to any valid char value. ...
    (comp.lang.c)
  • Re: undefined behaviour?
    ... Definitely undefined behaviour if fopenfailed. ... getcreturns an int. ... getc() fails and if a char is signed. ...
    (comp.lang.c)
  • [EXPL] ELOG Remote Shell Exploit
    ... char content; ... static int content_length; ... static unsigned char boundary; ... void get_server_version; ...
    (Securiteam)