firstly,i tried sizeof(signed short), output 2 bytes.
but when tried check hex representation of signed short, turns out 4 bytes :
#include <stdio.h> void main(){ signed short test ; test=-17; /* number */ printf(" -17 when viewed signed short hexa \t %x\n ", test); }`
the output :
-17 when viewed signed short hexa ffffffef
ffffffef means 32 bits not 16 bits !
the specifier %x
requires integer, , compiler automatically converts short
int
before print it.
if type signed type compiler performs sign extension, means if value negative propagate upper word of int. i.e. if have -17=0xffef
short converting int have oxffffffef
. if had positive value 17=0x11
, 0x11
int , print out 0x11
.
test made makes no sense.
on other hand size of type (int
, short
, etc) compiler and/or machine dependent, , using sizeof()
operator correct way check size.
Comments
Post a Comment