Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #106898
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Can a variable be both static and extern |
| Date | 2017-03-24 12:11 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87poh6vpqv.fsf@bsb.me.uk> (permalink) |
| References | <6e7c0a5e-70aa-4163-b29a-3625ffc541d0@googlegroups.com> |
acidlategamer@gmail.com writes:
> 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;
> }
>
> I said I will refrain from writing such things in a code, whatever the
> use case (because I really had no idea).
That's a good answer. It's obviously confusing and not a good idea, but
I suppose the situation might arise as code gets changed without looking
ate the full picture.
> Later I told that it should be a compilation error
An "extern" declaration (note: not a definition) does not change the
linkage of a previously declared identifier with internal linkage so the
code if conforming and should compile.
I suppose there is some scope for misunderstanding here since "it should
be a compilation error" might mean that the interviewer knows that it is
not an error, but thinks it should be one.
> 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.
Yes, as it must with any conforming compiler.
> 1. Where I am wrong?
> 2. What would be your answer?
I'd say pretty much what you said, though I'd point out that is was
legal code and then I'd try to bite my tongue if told that that's wrong.
> 3. Is there any use case exists?
I'll pass on that one!
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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