Declarations vs Definitions 声明和定义
1. The Core Distinction: Memory Allocation
The fundamental difference between a declaration and a definition lies in whether system memory (RAM) is allocated.
-
Declaration: Tells the compiler the name and type of an identifier. It introduces the identifier but does not allocate memory.
-
Definition: Tells the compiler to create the entity. It allocates memory (storage) for variables or generates machine code for functions.
The Golden Rule:
No Memory Reserved = Declaration
Memory Reserved = Definition
2. The Special Case: Structures
In the context of memory allocation, defining a structure type is considered a Declaration.
A. Structure Type Definition (The Blueprint)
struct Student {
char name[50];
int age;
};
-
Classification: Declaration.
-
Explanation: This code describes the layout and size of the data type. It informs the compiler what a Student looks like. However, no memory is reserved on the stack or heap because no actual student object exists yet.
B. Structure Variable Definition (The Object)
struct Student student1;
-
Classification: Definition.
-
Explanation: This code instructs the system to allocate actual bytes of memory (e.g., 54 bytes) to store the variable student1.
3. Detailed Comparison by Category
Variables
| Syntax | Type | Memory | Description |
|---|---|---|---|
| extern int x; | Declaration | No | "Current file: x exists somewhere else." |
| int x; | Definition | Yes | Allocates storage for x. |
| int x = 10; | Definition | Yes | Allocates storage and initializes it. |
Functions
| Syntax | Type | Memory | Description |
|---|---|---|---|
| void func(void); | Declaration | No | Prototype. Tells compiler the signature. |
| void func(void) { ... } | Definition | Yes | Implementation. Generates machine code instructions. |
Typedefs
| Syntax | Type | Memory | Description |
|---|---|---|---|
| typedef int* IntPtr; | Declaration | No | Creates an alias/nickname. No storage created. |
4. The One Definition Rule (ODR)
-
Declarations: Can appear multiple times in a program (e.g., in multiple .c files via headers) as long as they are consistent.
-
Definitions: Must appear exactly once in the entire program scope.
- Linker Error: If you define int count; in a header file and include it in two source files, the linker will fail (multiple definition of 'count') because it tries to allocate memory at the same address twice.
5. Summary Analogy
-
Declaration (e.g., struct type): An Architect's Blueprint. It shows the design of a house, but you cannot live in a piece of paper.
-
Definition (e.g., struct variable): The Physical House. Construction materials (memory) are used to build it.