关于全局变量和extern的用法---请教!!!

liuchang910781261 2014-11-25 11:49:22
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报错,重复定义~~~
我的问题是:那我在a.c中使用extern定义,即extern unsigned int Temperature_Temp;
那按照我的理解:因为已经定义为全局变量了,那在main.c中就不需要定义了,这样理解对不对???
实验结果:这样keil虽不报错,可是出现很多警告!!!为什么???

后来,我在a.c中将其定义为extern unsigned int Temperature_Temp;
在main.c中将其定义为unsigned int Temperature_Temp;
就可以了(没有警告,也没有错误),可是,我百思不得其解,求大神指教~~~
...全文
338 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuchang910781261 2014-11-26
  • 打赏
  • 举报
回复
额,是啊,小弟正在学习,感谢大家的帮助~~~
赵4老师 2014-11-25
  • 打赏
  • 举报
回复
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
}
zacharyLiu 2014-11-25
  • 打赏
  • 举报
回复
引用 1 楼 u011337306 的回复:
1.c文件要包含变量声明所在的头文件; 2.C文件引用另外一个C文件变量,可以直接在该C文件中对该变量进行声明; 另外,建议LZ搞清变量的作用域
感觉LZ还分不清什么是定义什么是声明!这些东西自己去领悟吧!
zacharyLiu 2014-11-25
  • 打赏
  • 举报
回复
1.c文件要包含变量声明所在的头文件; 2.C文件引用另外一个C文件变量,可以直接在该C文件中对该变量进行声明; 另外,建议LZ搞清变量的作用域
super_admi 2014-11-25
  • 打赏
  • 举报
回复
亲,确实如楼上所说,你把定义和声明的概念弄混淆了。 同一个程序中,一个全局变量,可以声明多次,但只能定义一次。因此,全局变量一般会放在.c or .cpp中,而声明会放在.h之中。
whizer 2014-11-25
  • 打赏
  • 举报
回复
1)请参考 <<The C Programming Language >> 和 <<C99(ISO_IEC_9899_1999)>>,或者搜"c语言变量与作用域"

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语言的规范.

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧