fixed length data types in C/C++

后端 未结 11 1016
春和景丽
春和景丽 2020-12-23 14:34

I\'ve heard that size of data types such as int may vary across platforms.

My first question is: can someone bring some example, what goes wrong, when p

11条回答
  •  爱一瞬间的悲伤
    2020-12-23 14:56

    Well, first example - something like this:

    int a = 45000; // both a and b 
    int b = 40000; // does not fit in 2 bytes.
    int c = a + b; // overflows on 16bits, but not on 32bits
    

    If you look into cstdint header, you will find how all fixed size types (int8_t, uint8_t, etc.) are defined - and only thing differs between different architectures is this header file. So, on one architecture int16_tcould be:

     typedef int int16_t;
    

    and on another:

     typedef short int16_t;
    

    Also, there are other types, which may be useful, like: int_least16_t

提交回复
热议问题