news 2026/5/9 1:11:03

【C语言】extern 关键字详解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【C语言】extern 关键字详解

该变量或函数的定义在其他地方。这是实现模块化和代码组织的重要手段。以下表格总结了extern关键字的主要用途。

用途

描述

示例

变量声明

告诉编译器变量在其他文件中定义,不分配内存。

extern int global_var;

变量定义

实际分配内存并初始化变量。

int global_var = 10;

函数声明

告诉编译器函数在其他文件中定义,提供函数的签名。

extern void my_function(int);

函数定义

实现函数的具体功能。

void my_function(int x) { ... }

1.1 变量声明与定义
  • 声明extern声明一个变量或函数,告诉编译器该变量或函数在其他文件中定义。例如: extern int global_var; // 声明 global_var 变量 这表明global_var变量在其他地方定义,但在当前文件中可以使用它。
  • 定义:定义一个变量或函数分配实际的内存空间。例如: int global_var = 10; // 定义 global_var 变量并初始化 这实际创建了global_var变量,并分配内存。
1.2 函数声明与定义
  • 声明:函数的声明也可以使用extern关键字,尽管在C语言中函数默认是extern的。例如: extern void my_function(int); // 声明 my_function 函数 这表明my_function函数在其他文件中定义,并接受一个整数参数。
  • 定义:函数的定义则包含实际的函数实现。例如: void my_function(int x) { // 函数实现 } 这实际实现了my_function函数的功能。

2.extern的实际应用

2.1 跨文件共享全局变量

假设我们有两个文件:file1.cfile2.c,并希望在这两个文件之间共享一个全局变量。

  • file1.c:

代码语言:c

AI代码解释

// 定义全局变量 int global_var = 10; void print_global_var() { printf("Global variable: %d\n", global_var); }
  • file2.c:

代码语言:c

AI代码解释

#include <stdio.h> // 声明全局变量 extern int global_var; void update_global_var(int new_value) { global_var = new_value; }
  • main.c:

代码语言:c

AI代码解释

#include <stdio.h> // 函数声明 void print_global_var(); void update_global_var(int); int main() { print_global_var(); // 输出: Global variable: 10 update_global_var(20); print_global_var(); // 输出: Global variable: 20 return 0; }

编译和链接

要编译这些文件并进行链接,可以使用以下命令(假设使用gcc编译器):

代码语言:c

AI代码解释

gcc file1.c file2.c main.c -o my_program

执行my_program后,输出结果如下:

代码语言:c

AI代码解释

Global variable: 10 Global variable: 20
2.2 跨文件共享函数声明

类似于全局变量,函数也可以通过extern关键字在文件之间共享。

  • file1.c:

代码语言:c

AI代码解释

#include <stdio.h> void print_message() { printf("Hello from file1.c\n"); }
  • file2.c:

代码语言:c

AI代码解释

#include <stdio.h> extern void print_message(); // 声明函数 void call_print_message() { print_message(); // 调用函数 }
  • main.c:

代码语言:c

AI代码解释

#include <stdio.h> extern void call_print_message(); // 声明函数 int main() { call_print_message(); // 输出: Hello from file1.c return 0; }

编译和链接

代码语言:c

AI代码解释

gcc file1.c file2.c main.c -o my_program

执行my_program后,输出结果如下:

代码语言:c

AI代码解释

Hello from file1.c

3. 注意事项

  • 初始化与定义extern关键字不用于初始化变量。初始化变量时应使用非extern声明。
  • 同名变量:在多个文件中使用extern声明同一个变量时,必须确保变量在一个地方定义,避免链接时的重定义错误。
  • 函数声明:函数的声明可以省略extern,因为函数声明默认是extern的。

https://www.dongchedi.com/article/7593052357961859609
https://www.dongchedi.com/article/7593053003943707161
https://www.dongchedi.com/article/7593053159065485886
https://www.dongchedi.com/article/7593032488635351614
https://www.dongchedi.com/article/7593036857261244953
https://www.dongchedi.com/article/7593037716343046681
https://www.dongchedi.com/article/7593040090407010841
https://www.dongchedi.com/article/7593053767038337560
https://www.dongchedi.com/article/7593038857445540376
https://www.dongchedi.com/article/7593038567396819518
https://www.dongchedi.com/article/7593037133037158937
https://www.dongchedi.com/article/7593040577730593304
https://www.dongchedi.com/article/7593038167410934334
https://www.dongchedi.com/article/7593038988328419902
https://www.dongchedi.com/article/7593038355794215449
https://www.dongchedi.com/article/7593035106705932825
https://www.dongchedi.com/article/7593030400185188888
https://www.dongchedi.com/article/7593036220402647614
https://www.dongchedi.com/article/7593038911606440472
https://www.dongchedi.com/article/7593055345048912409
https://www.dongchedi.com/article/7593036146780013080
https://www.dongchedi.com/article/7593055998798578238
https://www.dongchedi.com/article/7593055040021987865
https://www.dongchedi.com/article/7593055050289873432
https://www.dongchedi.com/article/7593055481636454936
https://www.dongchedi.com/article/7593053214056972825
https://www.dongchedi.com/article/7593055039552733720
https://www.dongchedi.com/article/7593053818359661118
https://www.dongchedi.com/article/7593054441390408254
https://www.dongchedi.com/article/7593053004065358361
https://www.dongchedi.com/article/7593055276706791998
https://www.dongchedi.com/article/7593059688728937022
https://www.dongchedi.com/article/7593058103126491710
https://www.dongchedi.com/article/7593056305229840958
https://www.dongchedi.com/article/7593057187287908888
https://www.dongchedi.com/article/7593059688728773182
https://www.dongchedi.com/article/7593057984951910974
https://www.dongchedi.com/article/7593057124842963480
https://www.dongchedi.com/article/7593059843347530264
https://www.dongchedi.com/article/7593054552925487641
https://www.dongchedi.com/article/7593057935971090969
https://www.dongchedi.com/article/7593058505985589785
https://www.dongchedi.com/article/7593057648438919742
https://www.dongchedi.com/article/7593059109587780120
https://www.dongchedi.com/article/7593058103126983230
https://www.dongchedi.com/article/7593059774406001177
https://www.dongchedi.com/article/7593056473081840153
https://www.dongchedi.com/article/7593056301866107417
https://www.dongchedi.com/article/7593057388245418558
https://www.dongchedi.com/article/7593059843347694104
https://www.dongchedi.com/article/7593057935971025433
https://www.dongchedi.com/article/7593058570980033086

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/25 5:49:58

MixTeX使用全攻略:5分钟学会LaTeX公式智能识别

MixTeX使用全攻略&#xff1a;5分钟学会LaTeX公式智能识别 【免费下载链接】MixTeX-Latex-OCR MixTeX multimodal LaTeX, ZhEn, and, Table OCR. It performs efficient CPU-based inference in a local offline on Windows. 项目地址: https://gitcode.com/gh_mirrors/mi/Mi…

作者头像 李华
网站建设 2026/4/16 14:50:29

如何用M2FP实现智能视频特效添加?

如何用M2FP实现智能视频特效添加&#xff1f; &#x1f9e9; M2FP 多人人体解析服务&#xff1a;为智能特效提供精准语义基础 在当前的智能视觉应用中&#xff0c;视频特效自动添加已成为直播、短视频、虚拟试衣等场景的核心功能之一。然而&#xff0c;传统基于边缘检测或简单…

作者头像 李华
网站建设 2026/5/6 18:25:07

青龙面板自动化脚本完全指南:轻松掌握QLScriptPublic高效使用技巧

青龙面板自动化脚本完全指南&#xff1a;轻松掌握QLScriptPublic高效使用技巧 【免费下载链接】QLScriptPublic 青龙面板脚本公共仓库 项目地址: https://gitcode.com/GitHub_Trending/ql/QLScriptPublic QLScriptPublic是专为青龙面板设计的自动化脚本集合&#xff0c;…

作者头像 李华
网站建设 2026/4/29 13:47:36

Next AI Draw.io:7步掌握智能图表自动化,告别繁琐手动绘制时代

Next AI Draw.io&#xff1a;7步掌握智能图表自动化&#xff0c;告别繁琐手动绘制时代 【免费下载链接】next-ai-draw-io 项目地址: https://gitcode.com/GitHub_Trending/ne/next-ai-draw-io 还在为复杂的图表制作耗费大量时间而苦恼吗&#xff1f;Next AI Draw.io 作…

作者头像 李华
网站建设 2026/5/6 9:03:16

智能开发助手Sweep:重新定义代码维护的新范式

智能开发助手Sweep&#xff1a;重新定义代码维护的新范式 【免费下载链接】sweep Sweep: AI-powered Junior Developer for small features and bug fixes. 项目地址: https://gitcode.com/gh_mirrors/sw/sweep 在日常开发工作中&#xff0c;你是否经常遇到这样的困扰&a…

作者头像 李华
网站建设 2026/5/2 16:39:32

Kronos金融大模型:颠覆性技术重塑股票市场预测新范式

Kronos金融大模型&#xff1a;颠覆性技术重塑股票市场预测新范式 【免费下载链接】Kronos Kronos: A Foundation Model for the Language of Financial Markets 项目地址: https://gitcode.com/GitHub_Trending/kronos14/Kronos 在当今高速发展的金融科技领域&#xff0…

作者头像 李华