home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20322 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.8 KB  |  63 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!europa.eng.gtefsd.com!gatech!swrinde!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!ira.uka.de!math.fu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!news.funet.fi!hydra!klaava!wirzeniu
  3. From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
  4. Subject: Re: initialized extern variables, coded in 1 file, used in many
  5. Message-ID: <1993Jan28.175755.7941@klaava.Helsinki.FI>
  6. Organization: University of Helsinki
  7. References: <1993Jan26.015543.23671@netcom.com> <926@ulogic.UUCP>
  8. Date: Thu, 28 Jan 1993 17:57:55 GMT
  9. Lines: 52
  10.  
  11. hartman@ulogic.UUCP (Richard M. Hartman) writes:
  12.  
  13. [ define EXTERN as extern by default, as nothing in main.c ]
  14.  
  15. >    EXTERN int uninitialized;
  16. >
  17. >    #ifdef EXTERN
  18. >    extern int initialized1;
  19. >    #else
  20. >    int initialized1 = 50;
  21. >    #endif
  22.  
  23. Wouldn't it be better to do
  24.  
  25.     EXTERN int uninitialized;
  26.     extern int initialized1;
  27.  
  28.     #ifdef MAIN
  29.     int initialized1 = 50;
  30.     #endif
  31.  
  32. instead, since this catches any errors that may creep in when you
  33. forget to change one copy of the initialized1
  34. declarations/definitions?
  35.  
  36. Of course, one might even go further and move the initializations out
  37. of the header altoghether, since they do no particular good there.  If
  38. they are in the header, and you change an initialization, make will
  39. recompile everything even if it isn't really needed.
  40.  
  41. If you do that, you can even forget about the EXTERN macro and use
  42. extern throughout, although then you have to add definitions for the
  43. uninitialized variables as well.
  44.  
  45. In case you didn't guess, this is the style I use.  In summary:
  46.  
  47. header.h:
  48.  
  49.     extern int foo;
  50.  
  51. main.c (or some other suitable source file):
  52.  
  53.     #include "header.h"
  54.  
  55.     int foo;
  56.  
  57. Easy, simple, fairly difficult to do mistakes, doesn't require
  58. preprocessor trickeries.
  59.  
  60. --
  61. Lars.Wirzenius@helsinki.fi  (finger wirzeniu@klaava.helsinki.fi)
  62.    MS-DOS, you can't live with it, you can live without it.
  63.