Keyword Classification Interpretation
In C language, keywords are identifiers with special meanings and purposes, used to define variable types, control program flow, specify storage attributes, etc. Below is a simple classification interpretation of these keywords:
-
Storage Class Keywords: such as
<span>auto</span>,<span>register</span>,<span>static</span>,<span>extern</span>, used to define the storage location and scope of variables. For example,<span>static</span>is used to define static variables, whose values persist between function calls. -
Type Definition Keywords: such as
<span>char</span>,<span>int</span>,<span>float</span>,<span>double</span>,<span>signed</span>,<span>unsigned</span>,<span>long</span>,<span>short</span>, etc., used to define basic data types. -
Control Flow Keywords: such as
<span>if</span>,<span>else</span>,<span>switch</span>,<span>case</span>,<span>for</span>,<span>while</span>,<span>do</span>,<span>break</span>,<span>continue</span>,<span>goto</span>,<span>default</span>, used to control the program execution flow. -
Other Keywords: such as
<span>const</span>used to define constants,<span>volatile</span>used to define variables that may be changed unexpectedly,<span>typedef</span>used to define new type names, etc.
Byte Order and Bit Order
Byte order and bit order are the same, using big-endian format. In big-endian format, the most significant byte of data is stored at the lowest memory address during data transmission and storage. For example, a 32-bit integer <span>0x12345678</span> is stored in big-endian format as: address 0 stores <span>12</span>, address 1 stores <span>34</span>, address 2 stores <span>56</span>, address 3 stores <span>78</span>.
Structure Alignment Modulus
The structure alignment modulus is the size of the largest member in the structure, or an integer multiple of its size. The purpose of alignment is to improve data access efficiency and avoid performance issues caused by unaligned data. For example, if a structure contains an <span>int</span> (4 bytes) and a <span>char</span> (1 byte), the alignment modulus of that structure is 4 bytes, and the total size of the structure is 8 bytes (4-byte alignment).
Type Definition
Using <span>typedef</span> allows the definition of new type names, improving code readability and maintainability. For example:
typedef struct student { char name[20]; int age; float score;} student_t;
Here, a new type <span>student_t</span> is defined to represent a student structure.
Enumeration Type
The enumeration type belongs to the integer type, and its enumeration constants are replaced by corresponding integer values at compile time. For example:
typedef enum color { red, white, black, green, color_num} color_t;
Here, a color enumeration type <span>color_t</span> is defined, where <span>red</span> has a value of 0, <span>white</span> has a value of 1, and so on, <span>color_num</span> has a value of 4, indicating the number of enumeration elements.
Constant Folding
Constant folding is a technique used by the compiler to optimize constant expressions during the compilation phase. For example:
const int a = 3;const int b = a + 5;
The compiler will calculate the value of <span>b</span> as 8 during the compilation phase, rather than calculating it at runtime.
Left-Right Rule
The left-right rule is a method used to parse complex pointer declarations. For example:
int *(*(*f)(int))[10];
The parsing process is as follows:
-
Start from the innermost parentheses,
<span>f</span>is a pointer. -
Looking to the right,
<span>f</span>is a function pointer, and the parameter type of that function is<span>int</span>. -
Looking to the left,
<span>f</span>points to a function that returns a pointer. -
Exiting the parentheses, continue parsing; the pointer points to an array, and the element type of the array is
<span>int*</span>, with an array size of 10.
Difference Between Arrays and Pointers
The array name represents the array type when declaring the array, while in other cases, it represents the address of the first element of the array. A pointer is a variable used to store a memory address. For example:
int arr[10];int *ptr = arr;
Here, <span>arr</span> is an array, and <span>ptr</span> is a pointer that points to the first element of the array <span>arr</span>.
Pointer Arrays and Array Pointers
-
Pointer Array: is an array where all elements are pointers. For example:
int *ptr_arr[10];
Here, a pointer array of size 10 is defined, where each element is a pointer to <span>int</span>.
-
Array Pointer: is a pointer that points to an array. For example:
int (*arr_ptr)[10];
Here, a pointer <span>arr_ptr</span> is defined, which points to an array of size 10 of <span>int</span>.
Pointers and Structures
When a structure variable is passed as a function parameter or return value, all member data of the structure is passed. In contrast, an array parameter is an address. For example:
void func(struct student s) { // Operate on structure variable s}void func_ptr(struct student *s) { // Operate on structure pointer s}
Function Pointers and Pointer Functions
-
Function Pointer: is a pointer that points to a function. For example:
int (*func_ptr)(int, int);func_ptr = ∑
Here, <span>func_ptr</span> is a pointer to a function that returns an <span>int</span> and takes two <span>int</span> parameters.
-
Pointer Function: is a function that returns a pointer. For example:
int *func(int a, int b) { // Return an int pointer}
Void Pointer
<span>void*</span> is a generic pointer type that can point to any data type. When using it, type conversion is required. For example:
void *ptr;int a = 10;ptr = &a;int *int_ptr = (int*)ptr;printf("%d", *int_ptr);
Here, <span>ptr</span> is a <span>void*</span> pointer pointing to the integer variable <span>a</span>, and through type conversion, it is converted to an <span>int*</span> pointer to access its value.