c question

fawcett at tahoma.UUCP fawcett at tahoma.UUCP
Fri Feb 13 08:17:45 AEST 1987


I have come across something that I did not expect to find.  I was trying
to print out a special character to the terminal and got a response other
that what I expected.  The code I used was:

main()
{
	union
	{
		char full_message;
		int _message;
	}u;	

	u.full_message = 0xaa;

	printf("%10x\n", (int)u.full_message);
	printf("%10x\n", u._message);
}

What I got when I ran this was:

  ffffffaa
  aa000000

I have tried various other methods for doing this to no avail.
When I tried to print the number as an integer, it worked as expected.
Does anyone know what I am doing wrong, or help me to understand why I got
this response.

John W. Fawcett				Voice: (206) 237-5080
Boeing Commercial Airplane Company	UUCP: ..!uw-beaver!ssc-vax!shuksan
P.O. Box 3707, M/S 66-04			!tahoma!fawcett
Seattle, WA  98124-2207



More information about the Comp.lang.c mailing list