Sujet : Re: do { quit; } else { }
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 10. Apr 2025, 16:41:50
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250410080629.532@kylheku.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-04-10, David Brown <
david.brown@hesbynett.no> wrote:
So currently, I have no explanation for why you may write "static int
foo; extern int foo;" and have "foo" be internal linkage, while "extern
int foo; static int foo;" is not allowed.
What's also not allowed is "static int foo; int foo;" !
It's because "extern" means "refer to the existing file scope
declaration of the identifer if there is one propagating its
properties, including linkage; otherwise if it doesn't exist,
create an external linkage reference"
You can use extern in a scope:
...
{
{
extern int putchar(void);
...
Even if the function has internal linkage, you must still use
extern.
static void foo(void);
void bar(void)
{
static int foo(void); // wrong: must be extern
If the "extern" keyword were called "filescope", it would be better
reflect its action of referring to a filescope declaration of the
identifier, assumed to have external linkage if it the declaration
doesn't already exist.
Also note how extern has the property of suppressing a definition.
In a header file, for an object, we usually don't want:
int obj;
because that is a tentative definition, which will convert to a
definition at the end of each translation unit which includes it. If we
put in extern, it's no longer a tentative definition, but only a
declaration. (Unless there is an initializer.)
But if someone else has an explanation for this, or a realistic use-case
for static followed by extern, I am curious to hear about it.
The only thing I can think of is to skip around shadowing:
static int x(void);
...
{
int x; // shadowing identifier
{
extern int x(void); // "unshadowing"
Macro-generated code could use this to make sure its references
are not shadowed.
However, if the macro interpolates argument expressions supplied
by the use site, and those expressions expect to refer to "x",
then it won't work. Hygiene has not been achieved.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca