69,382
社区成员
发帖
与我相关
我的任务
分享
extern
extern declarator // used when variable or function has external linkage
extern 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).
Declarations of variables and functions at file scope are external by default.
In C++, string-literal is the name of a language. The language specifier "C++" is the default. "C" is the only other language specifier currently supported by Microsoft C/C++. This allows you to use functions or variables defined in a C module.
All of the standard include files use the extern "C" syntax to allow the run-time library functions to be used in C++ programs.
For more information, see auto, register, static, const, and volatile.
Example
The following example declares the functions printf, getchar, and putchar with “C” linkage:
// Example of the extern keyword
extern "C" int printf( const char *, ... );
extern "C"
{
int getchar( void );
int putchar( int );
}
public
C++ Specific —>
public: [member-list]
public base-class
When preceding a list of class members, the public keyword specifies that those members are accessible from any function. This applies to all members declared up to the next access specifier or the end of the class.
When preceding the name of a base class, the public keyword specifies that the public and protected members of the base class are public and protected members, respectively, of the derived class.
Default access of members in a class is private. Default access of members in a structure or union is public.
Default access of a base class is private for classes and public for structures. Unions cannot have base classes.
For more information, see private, protected, friend, and Table of Member Access Privileges.
END C++ Specific
Example
// Example of the public keyword
class BaseClass
{
public:
int pubFunc();
};
class DerivedClass : public BaseClass
{
};
void main()
{
BaseClass aBase;
DerivedClass aDerived;
aBase.pubFunc(); // pubFunc() is accessible
// from any function
aDerived.pubFunc(); // pubFunc() is still public in
// derived class
}
extern is used when a file needs to access a variable in another file that it may not have #included
directly. Therefore, extern does not actually carve out space for a new variable, it just provides the
compiler with sufficient information to access the remote variable.
2)对你的问题解释一下.其实记住一点就可以了:c语言中变量使用之前要声明, extern可以声明本作用域之外的变量.
1 我新建了一个new.uvproj
2 其中包括main.c a.c a.h b.c b.h 还有define.h(其中包括数据的申明和I/O口的定义)
3 我在define.h中定义了一个全局变量extern unsigned int Temperature_Temp;
4 这个变量在a.c中用到,所以在a.c中如下定义unsigned int Temperature_Temp;
5 在main.c中也要用到这个全局变量,那我还要在这个.c文件中再定义一次?我试过,这样keil报错,重复定义~~~
[注]:main.c 看不到 a.c中的定义,你可以把define.h, #include进来就可以了.
我的问题是:那我在a.c中使用extern定义,即extern unsigned int Temperature_Temp;
那按照我的理解:因为已经定义为全局变量了,那在main.c中就不需要定义了,这样理解对不对???
[注]:不对
实验结果:这样keil虽不报错,可是出现很多警告!!!为什么???
[注]:extern是声明,这样的话a.c中声明了,但没有地方定义这个变量
后来,我在a.c中将其定义为extern unsigned int Temperature_Temp;
在main.c中将其定义为unsigned int Temperature_Temp;
就可以了(没有警告,也没有错误),可是,我百思不得其解,
[注]:这里的意思是这个变量有地方定义了(main.c),别的地方要用也声明了(a.c),所以符合了c语言的规范.