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


Groups > comp.lang.c > #106899

Re: Can a variable be both static and extern

From raltbos@xs4all.nl (Richard Bos)
Newsgroups comp.lang.c
Subject Re: Can a variable be both static and extern
Date 2017-03-24 12:14 +0000
Organization Go wash your mouth.
Message-ID <58d50784.12570828@news.xs4all.nl> (permalink)
References <6e7c0a5e-70aa-4163-b29a-3625ffc541d0@googlegroups.com>

Show all headers | View raw


acidlategamer@gmail.com wrote:

> I was asked this in a interview
> 
> #include <stdio.h>
> 
> static int a;
> extern int a;//

Let's get the obvious out of the way first. Yes, this is a stupid way to
write code. Don't write code like this.

However, it's legal, and it does what the interviewers thought it does.
That's not because it's a reasonable way to _write_ code, but because
code like this may end up being generated by reasonable #includes.


As for the legalities, this is what the Standard says. (I happen to be
using C99 at the moment, but AFAIAA this hasn't changed, although
paragraph numbers may have.)

  6.2.2 #3: If the declaration of a file scope identifier for an object
             or a function contains the storage class specifier static,
             the identifier has internal linkage.

Your first a is a file scope identifier for an object, declared with
static. It therefore has internal linkage.

  6.2.2 #4: For an identifier declared with the storage-class specifier
            extern in a scope in which a prior declaration of that
            identifier is visible, if the prior declaration specifies
            internal or external linkage, the linkage of the identifier
            at the later declaration is the same as the linkage
            specified at the prior declaration. [...]

Your second a is declared with extern, in the file scope in which the
previous one is already visible; that previous declaration specified
internal linkage, and therefore, the second one does the same.

I'm not sure whether this counts as "hiding" or whether there legally is
only one a, but I do know this: 
- your second declaration is the one that eventually gets used;
- because of "extern", it keeps the linkage of any previous declaration;
- because of "static", the previous declaration had internal linkage;
- therefore, a ends up being a file-scope identifier for an object with
  internal linkage, and the rest of the code works just fine.


Now for why it is sane that things work this way, even if you shouldn't
explicitly write code this way.

Suppose you have a program consisting of several sources. One of these
sources declares an object (say "foo_counter"), and some of the other
sources need to modify that object when they create or destroy some
foos. Of course, you could go the whole Object Orientation route and add
functions to increase, decrease, and poll foo_counter; but suppose that
your program is currently quite simple (all it does is read input from
three sources and tally the total number of foos), so you don't want to
go that route.
What you do is have a header, say "foo.h", in which you declare

  extern unsigned foo_counter;

which you then #include in every source which modifies or reads
foo_counter. (You'd already have that header, presumably, because you
also want some widely used functions for, say, nicely formatting the
foo_counter.)
Now you have a source file called "file_input.c" that starts

  #include <stdio.h>
  #include "foo.h"

  int process_file(FILE *inputfile);

and a file called "serial_input.c" that does something similar, and so
forth, _and_ a file called "foo_common.c" that starts

  #include <stdio.h>

  unsigned foo_counter=1; // Foos count from 1;

  #include "foo.h"

  char *pretty_counter();

The result of _that_ #include "foo.h" will be almost the same as the two
declarations for a you have above, except that there is no "static".


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.

Richard

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