"&的extern QUOT;一个函数里? [英] "extern" inside a function?

查看:277
本文介绍了"&的extern QUOT;一个函数里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,读有点老一书(以下简称C语言程序设计,第二版,由丹尼斯里奇),我来到了一个跨如下:

Well, reading "a bit old" book ("The C programming language", second edition, by Dennis Ritchie), I came a cross the following:

这是外部变量必须定义,只有一次,在任何函数之外;这留出存储它。变量也必须在想要访问它的每个函数声明

An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it

和我很喜欢 - ?!什么

and I was like - what?!

变量,还必须在想要访问它的每个函数声明。然后,我惊呆了一次:

"The variable must also be declared in each function that wants to access it". Then, I was shocked one more time:

int max; 
/* ... */
int main()
{
    extern int max;
    /* ... */
}

和另外一个问题 - ?!什么

And one more - what?!

据我所知(当然,它不是不够多,远),的extern 是有道理的,只有当你定义某处一个全局变量,你想访问通过另一个文件(而不是重新定义它)。

As far as I know (obviously, it's not much and far from enough), extern makes sense only when you define a global variable somewhere and you want to access it through another file (not to define it again).

所以:


  • 什么是这点的extern INT最大 的内部或其他任何功能?

  • 是否标准的真正的说,这是一个的必须的(即我需要声明,在这个例子中,这个的最大的每功能,将使用它?)

  • 这是同为C ++(这就是为什么我把C ++标签)?这是我第一次看到这样的事情。

  • What's the point of this extern int max inside the main or any other function?
  • Does the standard really says, that this is a must (that I need to declare, for this example, this max in each function, that will use it?)
  • Is this the same for C++ (that's why I placed the C++ tag)? This is the first time I see something like this.

注意:这是不一样的<一个href=\"http://stackoverflow.com/questions/9686198/what-is-the-use-of-declaring-a-static-variable-as-extern-inside-a-function\">What是利用声明静态变量作为函数内的extern的?

推荐答案

您后让我吃惊。我没有那回忆和我读过K&安培; R很久以前。我只在这里有第一版,它是有太多。然而,这还不是全部是算数的。从第一版:

Your post surprised me. I had no recollection of that and I've read K&R long ago. I only have the first edition here and it is there too. However, that is not all it says. From the first edition:

的变量也必须在想要每个函数声明
  访问;这可以通过一个明确的extern声明进行任何
  或隐式上下文。

The variable must also be declared in each function that wants to access it; this may be done either by an explicit extern declaration or implicitly by context.

上下文暗示。注意在后来的文本:

Note the "implicitly by context." Later in the text:

...如果一个变量的外部定义出现在源文件中
  其在一个特定的功能使用之前,那么就没有必要为一个
  extern声明中的作用。在外部声明主,
  ......因此多余的。事实上,常见的做法是将
  在源的开头的所有外部变量的定义
  文件,然后省略所有外部声明。

...if the external definition of a variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function. The extern declarations in main, ... are thus redundant. In fact, common practice is to place definitions of all external variables at the beginning of the source file, and then omit all extern declarations.

因此​​,这是说,使外部变量可以看到在函数内部做的只是功能,或者可以在任何函数外完成的源文件中的下列所有功能。我认为,这是在书的地方是在函数内部完成的唯一场所,后来它采用了熟悉的一次文件的方法。

So this is saying that making the extern variable visible can be done inside the function for just that function, or can be done outside any function for all functions following it in the source file. I believe that this is the only place in the book where it is done inside the function, later it uses the familiar once for the file approach.

这篇关于&QUOT;&的extern QUOT;一个函数里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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