What is the use of extern in C(C语言中extern的用途)?
2010-11-12 17:26
519 查看
The "extern" declaration in C is to indicate the existence of, and the type of, a global variable or function. A global variable, or a global function, is one that is available to all C modules (a single C module is typically a single .c file). An extern is something that is defined externally to the current module. In many cases, you can leave off the extern qualifier and not notice any difference because the linker can collapse multiple definitions to one. But the intent is then unclear in the code, and the code is error prone in case of typos. It is much clearer to define the global in one place, and then declare extern references to it in all the other places. When refering to globals provided by a library, especially a shared library, this is even more important in order to ensure you are talking about the correct, common instance of the variable.
Declaring a variable as extern will result in your program not reserving any memory for the variable in the scope that it was declared. For instance (as example) if a program's source code declared the variable var as a global volatile int in foo.c, to properly use it in bar.c you would declare it as extern volatile int var.
It is also not uncommon to find function prototypes declared as extern.
Zend/zend.h中有关的定义:
Declaring a variable as extern will result in your program not reserving any memory for the variable in the scope that it was declared. For instance (as example) if a program's source code declared the variable var as a global volatile int in foo.c, to properly use it in bar.c you would declare it as extern volatile int var.
It is also not uncommon to find function prototypes declared as extern.
Zend/zend.h中有关的定义:
#ifdef __cplusplus #define BEGIN_EXTERN_C() extern "C" { #define END_EXTERN_C() } #else #define BEGIN_EXTERN_C() #define END_EXTERN_C() #endif
相关文章推荐
- Part 68 - What is the use of NonAction attribute in mvc
- What is the use of having destructor as private?
- What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations in Android?
- 异常-----Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.\u003Cformat> or ?string(format) built-
- Question 10: Given the following program snippet, what can we conclude about the use of dynamic_cast in C++?
- To see what is in the MAT-file prior to loading it, use whos -file
- what is the difference of select single and select up to one row in abap
- What Is The Meaning of 0.0.0.0 In Routing Table?
- 软件随想录(local.joelonsoftware.com/wiki)-2001年05月05日 这个国家的狗做什么工作? - What Is the Work of Dogs in this Cou
- What is the general use of Awake(), Start(), Update(), FixedUpdate(), LateUpdate()?
- What is the use of the res/values/public.xml file on Android?
- Question 38: What is the output of the program below in C++?
- what is the use of truncate command
- What is the use of "?
- What is the maximum number of files allowed in a HDFS directory?
- What is the significance of asmlinkage modifier in C?
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- What is the role of the activation function in a neural network?
- ccah-500 第47题 What is the purpose of ZooKeeper in such a configuration
- what is the purpose of using translatable in Android strings?