[英]How does unsigned short int differs from unsigned int
#include<stdio.h>
int main()
{
unsigned short int x=-10;
short int y=-10;
unsigned int z=-10000000;
int m=-10000000;
printf("x=%d y=%d z=%d m=%d",x,y,z,m);
return 0;
}
output = x=65526 y=-10 z=-10000000 m=-10000000
我的查詢是如何unsigned short int
不同於unsigned int
的數據保持的情況。 即x=65526 where as z=-10000000 why x is not equal -10 where as z can hold any data
作為短是2字節如此twos complement -10 is 65526
但為什么在不相同的case
的z
當unsigned short int x=-10;
發生, x
,無符號,得到模數或“包裹”值65526
(OP sizeof(短)為2)。 類似x = power(256,sizeof(unsigned short)) - 10
。
當打印x
,它作為int
(可變參數提升)傳遞給printf()
)。 OP的sizeof(int)是4,所以65526適合int
。 然后printf()
看到%d
並打印“65526”。
z
有一個類似的故事,但sizeof(z)是4.並初始化z = power(256,sizeof(unsigned)) - 10
。
你的printf()
使用%d
說明符進行unsigned
。 OP應該使用%u
。
printf("x=%u y=%d z=%u m=%d",x,y,z,m);
unsigned short int
保證至少覆蓋0到65535的范圍。
unsigned int
保證至少覆蓋unsigned short int
的范圍。 它可能涵蓋更廣泛的范圍。
unsigned int
通常是處理器最佳使用的原生大小 - 通常最快。
由於unsigned short int
在某些實現中小於unsigned int
。 優選使用大型陣列節省空間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.