c语言中extern关键字_了解C语言中的extern关键字

c语言中extern关键字

In this article, we’ll take a look at understanding the extern keyword in C.

在本文中,我们将了解C中的extern关键字。

This is primarily used when you have multiple source files, and you want to share variables among those files.

这主要在有多个源文件并且要在这些文件之间共享变量时使用。

Let’s take a look at how we can use extern properly, to compile all our files without conflicts.

让我们看一下如何正确使用extern来编译所有文件而不会发生冲突。



extern关键字有什么作用? (What does the extern keyword do?)

Before we look at extern, let’s look at two types of statements first:

在讨论extern之前,让我们先看两种类型的语句:

  • A variable declaration statement

    变量声明语句
  • A variable definition statement

    变量定义语句

A declaration statement is something like this:

声明语句是这样的:


int a;

char* ch;

You inform the compiler that these variables exist somewhere already, so it does not allocate memory separately for them. This is a crucial point.

您通知编译器这些变量已经存在,因此它不会为它们单独分配内存。 这是关键点。

There is another statement for defining a variable, where you explicitly allocate storage for that particular variable.

还有另一个用于定义变量的语句,您可以在其中为该特定变量显式分配存储。

To define a variable, we typically do something similar to the below snippet.

要定义变量,我们通常会执行与以下代码段相似的操作。


// Defining a
int a = 10;

// Defining ch, even if it is NULL
char* ch = NULL;

Now, let’s move on to extern. We can use the extern keyword only when we are declaring a global variable. Let’s make this clear.

现在,让我们继续进行extern 。 只有在声明全局变量时,才能使用extern关键字 让我们澄清一下。

This means that you cannot prefix the extern keyword when defining a global variable.

这意味着在定义全局变量时,不能在extern关键字前添加前缀。


// Allowed. Variable declaration
extern int a;

// Not allowed. extern with variable definition
extern int b = 5;

To understand why, let’s look at what extern actually means.

要了解原因,让我们看看extern实际含义。

When you’re prefixing a global variable declaration with an extern keyword, this means that you’re telling the compiler that this variable is actually defined in some other file!

当给全局变量声明加上extern关键字前缀时,这意味着您要告诉编译器该变量实际上是在其他文件中定义的

This means that the compiler does not need to allocate the same shared variable twice, and this will not cause conflict when working with multiple source files.

这意味着编译器不需要两次分配相同的共享变量,并且在使用多个源文件时不会引起冲突。

在C中使用extern关键字–完整示例 (Using the extern keyword in C – A complete example)

Let’s look at an example to make this clear.

让我们看一个例子来阐明这一点。

Assume that we have two source files and two header files:

假设我们有两个源文件和两个头文件:

  • file1.c, file1.h

    file1.cfile1.h
  • file2.c file2.h.

    file2.c file2.h

Assume a separate main program called main.c, which is the driver program.

假设有一个单独的主程序main.c ,它是驱动程序。

So let’s consider file1.c, which has a global variable called int file1_var defined. So, we intend to share this to file2.c using extern.

因此,让我们考虑file1.c ,它具有一个称为int file1_var的全局变量defined 。 因此,我们打算使用extern将其共享到file2.c

I’ll provide sample code for file1.h, which has a simple function prototype for the functions of file1.c, so that other programs can find out which function to invoke, when including the header file file1.h.

我将为file1.h提供示例代码,该文件具有file1.c函数的简单函数原型,以便其他程序在包含头文件file1.h时可以找出要调用的函数。


// file1.h
// Contains function prototypes for functions of file1.c

int addition_1(int a, int b);

// file1.c
// Contains function definition for addition_1()
#include "file1.h"
#include <stdio.h>

// Also has a global variable called file1_var
// We are defining it here so that it can be shared by other files
// when they use extern on this variable
int file1_var = 100;

int addition_1(int a, int b) {
    file1_var = file1_var + 100;
    printf("Inside file1.c addition(). file1_var = %d\n", file1_var);
    return a + b;
}

I have set the initial value of file1_var as 100.

我已将file1_var的初始值设置为100。

Similarly, I will do the same for file2.h and file2.h. But now, since we want a reference to file1_var, we need to use extern, and only declare it.

同样,我将对file2.hfile2.h进行相同的file2.h 。 但是现在,由于我们要引用file1_var ,因此需要使用extern ,仅对其进行声明。


// file2.h

int addition_2(int a, int b);

// file2.c
// Contains function definition for addition_2()
#include "file2.h"
#include <stdio.h>

// Makes an extern reference to file1_var
extern int file1_var;

int addition_2(int a, int b) {
    file1_var = file1_var + 200;
    printf("Inside file2.c addition(). file1_var = %d\n", file1_var);
    return a + b;
}

Now, the two files share a variable file1_var, which also gets updated when any of the addition() functions gets called.

现在,这两个文件共享一个变量file1_var ,当调用任何file1_var addition()函数时,该文件也会更新。

Let’s write the code for the driver program main.c, which calls both functions. So, we must include both header files file1.h and file2.h.

让我们为驱动程序main.c编写代码,该程序调用这两个函数。 因此,我们必须同时包含头文件file1.hfile2.h


#include "file1.h"
#include "file2.h"
#include <stdio.h>

// We can also use the file1_var reference here!
extern int file1_var;

// We must have only one main() reference
// Since this is our driver program, the main function must be only here
int main() {
    int res1 = addition_1(10, 20);
    int res2 = addition_2(30, 40);
    printf("file1_var = %d\n", file1_var);
    return 0;
}

I’ll be using gcc to compile all our files:

我将使用gcc编译所有文件:


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

This command compiles and links the files main.c, file1.c and file2.c, and creates an executable called main.out.

该命令编译并链接文件main.cfile1.cfile2.c ,并创建一个名为main.out的可执行文件。

To execute, type ./main.out.

要执行,请输入./main.out


./main.out

Output

输出量


Inside file1.c addition(). file1_var = 200
Inside file2.c addition(). file1_var = 400
file1_var = 400

The output indeed seems to match what we expect. file1_var will get incremented by 100, when addition_1() gets called.

输出确实确实符合我们的预期。 当调用addition_1()时, file1_var将增加100。

Now, again, it will increment by 200, when addition_2() gets called. So finally, the updated value of file1_var is 400!

现在,再次调用addition_2()时,它将增加200。 因此,最后, file1_var的更新值为400!

We have successfully used extern to share variables between multiple source files!

我们已经成功地使用extern在多个源文件之间共享变量!

NOTE: To understand more about compiling and using multiple source and header files, refer this link. This mentions the best practices to avoid re-declaration errors when linking multiple files, so please read this too.

注意 :要了解有关编译和使用多个源文件和头文件的更多信息,请参考链接。 这里提到了避免链接多个文件时重新声明错误的最佳实践,因此也请阅读此内容。



结论 (Conclusion)

In this article, we learned how we could use the extern keyword in C++ to share variables across source files. This is essential if you’re working with multiple files and global variables.

在本文中,我们学习了如何在C ++中使用extern关键字在源文件之间共享变量。 如果要使用多个文件和全局变量,这是必不可少的。

参考资料 (References)



翻译自: https://www.journaldev.com/38985/extern-keyword-in-c

c语言中extern关键字

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值