Re: extern



Bill Cunningham wrote:
Is extern used to externalize functions as well as variables? If I have this function,

int num(int n);

Should it be declared as above in the file in which main is called and compiled with the file that contains num's body? Or should it be declared as this,

extern num (int n);

The usual convention is that you would put the declaration ("int num(int n);") in a header (.h) file and then include that header file into any source (.c) file that called that function plus the source file that actually defined it.

"extern" is the default for function declarations, so it's not necessary. If you wanted to share a variable across multiple source files (a "global" variable), though, you would need the "extern" on its declaration in the header file.

S
.



Relevant Pages

  • Re: cout << char*
    ... "Igor Tandetnik" wrote: ... Each source file is ... Including CLib.h in library.cpp is adding the declarations to the definitions. ... int id ...
    (microsoft.public.vc.language)
  • Re: Why is this not an error in Visual C?
    ... says that when an old-style function definition is processed, ... That is, I think, `int fint x; ... Concerning the compatibility of function ... All declarations in the same scope that refer to the same object or function ...
    (comp.lang.c)
  • Re: Why is this not an error in Visual C?
    ... int foo ... What we're looking for to describe MSVC as broken is a constraint ... involve declarations of incompatible types. ... compatibility and of a composite type, ...
    (comp.lang.c)
  • Re: Why is this not an error in Visual C?
    ... says that when an old-style function definition is processed, ... That is, I think, `int fint x; ... Concerning the compatibility of function ... All declarations in the same scope that refer to the same object or function ...
    (comp.lang.c)
  • Re: range vs precisionC
    ... volatile int a; ... in this case that is volatile. ... and we have declarations like: ... What is needed now, I think, is to realize that subtypes (and all those ad ...
    (comp.lang.c)