Re: casting to unsigned char for is*() and to*() functions
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Wed, 29 Jun 2005 21:48:39 -0500
On 29 Jun 2005 07:28:23 -0700, mr_semantics@xxxxxxxxxxx wrote in
comp.lang.c:
> I have been reading about the practise of casting values to unsigned
> char while using the <ctype.h> functions. For example,
>
> c = toupper ((unsigned char) c);
>
> Now I understand that the standard says this about the <ctype.h>
> functions:
>
> "The header <ctype.h> declares several functions useful for classifying
> and mapping characters.166) In all cases the argument is an int, the
> value of which shall be representable as an unsigned char or shall
> equal the value of the macro EOF. If the argument has any other value,
> the behavior is undefined."
>
> I am having a hard time formulating my question - basically its like
> this though - Some people say cast to unsigned char (as in the above
> example), whereas I have seen some people argue that casting to
^^^^^^^^^^^
Who are these "some people"? What are their qualifications to offer
advice on this subject?
On an implementation where plain char is signed (which is quite
common, especially on x86 systems, meaning Windows, most Linux, and
soon to be Macintosh), then this:
char ch = CHAR_MIN;
int uc = toupper(ch);
....produces undefined behavior, unless the macros CHAR_MIN and EOF
happen to be equal, not likely. The very words you quoted above
specifically say so.
So your "some people" are very foolish if they are offering advice
based on fundamental misconception.
> unsigned char is unecessary, and if it is done, then a recast back to
> int is necessary, because functions like toupper() expect an int, eg,
If the same foolish "some people" are saying this, then they are
actually beyond misconception and have arrived at ignorance. The only
advice they are qualified to give about C is perhaps how to spell the
name of the language. And even then I would double check their
answer.
>
> toupper( (int)((unsigned char) c) );
>
> So what is the right thing to do? Cast to unsigned char? Cast to
> unsigned char and back to int?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- References:
- casting to unsigned char for is*() and to*() functions
- From: mr_semantics
- casting to unsigned char for is*() and to*() functions
- Prev by Date: Re: Floatin point issues
- Next by Date: Re: what's wrong with atof() and casting?
- Previous by thread: Re: casting to unsigned char for is*() and to*() functions
- Next by thread: Re: Object persistence in C
- Index(es):
Relevant Pages
|