Absolute size of 'short'
Mike Maloney
mike at ISIDAPS5.UUCP
Tue Aug 2 06:53:42 AEST 1988
Dear C-Heavies,
Is the size of a (signed or unsigned) short integer guarenteed to
be two bytes? I need to manipulate and compare some unsigned ints
modulo 65536. It would be clean and convenient to just let the
machine handle my wrap-around from 0 to 0xffff and verse-vica.
The following works fine on my Xenix-386 machine, but is it portable?
Note that the default integer size is 4 bytes, so the expression 'u'
inside the printf's is being converted.
unsigned short u;
u = 0xffff;
u++;
printf("0x%x\n",u); ==> 0x0
u = 0;
u--;
printf("0x%x\n",u); ==> 0xffff
Note however:
u = 0xffff;
u + 1 != 0 ==> In the expression 'u + 1', 'u' is upwardly
converted to 4 bytes before adding 1.
but
(unsigned short)(u + 1) == 0
--
Mike Maloney "The nice thing about standards
Integral Systems, Inc. is that there are so many to
Lanham Maryland choose from" - Murphy
More information about the Comp.lang.c
mailing list