Re: keyword extern

From: Emmanuel Delahaye (emdelYOURBRA_at_noos.fr)
Date: 06/26/04


Date: 26 Jun 2004 09:29:44 GMT

In 'comp.lang.c', umbs.sairam@gmail.com wrote:

> One of the purposes of using extern in multiple files is restricting
> the visibility of the variable only to the file that needs to "see"
> it.

It certainely is a bad practice. What if the type changes (int to long, float
to double, etc.) ? Because the compiler has no way to check the consistency
of the code, if you miss a change the code becomes inconsitent. This practice
is obviously wrong.

If you are concerned with the visibility of the global variables, which
is a good thing, there are other options:

- don't use global at all. (Use ADT's)
- give the user no right to change the data (a const pointer to the data)

/* data.h */
extern int const *px;

/* data.c */
#include "data.h"
static int x;
int const *px = &x;

Of course, in real life, data are gathered in some structure and a single
'context' pointer is enough.

Note that this 'protection' is just an intention. There is no way to prevent
an sloopy programmer to write and compile a code that uses a typecast to
force the pointer to a read/write access to the data. But it will invoke an
undefined behaviour. The ADT solution is safer.

> 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.

-- 
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/


Relevant Pages

  • Re: how this works ?
    ... int foo2 ... a pointer value is passed. ... If you use extern to qualify an object declaration, ... compiler that a definition exists at file scope, ...
    (comp.lang.c)
  • Re: how this works ?
    ... int foo2 ... a pointer value is passed. ... If you use extern to qualify an object declaration, ... compiler that a definition exists at file scope, ...
    (comp.lang.c)
  • Re: real newbie question on pointers
    ... This is also very old C practice, ... Below, because of sloppy braces, you have a function definition ... mac osx 10.4.11 tiger gcc compiler in terminal. ... int main ...
    (comp.lang.c.moderated)
  • Using Get() and Set() instead of accessing the variable directly
    ... I feel as though accessing variables directly is just bad practice. ... I can access private/protected variables, but when I divide up the class ... int m_x; ... I don't know if the compiler treats it as accessing the variable directly or ...
    (microsoft.public.vc.mfc)
  • Re: Valid C syntax.
    ... int  a,b,c; ... You should not over-think what the compiler will do. ... to specifications. ... In practice, I would accomplish the above in two statements if there ...
    (comp.lang.c)