C语言中,`extern` 和 `#include`

C语言中,extern#include

1. #include

#include 是C语言中的预处理指令,用于将指定文件的内容插入到当前文件中。通常用于包含头文件(.h 文件),头文件中通常包含函数声明、宏定义、类型定义等。

使用场景:

  • 将函数声明、宏定义、结构体定义等共享内容放在头文件中。
  • 在多个源文件中共享相同的声明或定义。

示例:

头文件 functions.h
c 复制代码
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

// 函数声明
void demo();

// 全局变量声明
extern int globalVar;

#endif
源文件 functions.c
c 复制代码
#include <stdio.h>
#include "functions.h"

// 全局变量定义
int globalVar = 10;

// 函数定义
void demo() {
    printf("This is the demo function.\n");
}
主文件 main.c
c 复制代码
#include "functions.h"

int main() {
    demo();  // 调用函数
    globalVar = 20;  // 修改全局变量
    printf("Global variable value: %d\n", globalVar);
    return 0;
}

特点:

  • #include 是将头文件的内容直接插入到当前文件中。
  • 适合共享函数声明、宏定义、结构体定义等。
  • 头文件通常使用 #ifndef#define#endif 防止重复包含。

2. extern

extern 是C语言中的关键字,用于声明一个变量或函数是在其他文件中定义的。它告诉编译器:"这个变量或函数的定义在其他地方,不要在这里分配存储空间。"

使用场景:

  • 在多个源文件中共享全局变量。
  • 在头文件中声明全局变量或函数,避免重复定义。

示例:

头文件 globals.h
c 复制代码
#ifndef GLOBALS_H
#define GLOBALS_H

// 全局变量声明
extern int globalVar;

// 函数声明
void demo();

#endif
源文件 globals.c
c 复制代码
#include <stdio.h>
#include "globals.h"

// 全局变量定义
int globalVar = 10;

// 函数定义
void demo() {
    printf("This is the demo function.\n");
}
主文件 main.c
c 复制代码
#include "globals.h"

int main() {
    demo();  // 调用函数
    globalVar = 20;  // 修改全局变量
    printf("Global variable value: %d\n", globalVar);
    return 0;
}

特点:

  • extern 只是声明变量或函数的存在,不会分配存储空间。
  • 适合在多个源文件中共享全局变量。
  • 通常与头文件一起使用,避免重复定义。

3. #includeextern 的区别

特性 #include extern
作用 将指定文件的内容插入到当前文件中。 声明变量或函数是在其他文件中定义的。
使用场景 共享函数声明、宏定义、结构体定义等。 共享全局变量或函数声明。
存储空间分配 不涉及存储空间分配。 不涉及存储空间分配。
文件类型 通常用于头文件(.h 文件)。 可以用于头文件或源文件。
重复包含问题 需要使用 #ifndef 防止重复包含。 无重复包含问题。

4. 如何选择使用 #includeextern

使用 #include 的情况:

  • 需要共享函数声明、宏定义、结构体定义等。
  • 需要将多个源文件共享的内容集中到一个头文件中。

使用 extern 的情况:

  • 需要在多个源文件中共享全局变量。
  • 需要在头文件中声明全局变量或函数,避免重复定义。

5. 综合示例

文件结构:

复制代码
project/
├── main.c
├── globals.h
└── globals.c
头文件 globals.h
c 复制代码
#ifndef GLOBALS_H
#define GLOBALS_H

// 全局变量声明
extern int globalVar;

// 函数声明
void demo();

#endif
源文件 globals.c
c 复制代码
#include <stdio.h>
#include "globals.h"

// 全局变量定义
int globalVar = 10;

// 函数定义
void demo() {
    printf("This is the demo function.\n");
}
主文件 main.c
c 复制代码
#include "globals.h"

int main() {
    demo();  // 调用函数
    globalVar = 20;  // 修改全局变量
    printf("Global variable value: %d\n", globalVar);
    return 0;
}

输出:

复制代码
This is the demo function.
Global variable value: 20

总结

  • #include 用于将文件内容插入到当前文件中,适合共享函数声明、宏定义等。
  • extern 用于声明变量或函数是在其他文件中定义的,适合共享全局变量。
  • 在实际项目中,通常会将 #includeextern 结合使用,以实现模块化编程和代码共享。
相关推荐
汉克老师1 小时前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(5、机甲战士)
c++·算法·蓝桥杯·01背包·蓝桥杯c++·c++蓝桥杯
Mr_Xuhhh2 小时前
项目需求分析(2)
c++·算法·leetcode·log4j
c++bug2 小时前
六级第一关——下楼梯
算法
PAK向日葵2 小时前
【C/C++】面试官:手写一个memmove,要求性能尽可能高
c语言·c++·面试
Morri32 小时前
[Java恶补day53] 45. 跳跃游戏Ⅱ
java·算法·leetcode
林木辛3 小时前
LeetCode热题 15.三数之和(双指针)
算法·leetcode·双指针
AndrewHZ3 小时前
【3D算法技术】blender中,在曲面上如何进行贴图?
算法·3d·blender·贴图·三维建模·三维重建·pcg
Jared_devin3 小时前
二叉树算法题—— [蓝桥杯 2019 省 AB] 完全二叉树的权值
数据结构·c++·算法·职场和发展·蓝桥杯
搞全栈小苏3 小时前
基于Qt QML和C++的MQTT测试客户端(CMakeLists实现)
xml·c++·qt
啊?啊?3 小时前
18 从对象内存到函数调用:C++ 虚函数表原理(继承覆盖 / 动态绑定)+ 多态实战
开发语言·c++·多态原理