在定义变量“的extern”关键字使用 [英] Use of 'extern' keyword while defining the variable

查看:97
本文介绍了在定义变量“的extern”关键字使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在看到<一个href=\"http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/1433387#1433387\">this回答
我有这样的疑问。
在我的项目,
我见过一些声明extern变量和像定义如下:

After seeing this answer I have this doubt. In my project, I have seen some extern variables declared and defined like below:

file1.h

file1.h

extern int a;

file1.c中

file1.c

extern int a=10;

但在我提到的链接,它说,在C文件应该像定义:

But in the link I mentioned it says that in the c file it should be defined like:

int a = 10;

是否定义过程中添加的extern 键词太有任何目的/意义。
或者它的方式有关系吗?

Does adding extern key word during the definition too has any purpose/meaning. Or does it matter by the way?

推荐答案

这不会改变的意思。 的extern 只有当你的声明的一个变量是有道理的。的定义的有的extern 的变量是一样的,因为没有标记所有的全局变量静态是默认连接器上显示的符号。

It does not change the meaning. extern only makes sense when you declare a variable. Defining a variable with extern is the same because all global variables that are not marked static are symbols visible to the linker by default.

请注意,如果你不想初始化变量,也就是不具有部分 = 10 ,编译器会认为的extern int类型的总是声明的,而不是一个的定义的。在同样的意义上,有 int类型的全球始终是一个的定义的并不仅仅是一个的声明

Note that if you didn't want to initialise the variable, that is, not having the part = 10, the compiler will assume that extern int a is always a declaration and not a definition. In the same sense, having int a globally is always a definition and not just a declaration.

这篇关于在定义变量“的extern”关键字使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆