1. Common Variables in C Language
8-bit Unsigned Long Integer Range: [0-255]
16-bit Unsigned Long Integer Range: [0-65535]
32-bit Unsigned Long Integer Range: [0-4294967295]
Refer to the following:

Note:
-
Variable definitions should be at the beginning of the function, and no other statements should precede the variable definition statements.
-
In microcontroller development, only unsigned data types are typically used, unless complex floating-point calculations are involved.
-
When using statements like for loops to continuously accumulate a variable’s value, consider the overflow issue related to the data type range.
-
In STM32 development, there is no bit-type definition.
2. Enumeration
1. Enumeration Definition
It is a user-defined data type used to define a set ofnamed integer constants.
1) Definition Reference:
// Define enumeration type
enum Weekday {
MONDAY, // Default value is 0
TUESDAY, // Default value is 1
WEDNESDAY, // Default value is 2
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
// Declare enumeration variable
enum Weekday today = WEDNESDAY;
2) Enumeration constants can explicitly specify any integer value; unspecified constants will increment from the previous value.
enum HttpStatus {
OK = 200,
CREATED = 201,
BAD_REQUEST = 400,
NOT_FOUND = 404,
SERVER_ERROR = 500
};
enum Color {
RED, // 0
GREEN = 5,
BLUE, // 6
YELLOW = 10
};
3) Use typedef to create an alias
typedef enum {
JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC
} Month;
Month currentMonth = SEP; // Declare variable using alias
2. Essence and Advantages of Enumeration
-
Essence: Enumeration constants are essentially integers of type int (can be negative)
Data is stored in flash, and enumeration variables can only be assigned values of identifiers within the enumeration.
-
Advantages:
-
Improved Readability: if (today == WEDNESDAY) is much clearer than if (today == 2).
-
Easy Maintenance: If the value of a constant needs to be modified, it can be changed in one place in the enumeration definition.
-
Compiler Checks: Some compilers can provide better type checking than #define.
-
Debugging Friendly: Debuggers can display the names of enumeration constants instead of a meaningless number.
3. Structures
1. Definition
Definition: Different types of variables are packaged into a whole, forming a new “custom type”.
Example:
struct Tag {
MemberList;
}; // ← Semicolon cannot be omitted
2. Principles to Follow
1. The address of the first member is 0.
2. The address of each member is a multiple of its own size.
3. The total size of the structure is a multiple of the largest type contained in its members.
Example:
struct test {
char a; //1
short b; //2
char c; //1
}; // The size of this structure is 6 bytes
4. Unions
1. Definition
The definition syntax of a union is similar to that of a structure, but uses the union keyword instead of struct. Below is the definition syntax for a union.
union Tag {
MemberList;
}; // Semicolon cannot be omitted
2. Principles to Follow
All members share the same memory, and only one value can be stored at a time; size = size of the largest member.
Comparison with Structures
|
Dimension |
struct |
union |
|---|---|---|
|
Memory |
Each member has an independent address |
All start from the same address |
|
Size |
Sum of all members + alignment |
Size of the largest member + alignment |
|
Usage |
Package different data |
Multiple “views” of the same data |
|
Simultaneous Access |
Possible |
Illegal (undefined behavior) |
Course reference links are as follows:
C Structures | Runoob Tutorial
“Basic Knowledge of C Language” on Variables – Bilibili