C Language Basic data type: character type (char) shaping (short, int, long) floating-point (float, double) Note: The following type of bytes refers to the general situation, different platforms will vary, specific platform can be tested with the sizeof keyword. |
Type |
Number of bytes |
Type |
Number of bytes |
Char |
1 |
Short |
2 |
Int |
2 (16-bit system) or 4 (32-bit system) |
Long |
4 |
Float |
4 |
Double |
8 |
Type |
Range |
Type |
Range |
(signed) Char |
-128~~127 |
unsigned char |
0~~255 |
(signed) Short |
-32768~~32767 |
unsigned short |
0~~65535 |
(signed) INT |
-32768~~32767 |
unsigned int |
0~~65535 |
(signed) long |
-2147483648~~2147483647 |
unsigned long |
0~~4294967295 |
Float |
-3.4*-38 ~ ~ 3.4* |
Double |
-1.7*-308 ~ ~ 1.7*10308 |
1. The operation causes the character/integral type to overflow its range of values
eg.
int Main () { char127; 1 ; printf ("%d\n", ch); return 0 ; }
The output is:-128, note that the value given when defining CH is 127, which is already the maximum number that char can represent, and the origin of the final result-128 is this:
First step: First 127 in the computer with the binary representation as: 01111111;
Step Two: 01111111 + 00000001 = 10000000;
The third step: Because CH is signed, when the highest bit is 1 o'clock, the expression is negative, and the negative number of the computer is to use the complement to store, recall the calculation of negative complement method (first take negative value, then the binary system, the binary reverse, in the inverse of the value plus 1, that is a negative complement);
Fourth step: So according to the reverse step of the complement, we come to find the most primitive negative, because the subject of the complement is 10000000, first 10000000-00000001 = 01111111, and then 01111111 take the inverse for 10000000,10000000 = 128, because itself is negative, that is, CH is-128.
eg.
int Main () { char255; 1 ; printf ("%d\n", ch); return 0 ; }
The output result is: 0;
Its analysis is: first ch = 255 promotion for shaping, in the computer storage for: (000 .... ) 11111111, then (000 ....) ) 11111111+1 = (000 ... ) 0001 00000000; then intercept the last eight-bit binary to CH, since the last eight bits are all 0, so ch = 0.
Similarly, we can also analyze the result when we encounter the above situation in shaping int short, long, and so on.
2. declare a constant with pre-processing instruction # define to indicate how many seconds are in 1 years?
#define Second_per_year (60*60*24*365) UL
① #define语法的基本常识 (for example: cannot end with a semicolon, use of parentheses, etc.)
All macro definitions, enumeration constants, read-only variables are all capitalized, and the words are split with underscores.
const int max_length = 100; This is not a constant, but a read-only variable, please look backwards
#define FILE_PATH "/usr/tmp"
② writes out how you calculate how many seconds in a year instead of calculating the actual value will be more valuable
③ realized that this expression would overflow an int, so it would be better to use a long shape, then you would add to your own
C--Data type--range of values