understand “extern” in C

-apply to C variables and functions
-extends the visibility of the C variables and C functions

difference between declaration and definition
declaration  - exist somewhere in the program but the memory is not allocated 
                      (we could know the types from declarations)
definition - declaration + allocate memory for that variable/function
a variable/function can be declared any number of times but it can be defined one once
why? (cannot have two locations of the same variable/function)

extern

function

int foo(int arg1, char arg2); //extern exists by default
extern int foo(int arg1, char arg2);

The above two lines will be treated the same by C compiler since C compiler will add extern when compiling the first line.
Declaration can exist in several files and any files with the function declaration can use the function.
How C compiler works? 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.
*extern implicit for C function


variable

extern int var;     //declaration only
int var;                //definition   
*extern explicitly for C variable

Conclusion

1.declaration can be done any number of times but definition only once
2.”extern” 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 redundant.
4.When extern is used for a variable, it’s declared only
5.extern with initialization is allowed but with warning

One sentence: tell the program to find defintions in other files

//variable examples

#include<iostream>
using namespace std;
int var;
int main(){
	var =10;
	return 0;
}


// compile successfully <- defined and use (defined and not use is trivial)
// var is globally defined


extern int var;
int main(void)
{
  return 0;
}


//compile successfully <- declared only but not use
//var is globally declared


extern int var;
int main(void)
{
 var = 10;
 return 0;
}


//throw error <- declared only and use
//var is globally declared but not defined(no memory allocated for the variable)


#include "somefile.h"
extern int var;
int main(void)
{
 var = 10;
 return 0;
}


//compile successfully <- defined and use
//var is globally defined if somefile.h has the definition of var


extern int var = 0;
int main(void)
{
 var = 10;
 return 0;
}
// error <- declared with initialization
//from compiler: warning: 'extern' variable has an initializer [-Wextern-initializer]

Reference: https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
extern在C语言中是一个关键字,用于声明一个全局变量或函数,以表示它是在其他地方定义或实现的。 当我们在一个文件中使用extern来声明一个全局变量时,它告诉编译器该变量是在其他文件中定义的,并且可以在当前文件中使用该变量。 同样,当我们在一个文件中使用extern来声明一个函数时,它告诉编译器该函数是在其他文件中实现的,并且可以在当前文件中调用该函数。 使用extern关键字可以帮助我们在不同的文件中共享变量和函数,从而增强代码的可重用性和可维护性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [c语言extern(c语言中文网)](https://blog.csdn.net/yetaodiao/article/details/127366663)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [C语言中extern的用法](https://blog.csdn.net/zhaoshuzhaoshu/article/details/38339935)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值