开发者

Understanding Sign extension

开发者 https://www.devze.com 2023-03-30 01:12 出处:网络
int开发者_运维知识库 main() { unsigned int b; signed int a; char z=-1; b=z; a=z; printf(\"%d %d\",a,b);
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

精彩评论

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