``extern'' in header files? 
Author Message
 ``extern'' in header files?

Hello all,

I have a simple style question. (Hopefully this won't turn into an ugly
religious debate...). Is there any overly useful significance of using
the extern keyword for function prototypes in header files?

#ifndef foo_h
#define foo_h
extern int foo (void);
#endif // !foo_h

as opposed to:

#ifndef foo_h
#define foo_h
int foo (void);
#endif // !foo_h

I've seen both styles done frequently. Is there actually any difference?
As far as I can tell, a prototype defined in a header is by default
external.
As a counter argument, by the time the compiler actually sees it, there
is
no difference between a local prototype for a non-static function and
one
from an include file. I imagine that a strictly conforming program
requires
the extern, but I can't imagine a case where omitting it causes a
problem.

Thoughts?

Dave Shawley
--
--



Wed, 14 May 2003 03:00:00 GMT  
 ``extern'' in header files?


Quote:
>I imagine that a strictly conforming program
>requires
>the extern, but I can't imagine a case where omitting it causes a
>problem.

It does not. By default, file scope names have extern linkage and the
syntax for function declarations (both K&R style and prototype) is
distinct from that for a function definition. This is not the case for
variables where the extern qualifier makes something a declaration when
otherwise it would have been a definition.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Wed, 14 May 2003 03:00:00 GMT  
 ``extern'' in header files?

Quote:

> I have a simple style question. (Hopefully this won't turn into an
> ugly
> religious debate...). Is there any overly useful significance of using
> the extern keyword for function prototypes in header files?
        ...
> I've seen both styles done frequently. Is there actually any
> difference?

In practice, no.  They're both equivalent; whether you use the `extern'
keyword for (external) function prototypes is a matter of style.

Obviously if what you're declaring a prototype is really static external
(local to that translation unit), then you would use `static' and that
would mean something distinct.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ I always entertain great hopes.
\__/ Robert Frost
    Erik Max Francis' bookmarks / http://www.alcyone.com/max/links/
 A highly categorized list of Web links.
--



Wed, 14 May 2003 03:00:00 GMT  
 ``extern'' in header files?

Quote:
>from an include file. I imagine that a strictly conforming program
>requires
>the extern, but I can't imagine a case where omitting it causes a
>problem.

In a function declaration, the keyword "extern" is superfluous.
Not even a strictly conforming program requires it.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
--



Wed, 14 May 2003 03:00:00 GMT  
 ``extern'' in header files?

Quote:
> Hello all,

> I have a simple style question. (Hopefully this won't turn into an ugly
> religious debate...). Is there any overly useful significance of using
> the extern keyword for function prototypes in header files?

> #ifndef foo_h
> #define foo_h
> extern int foo (void);
> #endif // !foo_h

> as opposed to:

> #ifndef foo_h
> #define foo_h
> int foo (void);
> #endif // !foo_h

> I've seen both styles done frequently. Is there actually any difference?
> As far as I can tell, a prototype defined in a header is by default
> external.
> As a counter argument, by the time the compiler actually sees it, there
> is
> no difference between a local prototype for a non-static function and
> one
> from an include file. I imagine that a strictly conforming program
> requires
> the extern, but I can't imagine a case where omitting it causes a
> problem.

I can see no reason why either form is not acceptably other than the //
commnets which are only required for C99 for which AFAIK there are no
conforming compilers available. Having said that MS C compiler has supported
it since at least version 6 as an extension.

Quote:

> Thoughts?

> Dave Shawley
> --
> --


--
John Castle
Too err is human, to really louse things up takes a computer!


--



Wed, 14 May 2003 03:00:00 GMT  
 ``extern'' in header files?

Quote:
>I have a simple style question. (Hopefully this won't turn into an ugly
>religious debate...). Is there any overly useful significance of using
>the extern keyword for function prototypes in header files?

  I feel the use of extern for functions is unofficially deprecated and ANSI function prototypes should be used instead.
Of course, there is a lot of code leftover from K+R days that still has this style.

 Use extern for "imported" global variables.
--



Sun, 18 May 2003 09:04:15 GMT  
 ``extern'' in header files?

Quote:

>   I feel the use of extern for functions is unofficially deprecated and ANSI function prototypes should be used instead.
> Of course, there is a lot of code leftover from K+R days that still has this style.

Huh? What is there in K+R days that puts some emphasis on `extern`-declarations?
Is there a `extern`-difference in K+R sources? I don't know, I do only know ansi-c
(it's a longlived standard now), so could you (or anyone) enlighten me, if there is
difference of externs for K+R headers? I thought, that ansi-c was mostly backward
compatible, so we could easily #include K+R headers...

thank,
-- guido                                Edel sei der Mensch, hilfreich und gut

--



Sun, 18 May 2003 03:00:00 GMT  
 ``extern'' in header files?


Quote:


> > I imagine that a strictly conforming program requires the extern,
> > but I can't imagine a case where omitting it causes a problem.

> It does not. By default, file scope names have extern linkage and the
> syntax for function declarations (both K&R style and prototype) is
> distinct from that for a function definition. This is not the case for
> variables where the extern qualifier makes something a declaration
> when otherwise it would have been a definition.

One such case being "extern sometype const whatever;".  Other cases
(I'm fishing here, trying to reduce my obvious ignorance)?

Cheers,

- Alf

--
alf_DOT_steinbach_AT_ac_DOT_com (clean the address before replying)
--



Mon, 19 May 2003 03:00:00 GMT  
 ``extern'' in header files?


Quote:

>>   I feel the use of extern for functions is unofficially deprecated and ANSI function prototypes should be used instead.
>> Of course, there is a lot of code leftover from K+R days that still has this style.

>Huh? What is there in K+R days that puts some emphasis on `extern`-declarations?
>Is there a `extern`-difference in K+R sources?

   K+R C - is shorthand for the language described in "The C Programming Language"
first edition 1978 ( THE reference when I first learned the language), there were no
function prototypes so you had to use 'extern' for functions. In NEW code written today
you would use a function prototype in the .h file and the client code (.c file) would include that
h file.  Some folks still insist on using extern with functions in their .h files but
I think this is an anachronistic practice.

Quote:
>I thought, that ansi-c was mostly backward
>compatible, so we could easily #include K+R headers...

   absolutely - you don't want to break all the existing code.
--



Tue, 20 May 2003 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Which header file for 'wstring'??

2. Problem with 'extern'

3. How to use 'extern'

4. Project header file. Probs with struct's

5. help! can't include header file

6. why no error occur although i didn't include header file

7. I need all of BC++ 3.0's header files

8. Why can't open precompiled header file?

9. Can't what header file is needed by PlaySound()

10. Headers files I can't find (wfc.h, wfd.h whd_global.h)

11. Warnings from Microsoft's STL header files in VC++ 6.0 SP2

12. Can't set a bitmapped image into my CListView's header control

 

 
Powered by phpBB® Forum Software