Path: utzoo!utgpu!water!watmath!clyde!rutgers!cmcl2!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: extern Keywords: extern qualifier Message-ID: <7123@brl-smoke.ARPA> Date: 18 Jan 88 02:02:52 GMT References: <17428@topaz.rutgers.edu> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 38 In article <17428@topaz.rutgers.edu> mccarrol@topaz.rutgers.edu () writes: >First: Is it possible to put an external declaration outside the body >of any function? Sure, In fact, that's my usual approach. Some people who like the idea of an "import" specifier always put their extern declarations inside their function bodies. >I'm writing a large program in several separate >modules, and I'd like to have a listing at the beginning of each file >of what external variables are used in the file. Can extern be used >this way? Yes. Note that this conveniently supports having a module-specific header file that contains extern declarations for all module entry points and global data (i.e. a module interface specification header), with the same header #included in the source code for the module itself. >Second: I know that my compiler doesn't complain against extern's >outside of function bodies, but I'm not sure if it does what I want. >Is the compiler allocating space for these variables, or is it just >identifying thier names as a reference to something from another >source file, to be resolved at link-time? Not quite either. There are two possible methods of implementation of extern data in C, but the model you should think of is: If it says "extern", it is a reference to something allocated elsewhere; if it doesn't say "extern", it allocates storage, and of course storage should be allocated for an object precisely once among all the modules for an application. The module interface specification header I mentioned above must, therefore, declare all its global data as "extern", in order to not allocate it in every file that #includes the header. "extern" does not necessarily mean "in another source file"; the actual definition can also occur elsewhere in the same source file.