data type
-
Basic type
-
Integer type
type Storage size (bytes) char 1 unsigned char 1 signed char 1 int 2 or 4 unsigned int 2 or 4 short 2 unsigned short 2 long 4 unsigned long 4 -
Floating point type
type Storage size (bytes) Precision (significant digits) float 4 6 double 8 15 long double 16 19
-
-
Enumeration type
-
Void type: refers to that the memory occupied by variables is zero, void *pointer_name only indicates the address and does not declare the memory size (the object size is unknown)
-
Derived type
- Pointer type
- Array type
- Structure type
- Common body type
- Bit field type
- Function type
variable
Declaration: assure the compiler that variables will exist with the specified name and type, so that the compiler can further compile without knowing the specific details
Definition: establish storage space for declared variables (reserve memory to store variables to be input)
Unless the extern keyword is used, the variable definition will be completed directly at the time of declaration
When declaring variables, use the following structure:
// Declaration & definition type var_name; type var_name1, ..., var_nameN; // assignment type var_name = var_value; // Use the extern keyword, only declare, not define extern type var_name
Among them, the main function of type is to declare variable types, which is to allocate memory space for variables. By specifying specific types, the compiler can know the memory size to be reserved
When the variable changes beyond the specified memory space, memory overflow will occur and unexpected results may be returned
When declaring pointer variables, it is slightly different:
// Declaration & definition type *pointer_name; // The pointer pointing to the pointer can continue extrapolation type **pointer_name; // assignment type *pointer_name = mem_addr;
Where, the asterisk * indicates that the variable is a pointer variable (tell the compiler that it is only an address), and the type indicates the type of object that the variable points to (tell the compiler how far to find the object after reaching the address)
Storage class
Storage classes define the visibility and life cycle of variables in the program. The storage classes available in C language are:
- auto: default storage class for all local variables
- Register: a local variable stored in a register (not RAM). It has no memory address. The maximum size is equal to the size of the register (usually one word)
- static:
- When decorating a local variable, you can maintain the variable throughout the program life cycle without creating / destroying it every time you enter / leave the scope
- Modifying a global variable limits the scope of the variable to the file in which it is declared
- extern: provides a reference to a global variable to inform the compiler that the variable will be defined in other files
function
Definition method:
reture_type func_name(params_list) { function_body; }
Definition method of function pointer:
return_type (*func_pointer)(param1_type, ..., paramN_type); // assignment return_type (*pointer_name)(param1_type, ..., paramN_type) = & some_func;
enumeration
Complete the mapping from name to integer in batch. If the assignment is not displayed, the first element is 0 by default, and the non first element is the previous element + 1
If the integer corresponding to the element is discontinuous, the enumeration cannot be traversed
Definition method:
enum tag { element_name1 = some_int; ... element_name2 = some_int; } var_name;
structural morphology
A structure used to store multiple different types of data. The occupied memory size is the sum of all members
Definition method:
struct tag { type member1; ... type memberN; } var_name; // Declaration & definition struct tag var_name; // assignment struct tag var_name = {member1, ..., memberN}; // Assign values to members var_name.memberX = member_value;
Common body
Specifies a memory space that can store multiple types of data. The size of the space is determined by the member occupying the largest memory
Only one member can be used at a time (when a member is stored, other members will be affected due to shared memory)
Definition method:
union tag { type member1; ... type memberN; } var_name; // Declaration & definition union tag var_name; // assignment var_name.memberX = member_value;
Bit domain
Specify the number of bits that members will use (no greater than the type to which they are attached) to save memory When the original memory space is insufficient to store new members, a new memory space will be created (the size depends on the attachment type of new members)
Definition method:
struct tag { type member1 : bits; ... type memberN : bits; } var_name;
typedef keyword
It is used to define the alias of data type, and it can also simplify the declaration of structure data
Definition method:
typedef struct tag { type member1; ... type memberN; } struct_name; // Declaration & definition struct tag var_name; struct_name var_name;
const keyword
Used to indicate that a variable cannot be reassigned, making it a constant If you try to assign a value to a variable modified by const, you will cause an error (so you usually need to complete the assignment at the time of declaration). Observe the behavior of const itself, which is similar to "write protection" of variables
For defined constants, the coding specification requires that their names should be capitalized to remind programmers that "this is a constant"
usage method:
// Use for general variables type const var_name = var_value; // Use for pointers: // The variable pointed to by the protection pointer (the resident living in the address) remains unchanged, but the pointer (address) itself is variable type const *pointer_name = var_value; // The protection pointer (address) remains unchanged, but the variable pointed to (the resident of the address) is variable type * const pointer_name = var_value; // At the same time, protect the pointer and the variables it points to type const * const pointer_name = var_value;
The more common usage scenario is not to directly define constants, but to modify the formal parameters of the function to avoid the function making unexpected changes to the incoming parameters:
return_type func_name(type const *pointer_param) { function body; }