Character types in ANSI C

drw at cullvax.UUCP drw at cullvax.UUCP
Fri Feb 20 02:29:08 AEST 1987


cg at myrias.UUCP (Chris Gray) writes:
> I.e. which of the following are legal:
> 
>     char *p1;
>     unsigned char *p2;
>     signed char *p3;
> 
>     p1 = p2;	    /* case 1 */
>     p1 = p3;	    /* case 2 */
>     p2 = p3;	    /* case 3 */

Well, the char's are all widened into the 'appropriate' int types.
(These are called integral promotions, or some such.)  Then the
appropriate comparisons of int's and/or unsigned int's are performed.

I think that the rule for widening char's to int's is "a character
type is promoted to unsigned int if all possible values of the
character type can be represented by unsigned int, otherwise it is
promoted to int".

Thus, you get:
	p2 = p2		<->	(unsigned int)p2 = (unsigned int)p2
	p3 = p3		<->	(int)p3 = (int)p3
	p2 = p3		<->	(unsigned int)p2 = (unsigned int)(int)p3
(p1 acts like p2 or p3, depending on whether chars are signed)

Dale
-- 
Dale Worley		Cullinet Software
UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw
ARPA: cullvax!drw at eddie.mit.edu



More information about the Comp.lang.c mailing list