1. Basic data types
Test on your own computer!
Types of | Keyword | Number of bytes | Number of digits | Value |
---|---|---|---|---|
Integer | [singned] short | 2 | 16 | -32768~32767 |
[singned] int / long | 4 | 32 | -214783648~2147483647 | |
[singned] long long | 8 | 32 | -9223372036854775808―9223372036854775807 | |
unsingned short | 2 | 16 | 0~65535 | |
unsingned int / long | 4 | 32 | 0~4294967295 | |
unsingned long long | 8 | 32 | 0―18446744073709551615 | |
Floating point | float | 4 | 32 | ± (3.4×10-38~3.4×1038) |
double / long double | 8 | 64 | ± (1.7×10-308~1.7×10308 ) | |
Character type | [singned] char | 1 | 8 | -128~127 |
[unsingned] char | 1 | 8 | 0~256 |
2. Be sensitive to 2
Decimal | Binary | Decimal | Relationship with 2 | Decimal | Relationship with 2 |
---|---|---|---|---|---|
0 | 0 | 64 | 26 | 1 | 21-1 |
1 | 1 | 128 | 27 | 3 | 22-1 |
2 | 10 | 256 | 28 | 7 | 23-1 |
3 | 11 | 512 | 29 | 15 | 24-1 |
4 | 100 | 1024 | 210 | 31 | 25-1 |
5 | 101 | 2048 | 211 | 63 | 26-1 |
6 | 110 | 4096 | 212 | 127 | 27-1 |
7 | 111 | 8192 | 213 | 255 | 28-1 |
8 | 1000 | 16384 | 214 | 32767 | 215-1 |
16 | 10000 | 32768 | 215 | 65535 | 216-1 |
32 | 100000 | 65536 | 216 | 4294967295 | 232-1 |
3. Integer data storage method
1. Stored in binary form;
2. Stored in complement form, the highest bit represents the sign of the value (0 is positive, 1 is negative);
0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
---|
The first digit of the short type is 0, which means the maximum value in a positive number = 2 15 -1 = 32767;
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
---|
The first digit of the short type is 1, which means the minimum value in negative numbers =-2 15 =-32768;
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
---|
unsigned short type is unsigned value itself represents all bits: max = - 2 16 - 1 = - 65535;
4. Preliminary exploration of integer data overflow
#include <stdio.h>
int main()
{
short a = 32767;
printf("%hd\n", a + 1);
}
输出结果:
-32768
Data overflow law:
① Maximum data <=> Minimum data -1;
② Maximum data + overflow value <=> minimum data + (overflow value -1);
③ The beginning and end of the data type form a closed loop;
5. The storage principle of floating-point numbers
Floating point number X is expressed as: X=M×r E
Codename | name | Description |
---|---|---|
M | mantissa | Significant digits of the number X, its digits reflect the accuracy of the data |
r | Base, | The base of decimal is 10, the base of binary is 2 |
E | Order code | Determine the true position of the decimal point of the number X |
Types of | Number of bytes | Number of digits | Number sign S | Order code E | Mantissa M | Value | Value |
---|---|---|---|---|---|---|---|
float | 4 | 32 | First place | 8th place | 23rd place | ± (3.4×10-38~3.4×1038) | 7-bit precision |
double | 8 | 64 | First place | 11th place | 52nd place | ± (1.7×10-308~1.7×10308 ) | Accuracy 15 bits |
Calculate the value range of float type data:
①The float type has 1 sign bit, 8 exponent bits, and 23 mantissa bits;
②The index range is -128~127, and the maximum index is 127;
③The maximum mantissa is +1.11...(23 ones after the point, the decimal number is 1.8388607);
④1.8388607×2127 ≈ 2×2127 = 3.4×1038, also for negative numbers.
Floating point precision problem
#include <stdio.h>
int main()
{
double a, b, c;
a = 1234567890.123456789;
b = 987654321.987654321;
c = a + b;
printf(" %22.9lf\n", a);
printf("+ %22.9lf\n", b);
printf("------------------------\n");
printf("= %22.9lf\n", c);
return 0;
}
输出结果:
1234567890.123456717
+ 987654321.987654328
------------------------
= 2222222212.111111164
#include <stdio.h>
int main()
{
double a = 0.65f;
double b = 0.6f;
double c = a - b;
if (c == 0.05)
printf("对了!\n");
else
printf("错了!\n");
printf("%10.8lf\n%10.8lf\n", a, b);
return 0;
}
输出结果:
错了!
0.64999998
0.60000002