I read that the extern
keyword is implicit in the context of functions, so unless you specify otherwise using the static
keyword (which if I'm not mistaken is basically a completely separate concept from the static
that variables employ—they just share a keyword), they are visible to all object files. This makes sense; having the declarations be implicitly external, while technically unnecessary when the declarations are in the same file as the definition, is useful because the programmer doesn't have to type extern
every time they want to use a function out of its defining file, which is more often the case than not. What seems odd to me is that it is implicit for the declarations and the definition.
With a variable, I don't need to include an extern
for the definition, and in fact, while I can do this without error, my compiler gives me a warning for it.
For example, I can have mylib.h
:
int var = 5;
//it isn't necessary to write this as
//extern int var = 5;
//my compiler even warns against it
and test.c
#include "mylib.h"
extern int var;
I would normally assume the implicit extern
for functions to be the same, that is, if I defined int func(int par)
in mylib.h
, there would not be an implicit extern
before it, but there would be an implicit extern
for any declaration of it (such as if I declared it for use in test.c
).
It also doesn't make much sense from the perspective of the extern
keyword being used as a way of telling the compiler to look elsewhere for the definition, when the definition would never be external of the file it is in.
I feel like I'm missing something here.
int var
into an h-file and puttingextern int var
in th c-file is not the usual way. Not sure these to Qs are dupes but I think you'll find the needed info by reading: https://mcmap.net/q/17012/-how-do-i-use-extern-to-share-variables-between-source-files/4386427 and https://mcmap.net/q/15925/-what-is-the-difference-between-a-definition-and-a-declaration/4386427 – Conferralint var = 5;
inmylib.h
, then that defines the variable. In any given program, only one file can includemylib.h
— if more than one file includes the header, you get multiply-defined errors. With GCC 10.x, by default, even if you writeint var;
in a header (rather thanextern int var;
), you will end up with multiple definition errors — though older versions of GCC did not do that by default. – Durartestatic
keyword for both variables and functions limits the visibility of the named object. There are differences — you can have static variables inside functions, for example. But the concepts are sufficiently similar that saying "[static
applied to functions] is basically a completely separate concept from thestatic
that variables employ" is an overstatement at least. – Durarteint number = 0; double in_to_mm = 2.54;
which it places in one file, and the source file accessing the global variables usesextern int number; extern double in_to_mm;
– Repetitionextern
entirely. There is very limited use for the keyword in properly designed programs. A few special cases exist, but they are very specialized. – Caciaextern int number; extern double in_to_mm;
).To ensure proper cross-checking, the header should be included by the source file that defines and initializes those variables as well as by those that reference it. – Durartestatic
and you had to explicitly mark the function as visible outside the source file. – Durarteextern int var
thing out and the gcc compiler had no error or warning about my putting the definition in the header withoutextern
but including it with the declaration in the source file. Looking at sravs' answer, I guess it's because I only had one header and one source file in my test. – Repetitionextern
and the declaration has one, while in sravs' answer, it looks like it is the other way around where the definition has the keyword and the declarations omit it. – Repetitionextern
in definitions. You useextern
in declarations. The declaration simply says: This variable is defined in some other compilation unit, i.e. in another c-file. The use ofextern
declaration in h-files is simply to avoid to write the same stuff again and again in multiple c-files. – Conferralstatic
keyword, I thought that in the context of variables it is used for the purpose preventing the destruction of the variable between instances of running a function and that 'visibility' was more a concept of scope, where it depends on what 'level' of code block you put the variable in (i.e. loops, functions, conditions, etc.). – Repetitionstatic
orextern
, and regardless of whether it was a mistake or not; I do still wonder, why would the definitions have the implicitextern
? – Repetitionextern
for definitions, when we were talking about variables. I'm still kind of confounded by the implicit use in function definitions. – Repetitionextern
in a lot of h-files. – Conferral