开发者

non-static declaration following static declaration

Consider these ex开发者_开发问答amples:

static int a;
extern int a; //OK -- what linkage does the a have now?

static int a;
int a; //ERROR

extern int a;
static int a; //ERROR

int a;
static int a; //ERROR

extern int a;
int a; //OK as expected

int a;
extern int a; //OK as expected

Why was it OK in the first example but not in the second?

As far as file-scope variables (global-scope) are concerned, these have external linkage and a static duration when no keyword is specified.

Thank you

AFAIK, linkage and storage duration for functions is a bit different.

EDIT: I've tried compiling using gcc 4.5.2 -Wall -pedantic --std=c99

More on: http://c-faq.com/decl/static.jd.html You can see that the 1st example works there too but 2nd doesn't. However, I don't see what makes them so different.


The answer to your first question is found in §6.2.2 of the C standard:

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. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.

So the linkage of a is internal.

For your second question, the second sentence of the immediately following paragraph is apropos:

5 If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

Because a is an object, not a function, the declaration int a; with no storage-class specifier gives a external linkage. The same section then has this to say:

7 If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

Since, in your second example, a appears with both internal and external linkage, this paragraph is triggered. One (particularly helpful) manifestation of undefined behaviour is the error that your compiler is producing.

All of your examples can be understood by these rules:

  1. int a; always declares a with external linkage;
  2. static int a; always declares a with internal linkage;
  3. extern int a; declares a with whatever linkage it already had, or external linkage if it had none;
  4. Two declarations of a in the same scope with different linkage give undefined behaviour.


I got same error when I forget "{" instead of ";" at the end of a declaration.

Example:

extern void *HASHMP_get(struct HASHMP_wf_s *hmwf_ptr, Uint8 *key);
extern void *HASHMP_remove(struct HASHMP_wf_s *hmwf_ptr, Uint8 *key) {
extern Uint16 HASHMP_clear(struct HASHMP_wf_s *hmwf_ptr);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜