The real difference between “int” and “unsigned int”

后端 未结 9 547
时光取名叫无心
时光取名叫无心 2020-12-12 16:13

int:

The 32-bit int data type can hold integer values in the range of −2,147,483,648 to 2,147,483,647. You may also refer to this data type as sig

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 16:32

    He is asking about the real difference. When you are talking about undefined behavior you are on the level of guarantee provided by language specification - it's far from reality. To understand the real difference please check this snippet (of course this is UB but it's perfectly defined on your favorite compiler):

    #include 
    
    int main()
    {
        int i1 = ~0;
        int i2 = i1 >> 1;
        unsigned u1 = ~0;
        unsigned u2 = u1 >> 1;
        printf("int         : %X -> %X\n", i1, i2);
        printf("unsigned int: %X -> %X\n", u1, u2);
    }
    

提交回复
热议问题