The value range of the data type of c++ learning

foreword

To learn a language is to learn data types. Let's take a deeper look at the value range of C++ data types.

text

char

  • Normally, if char is defined as a signed type, its value range is usually -128 to 127 (or -127 to 127 on some platforms, depending on the compiler). This is because the char type usually occupies a byte (8 bits), one of which is used to represent the sign bit.

  • If char is defined as an unsigned type, its value range is usually 0 to 255, because unsigned char does not contain negative numbers.

short

  1. Signed short (signed short):

Usually takes 2 bytes (16 bits).
Values ​​usually range from -32768 to 32767.
One of the bits is used to represent the sign bit, so it can represent negative and non-negative numbers.
Unsigned short (unsigned short):

  1. Usually also takes 2 bytes (16 bits).
    The value range is usually from 0 to 65535.
    An unsigned short only represents non-negative numbers.
    These two types of short are the same in storage capacity, both occupying 2 bytes, but due to the different nature of symbols, their value ranges are also different.

int

  • signed int (signed int):

Typically takes 4 bytes (32 bits).
Values ​​usually range from -2147483648 to 2147483647.
One of the bits is used to represent the sign bit, so it can represent negative and non-negative numbers.

  • unsigned int (unsigned int):

Typically takes 4 bytes (32 bits).
Values ​​usually range from 0 to 4294967295.
Unsigned int only represents non-negative numbers.
These two types of int are the same in storage capacity, both occupying 4 bytes, but due to the different nature of the symbols, their value ranges are also different.

long

  • Signed long (signed long):

The value range of the signed long type is usually compiler and platform dependent, but it is usually at least as large as the int type, usually 4 or 8 bytes.
The value range is usually from -9223372036854775808 to 9223372036854775807 if it is 8 bytes in size.

  • unsigned long (unsigned long):

The range of unsigned long is also compiler and platform dependent, but it is usually at least as large as unsigned int, usually 4 or 8 bytes.
The value range is usually from 0 to 18446744073709551615, if the size is 8 bytes.

float

In C++, the value range of the float type generally follows the IEEE 754 floating-point number standard, which defines the rules for the representation and operation of floating-point numbers. The float type usually occupies 4 bytes (32 bits), and its value range is as follows:

Minimum positive normalized number (smallest positive non-zero number): approximately 1.4013e-45
Maximum normalized number: approximately 3.4028e+38
The float type can represent decimal and integer values, but be aware that its precision is limited because it There are only 32 bits, so there may be a loss of precision when representing very large or very small numbers. Floating point numbers can also have rounding errors, especially when doing floating point arithmetic.

double

In C++, the value range of the double type generally follows the IEEE 754 floating-point standard, which defines the rules for the representation and operation of floating-point numbers. The double type usually occupies 8 bytes (64 bits), and its value range is as follows:

Minimum positive normalization number (minimum positive non-zero number): about 4.9407e-324
Maximum normalization number: about 1.7977e+308
Unlike float, the double type has higher precision, so it can represent a larger range and Higher precision values. This makes the double type widely used in scientific computing, engineering, and other fields that require high-precision numbers.

Guess you like

Origin blog.csdn.net/wniuniu_/article/details/132720209