Conversions/casts one more time

Adam V. Reed adam at mtund.UUCP
Wed Dec 17 13:15:17 AEST 1986


> Keywords:
> 
> My program:
> >> unsigned char ucf() { return 0xaa; }
> >> 
> >> main() {
> >> 	char c;
> >> 	unsigned char uc = 0xaa;
> >> 
> >> 	c = uc;
> >> 	printf("\tc = %x, (char)uc = %x, (char)ucf() = %x\n",
> >> 	  c, (char)uc, (char)ucf());
> >> }
> 
> Adam Reed (mtund!adam) writes:
> >Ugh. %x expects an int, so the result of feeding it a char is,
> >*and ought to be*, UNDEFINED.
> 
> Golly!  Guess they've changed the language on me again.  Last I heard,
> char variables were converted to int whan passed as parameters.  That meant
> that when I did something like this I could see what the compiler produced
> when it converted the expression to int.  Guess I'll have to go back to writing
> 	int i, j, k;
> 	...
> 	i = c;
> 	j = (char)uc;
> 	k = (char)ucf();
> and looking at the result with adb.  It's SO hard to keep up with these things!
> 
> In case anyone wonders, the point of this question was that the 4.3 VAX C
> compiler sometimes throws away casts when it's doing promotions.  I don't
> believe this is correct, and those who have addressed that issue (by mail as
> well as by posting) have agreed.  Thanks to all of you who wrote.

Alternatively, and as I assumed, casting an argument may be interpreted by
your compiler as a directive to promote it to the type specified by the
cast, instead of the default (K&R p.24) int. Given the obvious utility of
such a feature, I still suspect that this is what it does. Could you check?
					Adam Reed (mtund!adam)



More information about the Comp.lang.c mailing list