0

I'm having trouble understanding the usage of extern and the topic of declaration vs definition. I don't get why

extern int i;

is a declaration (no memory allocation), while

int i;

is a definition (memory is allocated).

Aren't both statements only declaring a variable i as an int, only that one specifies that it's a global variable?

Areg Sarvazyan
  • 199
  • 2
  • 13
  • 1
    How should a definition look, then? – Gerhardh Apr 10 '18 at 05:15
  • IMHO, [Steve Jessop's answer to that question](https://stackoverflow.com/a/1411064/539810) is very good. There's also the idea in C of a "tentative definition" for global variables. You can write `int i; int i; int i;` and it's the same as `int i;` (you can do the same with `static int i;` as long as they're all `static`). If no external definitions are found before the end of the translation unit (e.g. `int i = 20;`), then an implicit `int i = 0;` at the end of the translation unit is done, creating an external definition. Linkage must still agree however, so `static int i; int i;` is invalid –  Apr 10 '18 at 07:06

1 Answers1

0

First let us clarify the difference between declaration and definition :

Declaration of a variable/function simply declares that the variable/function exists somewhere in the program but the memory is not allocated for them. But the declaration of a variable/function serves an important role. And that is the type of the variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function.

Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. Therefore, we can think of definition as a super set of declaration. (or declaration as a subset of definition).

From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function).

Since the declaration can be done any number of times and definition can be done only once, we can notice that declaration of a function/variable can be added in several C/H files or in a single C/H file several times. But we notice the actual definition of the function/variable only once (i.e. in one file only). And as the extern extends the visibility to the whole program, the functions/variables can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known. (By knowing the declaration of the function, C compiler knows that the definition of the function/variable exists and it goes ahead to compile the program).

In the case of int a;, what it is depends on where it is placed in the source code:

  • Within a function, int a; is a definition - it requests the creation of an int variable with automatic storage duration (and of course
    also a declaration);

  • At file scope, the answer is different in C and C++. In C, int a; is atentative definition (of which there can be more than one, as long as the types and linkage are agreeable); in C++, it is an ordinary definition with external linkage.

  • Within a struct or union specifier,int a; is a declaration of a member.

Abhishek Keshri
  • 3,074
  • 14
  • 31