Re: keyword extern
From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 06/26/04
- Next message: Joona I Palaste: "Re: whether this is valid"
- Previous message: madhur: "whether this is valid"
- In reply to: umbs.sairam_at_gmail.com: "Re: keyword extern"
- Next in thread: Minti: "Re: keyword extern"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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!
- Next message: Joona I Palaste: "Re: whether this is valid"
- Previous message: madhur: "whether this is valid"
- In reply to: umbs.sairam_at_gmail.com: "Re: keyword extern"
- Next in thread: Minti: "Re: keyword extern"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|