Embedded Learning 3 - Summary of C Data Types

1. Logical type: bool (requires header file <stdbool.h>) "non-zero is true and its value is 1, zero is false and its value is 0".

2. (32-bit system) Integer data is divided into the following categories according to byte size:
 (1) char: 1 byte
 (2) short: 2 bytes
 (3) int: 4 bytes (16-bit operating system) 2 bytes)
 (4) long: 4 bytes (8 bytes for a 64-bit operating system)
     In addition, it is necessary to distinguish between signed numbers and unsigned numbers: unsigned numbers (unsigned), whose 8 bits are Data bit, the value range is 0~255. The highest bit of a signed number (signed) is the data bit (0 is a positive number, 1 is a negative number), and the value range is -128~127.
    *In the case of data out of bounds, the system will not automatically report an error, but it cannot get the expected result.
 For example, if the signed char type data reaches 129, it will cross the boundary. Write the binary form and discuss it in detail:
129:
 Original code 1000 0001's complement
 1000 0001 's
 complement 1000 0001
     When the computer stores the data, it is treated as a signed number by default, so Treat the highest bit as the sign bit.
     So the complement code becomes a negative complement code, and the input is reversed to get the
 complement code 1000 0001 complement code
 1000 0000
 original code 1111 1111 (-127)
     The final output is -127, which is an out-of-bounds data. The so-called -128 only emphasizes a critical value (the data bit and the sign bit coincide), which is similar to the so-called -0 (complement: 1 000 0000). Different compilers operate on the margin differently. Remember not to cross the boundary.

3. Floating point type: (vim compiler displays 6-bit precision by default)
float (single-precision floating-point type) precision: 6
double (double-precision floating-point type) precision: 16  

4. char: character type


5. Constants: Quantities that cannot be changed while the program is running
(1) Integer constants: binary constants (0b), octal constants (0), decimal constants, and hexadecimal constants (0x).
(2) Floating point constant: 123.123; Scientific notation: 1.23*10^2; Exponential form: 1.23e+2
(3) Character constant: drawn with single quotation marks ''.
(4) String constant: printed with double quotation marks " ", and always ends with a '\0' character at the end. 
 
6. Variable: The amount that can be changed when the program is running, and the definition of the variable follows the
    storage type data type variable name
    There are two ways to define variables:
(1) Global variables: variables outside main{}
(2) Local variables: variables within main{} (if there are both global variables and local variables assigned to the same variable, the assignment of local variables shall prevail)
 
7. Storage type:
(1) auto: modify the local variable, if there is no storage type before the local variable, the default is auto type.
(2) register (register type): Put the modified variable in the register to improve the operating efficiency. If the register is full, it will return to the auto type by default.
(3) extern (external reference type): Refers to global variables in the same project.
    The storage type will allocate space in memory when it is defined, and the declaration of the storage type only tells the compiler that there is space for the variable, but it does not declare the open space, pay attention to the distinction.
    Finally, introduce the three functions of static:
1. Global variables limit the scope of variables and can only be used in this article.
2. function Restricts the scope of the function and can only be used in this article.
3. Local variables Change the storage location of local variables, prolong the life cycle of local variables, and keep the results of the previous operation.
 above.
*Attachment: Implicit conversion of data type (the conversion performed automatically by the computer, which may not be noticed at ordinary times): 1. Assignment: If the types on the left and right sides of the assignment symbol are inconsistent,  (1) floating-point data assignment
will occur when the hexadecimal assignment is performed
The precision of integer data will be lost
 . (2) Assigning a large data type to a small data type may cause out of bounds
. 2. Arithmetic: If the data types in the arithmetic expressions are inconsistent, the default is to convert to the largest data type.
 double <- float <- long <- int <- short <- char (32-bit operating system)
3. Signed or unsigned: The operation of signed and unsigned numbers is converted to unsigned numbers by default.
4.printf: Display conversion: (mostly used for pointer type conversion)
Format: (the data type to be converted) The target to be converted
Note : (1) When the large type turns to the small type, consider whether it is out of bounds.
            (2) The conversion of floating-point numbers to integers will lose precision.
            (3) Essence: It just shows that the result has changed, but the essence is still the original number.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324504518&siteId=291194637