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


Groups > comp.lang.c > #106977

Re: Can a variable be both static and extern

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Can a variable be both static and extern
Date 2017-03-24 11:35 -0700
Organization None to speak of
Message-ID <lnziga8qvg.fsf@kst-u.example.com> (permalink)
References <6e7c0a5e-70aa-4163-b29a-3625ffc541d0@googlegroups.com> <ob30dn$6b6$1@dont-email.me>

Show all headers | View raw


Richard Heathfield <rjh@cpax.org.uk> writes:
> On 24/03/17 11:23, acidlategamer@gmail.com wrote:
>> I was asked this in a interview
>>
>> #include <stdio.h>
>>
>> static int a;
>> extern int a;//
>>
>> int main()
>> {
>>   a = 10;
>>   printf("%d\n", a);
>>   return 0;
>> }
>
> That isn't a question, unless the implied question is "what does this 
> code mean?" in which case the correct answer is "it means that whoever 
> wrote it needs to be transferred to Marketing".
>
> The output of the code will be 10 and a newline character.
>
>> I said I will refrain from writing such things in a code, whatever
>> the  use case (because I really had no idea).
>
> You gave the right answer, but for the wrong reason. It's the right 
> answer because it's senseless to give 'a' file scope. It's also 
> senseless to call it 'a'.
>
>> Later I told that it should be a compilation error as static makes a
>> to live only while this file runs, extern makes it seem that a can live
>> outside of this scope, that is contradictory. This program compiled.
>
> I see no reason why it shouldn't compile.
>
>> 1.    Where I am wrong?
>
> I don't think you /are/ wrong in this case.
>
>> 2.    What would be your answer?
>
> Bring me the head of your project team leader!
>
>> 3.    Is there any use case exists?
>
> No. It's stupid code. There certainly are cases where file scope 
> identifiers are better than the alternatives, but this isn't one of 
> those cases.

If code like this appeared in production, then yes, it would be
stupid.  But the goal of this code isn't to print "10" and a newline;
that purpose would be served by `puts("10")`.  The goal is to ask a
question about the interaction of "static" and "extern".  Making the
code more elaborate so that the interaction actually makes sense
would just add clutter, and likely raise other irrelevant questions.

There is, in my opinion, nothing wrong with writing silly and useless
little sample programs whose purpose is to demonstrate a point
about the language.  And it has serve that purpose here, by raising
questions about the reasoning behind a particular language rule.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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