簡體   English   中英

unsigned short int與unsigned int的區別如何

[英]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但為什么在不相同的casez

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM