Initializing extern

J

jacob navia

Consider:

extern int a = 56;

This provokes a warning in gcc, no warnings in Microsoft C,
where it compiles without warnings at the highest warning level,
and an error in lcc-win32.

What would be the best behavior?

Is it really a fatal error, or just a small inconsistency?

jacob
 
A

André Gillibert

[Followup to comp.std.c]

Consider:

extern int a = 56;

This provokes a warning in gcc, no warnings in Microsoft C,
where it compiles without warnings at the highest warning level,
and an error in lcc-win32.

What would be the best behavior?
It depends on the context.
At file scope, the code is well-formed as far as I know.
But, at block scope, it's ill-formed, and a diagnostic message is required.

From 6.7.8-5:
If the declaration of an identifier has block scope, and the identifier
has external or
internal linkage, the declaration shall have no initializer for the
identifier.

int main() {
extern int a = 56; /* ill-formed... An error is probably the "best"
behavior, but a warning is ok too */
}

But:
extern int a = 56; /* well-formed... An error is forbidden, a warning is
allowed but not necessary */
int main() {}
Is it really a fatal error, or just a small inconsistency?
At file scope, I don't see anything wrong with this code. It doesn't even
look like an "inconsistency" to me.
 
G

Geoff

Consider:

extern int a = 56;

This provokes a warning in gcc, no warnings in Microsoft C,
where it compiles without warnings at the highest warning level,
and an error in lcc-win32.

What would be the best behavior?

Is it really a fatal error, or just a small inconsistency?

jacob

Both VC++ 6.0 and 2005 emit "error C2205: 'a' : cannot initialize
extern variables with block scope" when doing this inside a function
and compile without error when doing this at file scope. In this case
the variable is linked as PUBLIC in the _DATA segment. I suppose in
this case you are mixing declaration with definition so Visual C links
it that way. What version of MS C did you use?
 
J

jacob navia

Geoff said:
Both VC++ 6.0 and 2005 emit "error C2205: 'a' : cannot initialize
extern variables with block scope" when doing this inside a function
and compile without error when doing this at file scope. In this case
the variable is linked as PUBLIC in the _DATA segment. I suppose in
this case you are mixing declaration with definition so Visual C links
it that way. What version of MS C did you use?

Version 8. But it was at the global level, not at an inner
scope.

Thanks for your answer

jacob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top