int开发者_运维知识库 main()
{
unsigned int b;
signed int a;
char z=-1;
b=z;
a=z;
printf("%d %d",a,b);
}
gives -1 -1. why does no sign extension occur, ALSO, when does it occur?
Sign extension DID occur, but you are printing the results incorrectly. In your printf you specified %d
for b
, but b
is unsigned, you should have used %u
to print b
.
printf does not know the type of its arguments and uses the format specifies to interpret them.
printf("%d %u",a,b);
Because printf
looks at the raw memory, not the type. use %u
to print the value as unsigned.
See.
http://ideone.com/Qpcbg
精彩评论