How to limit the size of variable types, such as long int in a 64-bit system?

So, long int on a 32-bit system is 32 bits, and on a 64-bit system - 64 bits. Now I am writing code on a 32-bit system without worrying about overflow. If I run the same code on a 64-bit system, all my long variables will unnecessarily take up twice as much memory, which is a waste. So, how to limit the size of a variable in a 64-bit system.

For example, I have a variable long long int Count. Now Count can contain a maximum of 63 bits in my design. If I run the same code on a 64-bit system, Count will be 128 bits, but according to the logic, the remaining 64 bits will be wasted. So, I want to make sure that the graph accepts only the 64-bit version on a 64-bit system.

+4
source share
2 answers

So, long int on a 32-bit system is 32 bits, and on a 64-bit system 64 bits.

Not necessary. They can be 64 bits. Or they can be 32 bits.

So, how to limit the size of a variable in a 64-bit system.

Instead of using primitive default types in C, such as long, you should use int32_tfrom stdint.h. In contrast long, it is fully portable and has a deterministic size.

+3
source

"" , , int32_t int64_t. , (., , - ):

-, :

6.2.5 (5)... '' plain int , ( , INT_MIN INT_MAX, ).

"" , char, short int, int, long int long long int.

5.2.4.2.1 (1) , , , #if . , CHAR_BIT MB_LEN_MAX, , , , , . , , ( ) , .

:

7.20.1.1

(1) typedef intN_t N, . , int8_t 8 .

(2) typedef uintN_t N no . , uint24_t 24 .

(3) . , 8, 16, 32, 64 , ( ), , typedef.

, "", , /, typedef. , , , , .

+2

Source: https://habr.com/ru/post/1679214/


All Articles