C语言declared关键字作用是什么?

99ANYc3cd6
预计阅读时长 19 分钟
位置: 首页 C语言 正文

Of course. In C, the keyword declared is not a keyword itself. Instead, when people say a variable or function is "declared," they are referring to the act of declaring it.

c语言declared
(图片来源网络,侵删)

Let's break down what "declaration" means in C, as it's a fundamental concept.


What is a Declaration?

In C, a declaration is a statement that introduces an identifier (like a variable or function name) to the compiler and specifies its essential properties. The most important properties are:

  • The name of the identifier (e.g., myAge).
  • The type of the identifier (e.g., int, char, float, or a custom struct).
  • For functions, the types of its arguments and its return value.

A declaration tells the compiler: "Hey, this name exists, and here's what it is. I'll tell you more about it later, or you can find its full definition somewhere else."

Declaration vs. Definition (The Crucial Distinction)

This is the most important concept to understand. While often used interchangeably in casual conversation, in C, declaration and definition are two different things.

c语言declared
(图片来源网络,侵删)

A. Declaration

  • Purpose: To announce the existence of a name and its type.
  • What it does: It allocates zero memory for the variable (except for tentative definitions, which we'll cover later).
  • Syntax:
    • For variables: type name; (without an initial value) or extern type name;.
    • For functions: return_type function_name(type param1, type param2); (note the semicolon at the end).

Examples of Declarations:

// Variable declaration (also a definition, see below)
int my_score;
// Function declaration (a pure declaration, not a definition)
int add(int a, int b);
// Another variable declaration using 'extern'
extern int global_counter; 
// This tells the compiler that 'global_counter' exists as an int
// and is defined in another translation unit (e.g., another .c file).

B. Definition

  • Purpose: To create and allocate memory for an identifier.
  • What it does: It is a declaration that also allocates memory and (optionally) initializes the variable. For a function, it provides the actual code block.
  • Syntax:
    • For variables: type name = initial_value; or just type name; (without extern). This statement alone is both a declaration and a definition.
    • For functions: return_type function_name(type param1, type param2) { /* code here */ } (no semicolon at the end).

Examples of Definitions:

// Variable definition (allocates memory for an integer)
int player_lives = 3;
// Function definition (provides the actual code)
int add(int a, int b) {
    return a + b;
}
// Another variable definition (allocates memory, initialized to 0)
int player_lives; 

The "One Definition Rule" (ODR)

In a C program, you can have many declarations of the same identifier, but you can only have one definition.

  • Multiple Declarations are OK: You can declare a function in a header file (myfunctions.h) and then include that header in multiple source files (main.c, other.c). Each source file will see the declaration. This is standard practice.
  • Only One Definition: The actual code block for the function () must appear in only one .c file. If you try to define it in two different files, the linker will complain about "multiple definitions."

The Special Case: extern

The extern keyword is key to understanding the declaration/definition distinction.

c语言declared
(图片来源网络,侵删)
  • extern int x; is a declaration. It tells the compiler, "An integer named x exists, and its memory is allocated somewhere else." It does not allocate memory for x in the current file.
  • int x; (without extern) is a definition. It tells the compiler, "An integer named x exists, and I am allocating memory for it right here."

Example:

main.c

#include <stdio.h>
// This is a declaration. It tells main.c that 'global_var' exists
// and is defined in another file.
extern int global_var; 
int main() {
    // We can use global_var because it has been declared.
    printf("The value of global_var is: %d\n", global_var);
    return 0;
}

data.c

// This is the definition of global_var. It allocates memory and initializes it.
int global_var = 100; 

When you compile and link these two files together (gcc main.c data.c -o program), the program will work correctly.


Summary Table

Feature Declaration Definition
Purpose Introduces a name and its type to the compiler. Creates and allocates memory for a variable or provides the code for a function.
Memory Allocation No (for pure extern declarations). Yes.
Syntax Ends with a semicolon (). For variables: type name; or type name = value;. For functions: (no semicolon).
Uniqueness Can have multiple declarations. Can have only one definition (One Definition Rule).
Examples int func();
extern int x;
int func() { ... }
int x = 5;
int x;

In short, when someone says a variable is "declared," they mean the compiler has been informed of its name and type. The "definition" is the more concrete statement that actually creates it in memory.

-- 展开阅读全文 --
头像
C语言putpixel函数如何使用?
« 上一篇 04-15
织梦详情页统一模板如何统一实现?
下一篇 » 04-15

相关文章

取消
微信二维码
支付宝二维码

目录[+]