C: extern 关键字总结

                                         C extern keyword

一、declaration and definition 
     The declaration of a variable / function simply declares that the variable / function exists somewhere in the program but the memory is not allocated for them, and it only is the type of this variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In the case of function declaration, the program knows what are the arguments to that functions, their data type, the order of arguments and the return type of the function. 
The definition is that when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. 
From above explanation, it should be obvious that a variable/function can be declaredany number of times but it can be defined only once(Remember the basic principle that you can't have two locations of the same variable/function).  

二、extern keyword
1. use extern before a C function
  By default, the declaration and definition of  a c function have extern prepended with them. It means even though we don't use extern with the declaration/definition of C function, it is present there. For example, when we write:

                                                                    int foo(int arg1, char arg2);

There is an extern present in the beginning which is hidden and the compiler treats it as below.

                                                                    extern int foo(int arg1,char arg2);

Same is the case with the definition of a C function (Definition of a C function means writing the body of the function). Therefore whenever we define a C function, an extern is present there in the beginning o f the function definition. Since the declaration can be done any number of times and definition can be done only once, we can notice that declaration of a function can be added in several C/H files or in a single C/H file several times. But we notice the actual definition of the function only once (i.e in one file only). And as the extern extends the visibility to the whole program, the function can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known. (By knowing the declaration of the function, C compiler knows that the definition of the function exists and it goes ahead to compile the program). So that is all about extern with c functions.
2.use extern before a variable 
Let us explain it through under these examples. For example:

                                                                                 extern int var;

 Here, an integer type variable called var has been declared (remember no definition i.e no memory allocation for var so far). And we can do this declaration as many times as needed (remember that declaration can be done any number of times).
Now we define a variable. For example: 

                                                                                 int var;

 Here, an integer type variable called var has been declared and defined, and the memory for var is also allocated. Now here comes the surprise, when we declared/defined a C function, we saw that an extern was present by default.  While defining a function, we can prepend it with extern without any issues. But it is not the case with  C variables. If we put the presence of extern in variable as default  then the memory for them will not be allocated ever, they will be declared only. Therefore we put extern explicitly for C variables when we want to declare them without defining them. Also, as the extern extends the visibility to the whole program, by externing a variable we can use variable anywhere in the program provided we know the declaration of them and the variable is defined somewhere.
Now let us understand extern with examples. 

Example1:
                                                                                  int var;
                                                                                  int main()
                                                                                  {
                                                                                       var =10;
                                                                                       return 0;  
                                                                                   } 

Analysis: This program is compiled successfully. Here var is defined (and declared implicitly) globally.

Example2:
                                                                                   extern int var;
                                                                                   int main()
                                                                                   {
                                                                                       return 0;
                                                                                     }

Analysis: This program is compiled successfully. Here var is declared only. Notice var is never used so no problems.
Example 3:
                                                                                     extern int var;
                                                                                     int main()
                                                                                      {
                                                                                             var =10;
                                                                                              return 0;
                                                                                       }

Analysis: This program throws error in compilation. Because var is declared but not defined anywhere. Essentially, the var is not allocated any memory. And the program is trying to change the value to 10 of a variable that does not exist at all.
Example 4:
                                                                                   #include "somefile.h"
                                                                                   extern int var;
                                                                                   int main()
                                                                                  {
                                                                                          var=10;
                                                                                          return 0;
                                                                                    }

Analysis: Supposing that somefile.h has the definition of var. This program will be compiled successfully.
Example 5:
                                                                                  extern int var=0;
                                                                                  int main()
                                                                                 {
                                                                                       var =10;
                                                                                       return 0;
                                                                                }

Analysis: If a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated. i.e. that that variable will be considered as defined. Therefore, as per(as per:按照、依据、如同) the C standard, this program will compile successfully and work.

In short, we  summarize as follows.
1. Declaraton can be done any number of times but definition only once.
2."extern" keyword is used to extend the visibility of variables/functions
3.Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.
4.When extern is used with a variable, it's only declared not defined.
5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
extern关键字在C语言中有以下几个作用: 1. 声明外部变量:当在一个文件中定义了一个全局变量,而在另一个文件中需要使用该变量时,可以使用extern关键字来声明该变量。这样编译器在编译时不会报错,而在链接时会去查找定义过的变量。 2. 声明外部函数:类似于声明外部变量,当在一个文件中定义了一个函数,而在另一个文件中需要调用该函数时,可以使用extern关键字来声明该函数。这样编译器在编译时不会报错,而在链接时会去查找定义过的函数。 3. 单方面修改函数原型:当需要修改函数的原型时,可以使用extern关键字来声明函数。这样可以在调用该函数的地方进行修改,而不需要修改函数的定义。 总结来说,extern关键字的作用是在不同的文件中声明已经定义过的变量或函数,以便在编译和链接时能够正确地引用它们。 #### 引用[.reference_title] - *1* [【004 关键字extern “C“的作用是什么?](https://blog.csdn.net/qq_41709234/article/details/123082378)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [extern关键字作用](https://blog.csdn.net/qq_40569221/article/details/119817008)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [[C语言]C语言中关键字extern’的作用](https://blog.csdn.net/m0_58244165/article/details/130497382)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值