"extern" inside a function?
Asked Answered
K

4

23

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?!


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).

So:

  • 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.

Note: this is not the same as What is the use of declaring a static variable as extern inside a function?

Khanate answered 30/8, 2012 at 14:51 Comment(5)
Amazon.com says K&R 2nd ed. was published in 1988. I'd guess the standard has changed a bit since then. :)Linneman
Yep, I know the year of publishing, that's what I thought, but I wanted to ask, just to be sure. That's why I asked for a confirmation from the ISO standard.Khanate
@KirilKirov you need quotes from C or C++ standard?Deicide
I think this was all cleared up with ANSI C /C89 back in the day.Introit
@Kiril Kirov You have to read the next paragraphs (and the whole chapter), which should clear up your confusion. e.g "if the definition of the external 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 chapter is to teach you a bit about linkage and scope, it does so with easy examples, not necessarily best practices.Cachucha
D
14

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:

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:

...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.

Diaphysis answered 30/8, 2012 at 15:43 Comment(0)
D
11

extern int max inside main or function is saying to the compiler "I am not a local variable inside the main or function, I am the global variable defined elsewhere".

If the global is declared in the same file, not useful. In different file,yes, but not in each function, just declare one time in the head file of the source that use this global variable. This is the same in c++.

Descant answered 30/8, 2012 at 15:18 Comment(0)
A
2

The extern is linkage. It means this name, max, is linked to other occurrences of the name, possibly in other files. (That is, when the object modules are linked together to make an executable, all the linked references to this name will be made to refer to the same object.)

The scope of this declaration is the remainder of the function body it is in. That means other functions in this file do not see the name declared by this declaration (unless they declare it themselves).

Scope and linkage are different things.

Antisepsis answered 30/8, 2012 at 15:6 Comment(2)
Even in K&R 2ed the use of extern in this example wasn't necessary. "In certain circumstances, the extern declaration can be omitted. If the definition of an external 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 first instance of int max would be taken as the declaration.Introit
The use of extern int max inside the function might not be necessary, but, if int max is present inside the function, the extern is necessary. Otherwise, int max by itself, inside the function, declares a new automatic object with no linkage. It will be different from the external max.Antisepsis
C
0

Another possible reason for extern inside a function is to make sure that no local variable is shadowing the global variable

Without extern:

int max = 33;
int main()
{
    int max; 
    printf("%d", max); // prints indeterminate value (garbage)
}

With extern

int main()
{
    extern int max; 
    int max;
    printf("%d", max);
}

Output:

error: redeclaration of ‘max’ with no linkage

Charade answered 15/2, 2022 at 11:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.