home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11395 < prev    next >
Encoding:
Text File  |  1992-07-21  |  2.7 KB  |  74 lines

  1. Path: sparky!uunet!ogicse!decwrl!world!ksr!jfw
  2. From: jfw@ksr.com (John F. Woods)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Summary: Why use "extern" on function prototypes.
  5. Message-ID: <14004@ksr.com>
  6. Date: 21 Jul 92 15:58:50 GMT
  7. Article-I.D.: ksr.14004
  8. References: <12271@anderson> <1992Jul21.070641.13516@dcs.qmw.ac.uk>
  9. Sender: news@ksr.com
  10. Lines: 62
  11.  
  12. Since I'm following up a followup, and not sure of the attributions anymore,
  13. I'm just going to shear them off.
  14.  
  15. >> I checked in 'C the Complete Reference' by the C god himself, H.Schildt.
  16. >> He says that extern variables (and functions) are not allocated in that
  17. >> section of the program.  Thus, I think extern, although not necessary in
  18. >> headers, is used to reduce memory considerations.
  19.  
  20. Oh worshipper of false deities!  Had you read "The C Programming Language"
  21. by Kernighan and Ritchie, you would have learned that there are two kinds
  22. of ways of referring to external variables, _declarations_ and _definitions_.
  23. Declarations announce the properties of a variable; definitions also cause
  24. storage to be set aside.  The (file scope) declaration
  25.  
  26.     int i;
  27.  
  28. is called a "tentative definition"; if no definition with an initializer
  29. is seen later, it will define the variable i with an initial value of 0;
  30. otherwise, it will act the same as
  31.  
  32.     extern int i;
  33.  
  34. which is a declaration referring to some other definition.  There most be
  35. no more than one definition of an identifier; multiple definitions of the
  36. same identifier in a program does not result in extra copies of the variables,
  37. it is an *error*.  It is for this reason that object declarations in header
  38. files should contain the word "extern".
  39.  
  40. For functions, the situation is slightly different (but still the same).  This
  41. is a *definition* of a function:
  42.  
  43.     void foo() {
  44.         /* ok, so it isn't a very interesting function. */
  45.     }
  46.  
  47. This is a *declaration*:
  48.  
  49.     extern void foo();
  50.  
  51. But this is ALSO a declaration:
  52.  
  53.     void foo();
  54.  
  55. because there is no function body.  Function declarations do not *need* the
  56. keyword extern to indicate that they are declarations.
  57.  
  58. Now, there is one extra subtlety to the above (which, by the way, the Standard
  59. (rather, the Rationale thereto) calls the "Strict Ref/Def Model".  Many
  60. historical C implementations (in particular, the one written by Messrs. K&R)
  61. use the "Relaxed Ref/Def Model", in which tentative definitions remain
  62. tentative until link time; so, while the Standard considers
  63.  
  64.     file1.c                file2.c
  65.     int i;                int i;
  66.     foo() {                bar() {
  67.         i = 1;                i = 2;
  68.     }                }
  69.  
  70. to erroneously define "i" twice, the Relaxed Ref/Def model used by UNIX will
  71. fold *all* tentative definitions in multiple translation units into one
  72. tentative definition.  In this model, it is acceptable for header files not
  73. to use extern to declare external data objects.
  74.