In-depth analysis of C language data types and value ranges

Data Storage Overview

Variables in C language have different data types, and the value space of each data type is different. Therefore, variables of different data types have different value spaces. In the C language program, most of the various types of variables are described in decimal form, but in fact these variables are stored in the form of binary in the computer , and C language allows direct bit operations on the binary to complete special request.

A bit (bit, also known as "bit", abbreviated b) in C language can store a binary number (0 or 1), and a byte (Byte, abbreviated B) can store 8 bits (bit).

Data storage is in "byte" (Byte) , and data transmission is mostly in "bit" (bit). In a computer, a string of numbers is processed or calculated as a whole called a word. Words are usually divided into bytes. In memory, each cell usually stores one word, so each word can be addressed. The length of a word is expressed in bits (the word length refers to the number of binary digits that a computer can process at one time). Such as 32-bit and 64-bit operating systems, word length is an important factor to measure computer performance.

The amount of information storage is a measure of the amount of programs and data stored in the memory , and a bit stores a binary number, that is, 0 or 1, which is the smallest storage unit. 8 binary is a byte unit, an English letter (partial case) and English punctuation occupies one byte space (8bit), a Chinese character and Chinese punctuation occupy two byte space (16bit).

The relationship between computer storage units is as follows :

1 byte (B yte) = 8 bits (bit) 1 byte (Byte) = 8 bits (bit)1 byte ( By t e ) _=8 bits ( bit ) 1 KB (kilobyte) = 1024 B 1KB (kilobyte) = 1024B
1 KB ( kilobytes ) _=1024 B
1 MB (megabyte) = 1024 KB 1MB (megabyte) = 1024KB1 MB ( megabytes )=1024 KB 1 GB
(gigabyte) = 1024 MB 1GB (gigabyte) = 1024MB1 GB ( gigabytes )=1024 MB
1 TB (Trillion bytes) = 1024 GB 1TB (Trillion bytes) = 1024GB1 TB ( terabytes )=1024 GB
1 PB (Petabyte) = 1024 TB 1PB (Petabyte) = 1024TB1 PB ( Petabytes )=1024 TB
1 EB (Exabyte) = 1024 PB 1EB (Exabyte) = 1024PB1 exabyte ( exabytes )=1024 PB
1 ZB (ten trillion terabytes) = 1024 EB 1ZB (ten trillion terabytes) = 1024EB1 ZB ( ten trillion terabytes )=1024 EB
1 YB (one billion quintillion bytes) = 1024 ZB 1YB (one billion quintillion bytes) = 1024ZB1 Y B ( one trillion quintillion bytes )=1024 ZB
1 BB (hundred billions of billions of bytes) = 1024 YB 1BB (hundred billions of billions of bytes) = 1024YB1 BB ( hundred billion petabytes )=1024YB

basic data type

Programming languages ​​generally provide several different data types to meet the needs of programming.
The following are several basic data types:

1) Character type: represented by char
2) Integer type: represented by int
3) Single-precision real number type: represented by float
4) Double-precision real number type: represented by double
5) Empty type: represented by void

binary representation of an integer

Integers are divided into two types: unsigned and signed. Signed integers can be positive or negative. The sign is represented by the highest bit of the byte. 0 means positive and 1 means negative. . as follows:

1) Signed binary integer
10110100, its highest bit is the sign bit, therefore, ( 10110100 ) 2 (10110100)_2(10110100)2The decimal number of is − ( 2 5 + 2 4 + 2 2 ) = − 52 -(2^5+2^4+2^2) = -52(25+24+22)=−52 . _ while( 00110100 ) 2 (00110100)_2(00110100)2The decimal number is 2 5 + 2 4 + 2 2 = 52 2^5+2^4+2^2 = 5225+24+22=52 .
It should be noted that in order not to waste the storage space of the computer, there are different treatments for "positive zero" and "negative zero". For "positive zero" (00000000), it represents 0, and for "negative zero" (10000000), it represents -128.
2)Unsigned binary integer
The highest bit of 0 or 1 does not represent a sign bit, but a specific value. For example,( 10110100 ) 2 (10110100)_2(10110100)2, its decimal number is ( 2 7 + 2 5 + 2 4 + 2 2 ) = 180 (2^7+2^5+2^4+2^2) = 180(27+25+24+22)=180

Binary representation of floating point numbers

The representation of floating-point numbers in the computer can be divided into single-precision floating-point numbers and double-precision floating-point numbers according to the number of bytes allocated by the system. Usually, the computer will allocate 4 bytes to single-precision floating-point numbers and 8 bytes to double-precision float. When the computer stores floating-point numbers, it needs to convert decimal floating-point numbers into binary representation. The conversion method is to first convert the floating-point number into an integer part and a pure decimal part, and then convert the integer part and the pure decimal part into binary respectively.

1) For the integer part, divide by 2 and take the remainder until the quotient is zero. The first remainder is the highest bit, and the last integer bit is the lowest bit. For example, the decimal number 56 can be expressed in binary by dividing by 2 and taking the remainder: 5 6 10 = ( 111000 ) 2 56_{10} = (111000)_25610=(111000)2

insert image description here

2) The fractional part is multiplied by 2 and rounded until the remaining decimals are 0 or meet the precision requirements. The highest bit of the integer obtained first is the highest bit, and the last integer is the lowest bit. For example: decimal 0.432, the binary representation can be obtained by multiplying by 2 and taking the remainder: ( 0.432 ) 10 = ( 0.011 ) 2 (0.432)_{10} = (0.011)_2(0.432)10=(0.011)2

insert image description here

So you can convert the floating point number 4.625 into binary number as: ( 56.432 ) 10 = 111000.01 1 2 (56.432)_{10} = 111000.011_{2}(56.432)10=111000.0112

The storage of floating-point numbers follows the IEEE754 standard, and any binary floating-point number can be expressed in the following form:

insert image description here

Ranges

The integer (int) data type can also be used with the following four modifiers to describe the length and value range of the data:

1) signed (signed)
2) unsigned (unsigned)
3) long (long)
4) short (short)

The character (char) data type can only be modified with singned (signed) or unsigned (unsigned).

  1. 1 byte
    Character type: 【char】 The value range is ASCII characters
    Unsigned character type: 【unsingned char】 The value range is 0~255
    Signed character type: 【signed char】 The value range is -128~127
  2. 2 bytes
    Integer: [ int] The value range is -32768~32767
    Signed integer type: [ signed int] The value range is -32768~32767
    Unsigned integer type: [ unsigned int] The value range is 0~65535
    Short integer type: [ short int】The value range is -32768~32767
    Signed short integer: the value range is 【singned short int】-32768~32767
    Unsigned short integer: the value range is 【unsinged short int】0~65535
  3. 4 bytes
    Long integer: [ long int] The value range is -2147483648~2147483647
    Signed long integer: [ signed long int] The value range is -2147483648~2147483647
    Unsigned long integer: [ unsinged long int] The value range is 0~4294967295
    Single-precision real number type: 【float】value range is about ± 3.4 × 1 0 ± 38 \pm 3.4 \times10^{\pm38}±3.4×10± 38
    double-precision real number type: [double] the value range is about± 1.7 × 1 0 ± 308 \pm 1.7 \times 10^{\pm308}±1.7×10±308

The data type and byte length shown above are the standards that are suitable for the compilation environment of C language. Different compilation systems will expand the data type and byte length.

Guess you like

Origin blog.csdn.net/m0_67021058/article/details/130027645