主要功能是可以讓不同檔案間可以共用同一個變數
特色主要有以下兩點:
(1)變數宣告可以多次,宣告其存在
(2)變數定義只可以一次,讓程式為其配置空間
用法可以說 : 從程式撰寫員指定的文件借用該借用文件中的變數(包含結構)
並且用在正在撰寫的文件上。
-------------------------------------------------------------------------------
以下摘錄自MSDN的文件
http://msdn.microsoft.com/en-us/library/aa299338(v=vs.60).aspx
extern declarator // used when variable or function has external linkageextern string-literal declarator // used when linkage conventions of another
// language are being used for the declarator
extern string-literal { declarator-list } // used when linkage conventions of another
// language are being used for the declarators
The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. In C++, when used with a string, extern specifies that the linkage conventions of another language are being used for the declarator(s).Example Code:
// Example of the extern keyword extern "C" int printf( const char *, ... ); extern "C" { int getchar( void ); int putchar( int ); }
-------------------------------------------------------------------------------
來自Stack Overflow的問題
Q:Could someone exactly explain the concept ofextern
variables in C? The declaration, exact use ofextern
and its scope.
Ans: http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c在Stack Overflow 的連結中有非常"完整"的解答
-------------------------------------------------------------------------------
繼續努力學習C/C++
沒有留言:
張貼留言