Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #106942

Re: Can a variable be both static and extern

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Can a variable be both static and extern
Date 2017-03-24 17:38 +0100
Organization A noiseless patient Spider
Message-ID <ob3ht8$5fo$1@dont-email.me> (permalink)
References <6e7c0a5e-70aa-4163-b29a-3625ffc541d0@googlegroups.com> <58d50784.12570828@news.xs4all.nl> <ob3gad$var$1@dont-email.me> <pJbBA.201100$XG2.105941@fx08.iad>

Show all headers | View raw


On 24/03/17 17:22, Scott Lurndal wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 24/03/17 13:14, Richard Bos wrote:
>>
>> <snip>
>>
>>>
>>> In this example, it's not useful, in fact it would be wrong, to have the
>>> "static". I could think of a case in which it would be useful, but it
>>> would have to be a relatively complicated edge case. However, it would
>>> be possible; and forbidding "static" on the identifiers which can be
>>> "extern"ed would cause more problems than it would solve.
>>>
>>
>> The example you gave here is just a standard global variable - defined
>> (and initialised) in one file as "unsigned foo_counter = 1"; and
>> declared as "extern unsigned foo_counter;" in a header file that is
>> #included by all files that use it, including the file that defines it.
>>
>> While it is common to discourage the use of global (cross-module)
>> variables like this, it is certainly standard practice in C and used
>> heavily in some types of coding.
>>
>>
>> The question is, are there any real uses of:
>>
>> static int foo;
>> extern int foo;
>>
>> in the same translation unit (as different headers if you like) ?
>>
>> I can't think of any myself.  I find it odd that this combination is
>> allowed - I see no good uses for it, and it could easily occur as a
>> mistake (especially for people who write "extern" declarations directly
>> in the their C code, rather than just in headers).
>>
>> Of course, it could just be my lack of imagination here!
>>
> 
> While uncommon, one need not organize all #include preprocessor
> directives before file-scope declarations in a given translation
> unit.
> 

That's true.  (And there are sometimes good reasons for having
#include's in odd places.)

But to get the "static int foo; extern int foo;" pair you would have to
be writing:

// in "file.c"
// Here is a file-local variable foo:
static int foo;
#include "header.h"

// in "header.h"
// Here is a declaration of a program-global variable foo:
extern int foo;


And that second declaration is then wrong - you are /not/ declaring a
program-global variable.  You are re-declaring an existing file-local
variable.  So if you use "foo" later on, you are likely to be using the
/wrong/ foo.

It would be a fine example of why this ought to have been an error.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Can a variable be both static and extern acidlategamer@gmail.com - 2017-03-24 04:23 -0700
  Re: Can a variable be both static and extern Richard Heathfield <rjh@cpax.org.uk> - 2017-03-24 11:40 +0000
    Re: Can a variable be both static and extern acidlategamer@gmail.com - 2017-03-24 04:54 -0700
      Re: Can a variable be both static and extern Richard Heathfield <rjh@cpax.org.uk> - 2017-03-24 12:09 +0000
        Re: Can a variable be both static and extern James Kuyper <jameskuyper@verizon.net> - 2017-03-24 11:45 -0400
          Re: Can a variable be both static and extern raltbos@xs4all.nl (Richard Bos) - 2017-03-25 22:27 +0000
    Re: Can a variable be both static and extern Keith Thompson <kst-u@mib.org> - 2017-03-24 11:35 -0700
  Re: Can a variable be both static and extern Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-24 12:11 +0000
  Re: Can a variable be both static and extern raltbos@xs4all.nl (Richard Bos) - 2017-03-24 12:14 +0000
    Re: Can a variable be both static and extern David Brown <david.brown@hesbynett.no> - 2017-03-24 17:11 +0100
      Re: Can a variable be both static and extern scott@slp53.sl.home (Scott Lurndal) - 2017-03-24 16:22 +0000
        Re: Can a variable be both static and extern David Brown <david.brown@hesbynett.no> - 2017-03-24 17:38 +0100
      Re: Can a variable be both static and extern James Kuyper <jameskuyper@verizon.net> - 2017-03-24 12:23 -0400
        Re: Can a variable be both static and extern David Brown <david.brown@hesbynett.no> - 2017-03-24 17:41 +0100
          Re: Can a variable be both static and extern jameskuyper@verizon.net - 2017-03-24 09:57 -0700
          Re: Can a variable be both static and extern supercat@casperkitty.com - 2017-03-24 10:38 -0700
          Re: Can a variable be both static and extern Richard Heathfield <rjh@cpax.org.uk> - 2017-03-24 18:24 +0000
            Re: Can a variable be both static and extern "James R. Kuyper" <jameskuyper@verizon.net> - 2017-03-25 10:47 -0400
              Re: Can a variable be both static and extern Richard Heathfield <rjh@cpax.org.uk> - 2017-03-25 16:48 +0000
                Re: Can a variable be both static and extern Keith Thompson <kst-u@mib.org> - 2017-03-25 10:31 -0700
                Re: Can a variable be both static and extern Richard Heathfield <rjh@cpax.org.uk> - 2017-03-25 18:23 +0000
                Re: Can a variable be both static and extern supercat@casperkitty.com - 2017-03-25 12:28 -0700
                Re: Can a variable be both static and extern Ike Naar <ike@iceland.freeshell.org> - 2017-03-25 19:50 +0000
                Re: Can a variable be both static and extern supercat@casperkitty.com - 2017-03-25 13:20 -0700
                Re: Can a variable be both static and extern jameskuyper@verizon.net - 2017-03-25 10:44 -0700
                Re: Can a variable be both static and extern supercat@casperkitty.com - 2017-03-25 12:13 -0700

csiph-web