Detail the size and representation range of the basic data types in C and C++

  • 2020-06-03 07:20:21
  • OfStack

The main problem of this paper is about the size and representation range of basic data types int, long, long long, float, double, char and string in C and C++, which are detailed as follows.

1. Summary of the size and scope of the base type (the following is the default under 32-bit operating system) :

Byte: byte; A: bit.

1. Short integer short: Memory size: 2byte=16bit;

Range: -32768~32767; (i.e., 15 ~ 2-2 ^ ^ 15-1)

2. Integer int: Memory size: 4byte=32bit;

Range: -2147483648~2147483647; (i.e., - 2 ^ 31 ~ 2 ^ 31-1)

unsigned: Memory size: 4byte=32bit;

Range: 0~4294967295; (i.e., 0 ~ 2 ^ 32-1)

3. Long integer long: Memory size: 4byte=32bit;

Range: -2147483648~2147483647; (i.e., - 2 ^ 31 ~ 2 ^ 31-1)

unsigned long: Memory size: 4byte=32bit;

Range: 0~4294967295; (i.e., 0 ~ 2 ^ 32-1)

short, int and long are all signed by default. long and int each take up 4 bytes of space. What is the difference between them?

16-bit operating system: long: 4 bytes, int: 2 bytes

32-bit operating system: long: 4 bytes, int: 4 bytes

64-bit OS: long: 8 bytes, int: 4 bytes

The number of bytes of int in different bit operating systems is different. If you want to write a portable program, you'd better modify int type with long when 16-bit and 32-bit operating systems were popular in the early years. Now, int is quite popular for 32-bit and 64-bit operating systems. Of course, it all depends on how you interpret it, since they take up a variable number of bytes in different operating systems, so each has its own application, not to say whether it's good or bad.

Here are some science-based rules for them:

The language of C requires that the number of bytes occupied by long should not be less than that of int and int should not be less than that of short, regardless of the platform.

4. Character char: Memory size: 1byte=8bit;

Can express range: not sure!! ;

unsigned char: Memory size: 1byte=8bit;

Range: 0~255; (2 ^ 0 ~ 8-1)

singned char: Memory size: 1byte=8bit;

Range: -128~127; (2 ^ 7 ~ 2 ^ 7-1)

The default type of char is either unsigned or signed, depending on the compiler. You can test the symbol type of char for the compiler under 1.

5. Boolean bool: Memory size: 1byte=8bit;

Range: can only take two values false or true; So the minimum is 0 and the maximum is 1.

6. Single precision float: Memory size: 4byte=32bit;

The range can be expressed :(1.17549 e-038)~(3.40282e+038); // Note: Floating-point Numbers are stored in memory according to scientific counting method. The precision of floating-point Numbers is determined by the number of mantisas.

7. Double precision double: Memory size: 8byte=32bit;

The range that can be expressed :(2.22507 e-308)~(1.79769e+308);

Note: How to distinguish and use these two floating point types, the precision of the first float and double, seven float retain the decimal point, and keep double behind the decimal point 16, 6 float can guarantee a valid number, and 15 double can guarantee valid number, if not in pursuit of the precision of cases with float better, of course, save memory, if you need a high precision, better use double, at ordinary times we are using double floating-point variables 1, After all, the precision is high, the loss of 1 - level precision cannot be ignored.

8. String string: because string belongs to a class type in c + +, basic data types, not class cannot calculate the in memory of size, must use sizeof (string) to calculate words, 1 is calculated results sizeof (string) = 4 byte, if string string content are many, obviously, it is not the true size, string class functions such as: the size of calculating the byte size (), length ().

conclusion

That is the end of this article on the size and presentation scope of the basic data types in C and C++, which I hope will be helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: