Re: keyword extern

From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 06/26/04


Date: Sat, 26 Jun 2004 20:46:13 GMT

umbs.sairam@gmail.com wrote: (and stripped all attributions)
>
>>> In your question, in both the cases the variable "a" is visible in
>>> all the files. In such a situation, there is no benefit in using the
>>> keyword "extern. You might as well declare the variable as global.
>>
>> Sorry, I failed to understand your point. 'extern' doesn't make
>> global a variable. It's the lack of 'static' that makes it.
>
> I am sorry if I did not explain myself clearly. If we have the
> following files
> (pseudo-code)
>
> head.h
> /* common to all files */
>
> head1.h
> /* used in file1.c */
>
> file1.c
> include "head.h"
> include "head1.h"
> /* code of file1.c */
> .....
>
> headN.h
> /* used in fileN.c */
>
> fileN.c
> include "head.h"
> include "headN.h"
> /* code of fileN.c */
>
> Now, declare a variable in head1.h as int i and define it in
> file1.c as i = 10. My argument in the above post is, if "i" is
> declared multiple times using extern in all the files file2.c
> ... fileN.c then the significance of "extern"ing is lost. The
> programmer could have acheived the same functionality by
> declaring the variable in head.h

NEVER, ever, define a variable in a header file. Define it in the
.c file where it most naturally belongs. If this is a 'global'
variable, omit the "static" qualifier, otherwise include it for
all file scope variables. To make it available in other files
declare it with the "extern" qualifier in the header file for the
.c file in which defined.

Remember, the purpose of header files is to make something in the
.c file accessible to other .c files. Nothing more or less. The
fact that #include can be misused in various ways does not affect
this.

The very presence above of a head.h common to all files suggests
misuse. A variable visible in another file is subject to
accidental modification by a typo, or simply failing to remember
something obscure. Minimize and control scope as far as possible.

-- 
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!


Relevant Pages

  • Re: confused about extern use
    ... going to achieve by declaring it as extern in the header file a.h. ... composed on one or more compilation units. ... Essentially you *declare* the type wherever it is ...
    (comp.lang.c)
  • Re: Exported function mangaled name
    ... with many DLLs and supporting this for Windows 32 bit ... that header file; ... You have to declare the function the same way in your .cpp file as in ... extern "C" { ...
    (microsoft.public.vc.mfc)
  • Re: How to declare functions in so to be accessable to applications
    ... >> But the application tells my, that it didn't find the entry point ... > Your header file that the application uses, ... > prototype normally, not as an extern. ... When you declare it extern, ...
    (comp.unix.programmer)
  • Re: keyword extern
    ... my preferred way would be to declare it in all the .c files that ... > need the extern variable rather than put the extern declaration in the ... > header file and include the header file in the c files. ... A header is certainely not the place for an object definition for an ...
    (comp.lang.c)
  • Re: Exported function mangaled name
    ... that header file; it must be declared as __declspec ... You have to declare the function the same way in your .cpp file as in ... #define LIBSPEC __declspec ... See my essay on The Ultimate DLL Header File on my MVP Tips site. ...
    (microsoft.public.vc.mfc)