开发者

Understanding Sign extension

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜