Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

c - error: extern declaration of 'i' follows declaration with no linkage

In the following program, I thought that extern int i; will change the following i to refer to the i defined outside main:

#include <stdio.h>

extern int i=1; // warning: 'i' initialized and declared 'extern'

int main()
{
    int i=2;
    printf("%d
", i);
    extern int i; // error: extern declaration of 'i' follows declaration with no linkage
    printf("%d
", i);
    return 0;
}

What is the reason of the "error: extern declaration of 'i' follows declaration with no linkage", where "declaration with no linkage" refers to int i=2;?

After I remove int i=2 in main,

  • the error is gone,
  • the warning "warning: 'i' initialized and declared 'extern'" on extern int i=1; also disappear . Why is that?

Thank you for explanations!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
#include <stdio.h>

int i=1;  // external variable

int main()
{
    int i=2;            // local variable
    printf("%d
", i);  // print local variable i==2

    {
    extern int i;       // point to external variable
    printf("%d
", i);  // print external variable i==1
    }

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...