Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"extern" inside a function?

Tags:

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?

like image 327
Kiril Kirov Avatar asked Aug 30 '12 14:08

Kiril Kirov


People also ask

Can extern be used inside a function?

The extern must be applied to all declarations in all files. (Global const variables have internal linkage by default.) extern "C" specifies that the function is defined elsewhere and uses the C-language calling convention. The extern "C" modifier may also be applied to multiple function declarations in a block.

Can you extern a function in C?

The extern keyword in C and C++ extends the visibility of variables and functions across multiple source files. In the case of functions, the extern keyword is used implicitly. But with variables, you have to use the keyword explicitly.

Can extern variable be initialized?

You can initialize any object with the extern storage class specifier at global scope in C or at namespace scope in C++. The initializer for an extern object must either: Appear as part of the definition and the initial value must be described by a constant expression; or.

How do I use extern in CPP?

The “extern” keyword is used to declare and define the external variables. The keyword [ extern “C” ] is used to declare functions in C++ which is implemented and compiled in C language. It uses C libraries in C++ language.


2 Answers

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.

like image 111
Avi Berger Avatar answered Oct 04 '22 19:10

Avi Berger


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

like image 37
Scott Zhu Avatar answered Oct 04 '22 19:10

Scott Zhu