public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* egcs, casting the argument of eg isalpha to an int...
@ 1998-02-13 17:41 Kaveh R. Ghazi
  1998-02-14  4:36 ` Manfred Hollstein
  1998-02-16  3:54 ` Lassi A. Tuura
  0 siblings, 2 replies; 6+ messages in thread
From: Kaveh R. Ghazi @ 1998-02-13 17:41 UTC (permalink / raw)
  To: egcs

	I'm getting a lot of warnings building egcs of the form
"subscript has type `char'".  These seem to occur because the ctype
is* functions (eg isalpha) are used and passed a `char' type.  These
functions are prototyped in ctype.h as taking an int parameter but
they are often defined as macros which do a lookup in an array like
this:

 > #define isalpha(__c) (__ctype2[__c]&(_A))

So this appears to be the source of the warning.


	Now system.h has custom versions of the ctype macros which
look like this:

 > #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))

I'm thinking of changing them to this:

 > #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (((int)(c))))

This would seem to avoid the warning.

	Is this solution correct from a technical standpoint?

		Thanks,
		--Kaveh
--
Kaveh R. Ghazi			Project Manager / Custom Development
ghazi@caip.rutgers.edu		ICon CMT Corp.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: egcs, casting the argument of eg isalpha to an int...
  1998-02-13 17:41 egcs, casting the argument of eg isalpha to an int Kaveh R. Ghazi
@ 1998-02-14  4:36 ` Manfred Hollstein
  1998-02-15  8:56   ` Kamil Iskra
  1998-02-16  3:54 ` Lassi A. Tuura
  1 sibling, 1 reply; 6+ messages in thread
From: Manfred Hollstein @ 1998-02-14  4:36 UTC (permalink / raw)
  To: ghazi; +Cc: egcs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1340 bytes --]

On Fri, 13 February 1998, 15:08:23, ghazi@caip.rutgers.edu wrote:

 > 	I'm getting a lot of warnings building egcs of the form
 > "subscript has type `char'".  These seem to occur because the ctype
 > is* functions (eg isalpha) are used and passed a `char' type.  These
 > functions are prototyped in ctype.h as taking an int parameter but
 > they are often defined as macros which do a lookup in an array like
 > this:
 > 
 >  > #define isalpha(__c) (__ctype2[__c]&(_A))
 > 
 > So this appears to be the source of the warning.
 > 
 > 
 > 	Now system.h has custom versions of the ctype macros which
 > look like this:
 > 
 >  > #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (c))
 > 
 > I'm thinking of changing them to this:
 > 
 >  > #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (((int)(c))))
 > 
 > This would seem to avoid the warning.
 > 
 > 	Is this solution correct from a technical standpoint?
 > 

This yields wrong results for e.g. bloody german umlauts with the
high bit set:

int my_isalpha (int c)
{
	printf ("%d, '%c'\n", c, c);
	return 0;
}

main ()
{
	my_isalpha ((int) 'A');
	my_isalpha ((int) 'ä');
}

gives:

65, 'A'
-28, 'ä'

the following cast sequence would be better:

	my_isalpha ((int) ((unsigned char) 'A'));
	my_isalpha ((int) ((unsigned char) 'ä'));

This gives:

65, 'A'
228, 'ä'

Manfred

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: egcs, casting the argument of eg isalpha to an int...
  1998-02-14  4:36 ` Manfred Hollstein
@ 1998-02-15  8:56   ` Kamil Iskra
  0 siblings, 0 replies; 6+ messages in thread
From: Kamil Iskra @ 1998-02-15  8:56 UTC (permalink / raw)
  To: Manfred Hollstein; +Cc: egcs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 656 bytes --]

On Sat, 14 Feb 1998, Manfred Hollstein wrote:

> the following cast sequence would be better:
> 
> 	my_isalpha ((int) ((unsigned char) 'A'));
> 	my_isalpha ((int) ((unsigned char) 'ä'));
> 
> This gives:
> 
> 65, 'A'
> 228, 'ä'

But why the explicit cast from unsigned char to int? The following should
be enough:

	my_isalpha ((unsigned char) 'A');
	my_isalpha ((unsigned char) 'ä');

/ Kamil Iskra    AmigaOS  Linux/i386  Linux/m68k               \
| GeekGadgets GCC maintainer   UNIX system administrator       |
| iskra@student.uci.agh.edu.pl  kiskra@ernie.icslab.agh.edu.pl |
\ kamil@dwd.interkom.pl   http://student.uci.agh.edu.pl/~iskra /


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: egcs, casting the argument of eg isalpha to an int...
  1998-02-13 17:41 egcs, casting the argument of eg isalpha to an int Kaveh R. Ghazi
  1998-02-14  4:36 ` Manfred Hollstein
@ 1998-02-16  3:54 ` Lassi A. Tuura
  1 sibling, 0 replies; 6+ messages in thread
From: Lassi A. Tuura @ 1998-02-16  3:54 UTC (permalink / raw)
  To: egcs

Kaveh R. Ghazi wrote:
|> I'm thinking of changing them to this:
|> 
|>  > #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (((int)(c))))
|> 
|> This would seem to avoid the warning.
|> 
|> 	Is this solution correct from a technical standpoint?

Often characters must be casted to `unsigned char' before being used
with is* functions.  The reason is that the functions require the
argument to be an int with non-negative or EOF value.  If you pass in a
char and chars are signed, a character with the high bit set receives
incorrect treatment (sign extension). Thus, if you got the character by
scanning a string, convert it to `unsigned char' first to make sure this
does not happen.  If you got it from fgetc, no conversion is necessary
-- but you must make sure you use int in the first place, and not
convert the returned value to char. 

//lat
--
Lassi.Tuura@cern.ch          There's no sunrise without a night


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: egcs, casting the argument of eg isalpha to an int...
  1998-02-15 19:08 Kaveh R. Ghazi
@ 1998-02-16  3:18 ` Manfred.Hollstein
  0 siblings, 0 replies; 6+ messages in thread
From: Manfred.Hollstein @ 1998-02-16  3:18 UTC (permalink / raw)
  To: ghazi; +Cc: egcs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1073 bytes --]

On Sun, 15 February 1998, 22:08:04, ghazi@caip.rutgers.edu wrote:

 >  > From: Manfred Hollstein <manfred@s-direktnet.de>
 >  > 
 >  >  > I'm thinking of changing them to this:
 >  >  > 
 >  >  >  > #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (((int)(c))))
 >  >  > 
 >  >  > This would seem to avoid the warning.
 >  >  > 
 >  >  > 	Is this solution correct from a technical standpoint?
 >  >  > 
 >  > 
 >  > This yields wrong results for e.g. bloody german umlauts with the
 >  > high bit set:
 >  > 
 >  > 
 >  > the following cast sequence would be better:
 >  > 
 >  > 	my_isalpha ((int) ((unsigned char) 'A'));
 >  > 	my_isalpha ((int) ((unsigned char) 'ä'));
 >  > 
 >  > Manfred
 > 
 > 	I think you may be right except for the isascii function which
 > is supposed to work for all int values.  The rest are valid only for
 > an int whose value is representable in an unsigned char.
 > 
 > 	So perhaps isascii's argument gets cast to int, the rest get
 > cast to unsigned char?

Ooops, didn't thought of that. I guess this way is the wright one.

Manfred

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: egcs, casting the argument of eg isalpha to an int...
@ 1998-02-15 19:08 Kaveh R. Ghazi
  1998-02-16  3:18 ` Manfred.Hollstein
  0 siblings, 1 reply; 6+ messages in thread
From: Kaveh R. Ghazi @ 1998-02-15 19:08 UTC (permalink / raw)
  To: manfred; +Cc: egcs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 952 bytes --]

 > From: Manfred Hollstein <manfred@s-direktnet.de>
 > 
 >  > I'm thinking of changing them to this:
 >  > 
 >  >  > #define ISALPHA(c) (IN_CTYPE_DOMAIN (c) && isalpha (((int)(c))))
 >  > 
 >  > This would seem to avoid the warning.
 >  > 
 >  > 	Is this solution correct from a technical standpoint?
 >  > 
 > 
 > This yields wrong results for e.g. bloody german umlauts with the
 > high bit set:
 > 
 > 
 > the following cast sequence would be better:
 > 
 > 	my_isalpha ((int) ((unsigned char) 'A'));
 > 	my_isalpha ((int) ((unsigned char) 'ä'));
 > 
 > Manfred

	I think you may be right except for the isascii function which
is supposed to work for all int values.  The rest are valid only for
an int whose value is representable in an unsigned char.

	So perhaps isascii's argument gets cast to int, the rest get
cast to unsigned char?

		--Kaveh
--
Kaveh R. Ghazi			Project Manager / Custom Development
ghazi@caip.rutgers.edu		ICon CMT Corp.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1998-02-16  3:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-02-13 17:41 egcs, casting the argument of eg isalpha to an int Kaveh R. Ghazi
1998-02-14  4:36 ` Manfred Hollstein
1998-02-15  8:56   ` Kamil Iskra
1998-02-16  3:54 ` Lassi A. Tuura
1998-02-15 19:08 Kaveh R. Ghazi
1998-02-16  3:18 ` Manfred.Hollstein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).