高手:extern编译出错??

FengPrince 2012-11-30 04:13:40
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.

int main()
{
int i;
extern int i;
return ;
}
编译出错,说i无连接,明显跟最后一句话相悖啊!

PS:我承认,这个问题a很无聊。。。
...全文
317 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
七擒关羽 2012-12-03
  • 打赏
  • 举报
回复
引用 5 楼 FengPrince 的回复:
引用 4 楼 studyCBC 的回复:C/C++ code??123456int main(){ int i;extern int i;//i已被声明并定义了,你无法重声明return ;} 注意extern的用法,一般用于声明外部变量,当然也可以做一般声明的修饰 你那个是最基本的用法。。。 一般的书都是这样讲的。 实际的extern的含义要比这广。。。 ……
。。。。还在纠结,那解释详细点 1、extern只是修饰 可以有作用也可以没有作用 2、变量是有作用范围的,在同一个范围内不可以出现相同名字的变量名,因为编译器需要索引,相同类型,相同名称的变量在处理中会是相同名称,无法辨识 3、实在不清楚,又没有人解答,建议你看编译产生的文件名,注意不同的编译器会不同
FengPrince 2012-11-30
  • 打赏
  • 举报
回复
还找了一个同样的问题,而且能编译通过,虽然帖子没能解决问题,但仍值得参考。。。 http://cboard.cprogramming.com/c-programming/119914-c-standard-extern-no-linkage.html
FengPrince 2012-11-30
  • 打赏
  • 举报
回复
引用 11 楼 anhuizhuanjiao 的回复:
extern int i;//声明一个全局变量i int i; //定义一个全局变量i 就像 C/C++ code??1234567int i;int _tmain(int argc, _TCHAR* argv[]){ int i; system("pause"); return 0;} 这样是可以的,而 C/C++ code??1……
int i并非定义一个变量,当作为全局变量时,是tentative definition。它可以出现多次。 int i=250这才是定义。。。只能出现一次。 当然,在C++中就不一样了,均是定义。
转角天边 2012-11-30
  • 打赏
  • 举报
回复
extern int i;//声明一个全局变量i int i; //定义一个全局变量i 就像

int i;
int _tmain(int argc, _TCHAR* argv[])
{
	int i;
	system("pause");
	return 0;
}
这样是可以的,而

int _tmain(int argc, _TCHAR* argv[])
{
	int i;
        int i;
	system("pause");
	return 0;
}
不可以。 你这里也可以这么理解

extern int i;
int main()
{
    int i;
    return ;
}
这样可以,而你那样会重定义
FengPrince 2012-11-30
  • 打赏
  • 举报
回复
引用 7 楼 whizer 的回复:
楼主看的是C标准吧,你可以看看下面这段说明,它可以解释为什么i无连接. The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a f……
你这个是解释no linkage的。。。。 我在stackoverflow上找到答案了,问题跟我一模一样。。。
FengPrince 2012-11-30
  • 打赏
  • 举报
回复
我只想说,android平板(带键盘)在学习工作上跟PC比起来真是渣啊,IOS估计也好不哪去。难怪说平板是娱乐玩具。。。 哎,看来想找一个像WINDOWS/LINUX PC这样高效又能像平板这样轻便的工具是不可能了。 期待android能够完善啊,哎。。。。 PS:上面的帖子还没写完,光标不知怎么搞的,页面自动放大,直接提交了,晕。。。
FengPrince 2012-11-30
  • 打赏
  • 举报
回复
I have made it!! errr because your code violates a constraint in §6.7: 3 If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space, except for tags as specified in 6.7.2.3. http://stackoverflow.com/questions/7239911/block-scope-linkage-c-standard
whizer 2012-11-30
  • 打赏
  • 举报
回复
楼主看的是C标准吧,你可以看看下面这段说明,它可以解释为什么i无连接. The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.
常如意 2012-11-30
  • 打赏
  • 举报
回复
这样写没什么意义 查查extern的用法看看
FengPrince 2012-11-30
  • 打赏
  • 举报
回复
引用 4 楼 studyCBC 的回复:
C/C++ code??123456int main(){ int i;extern int i;//i已被声明并定义了,你无法重声明return ;} 注意extern的用法,一般用于声明外部变量,当然也可以做一般声明的修饰
你那个是最基本的用法。。。 一般的书都是这样讲的。 实际的extern的含义要比这广。。。
七擒关羽 2012-11-30
  • 打赏
  • 举报
回复

int main()
{
    int i;
extern int i;//i已被声明并定义了,你无法重声明
return ;
}
注意extern的用法,一般用于声明外部变量,当然也可以做一般声明的修饰
这不是鸭头 2012-11-30
  • 打赏
  • 举报
回复
extern int i;要写在不同的文件中
FengPrince 2012-11-30
  • 打赏
  • 举报
回复
引用 1 楼 ovoovo 的回复:
你这样写的意义在哪里?
我说过,确实很无聊的东西。 只不过,我想全面确认一下extern的用法。
  • 打赏
  • 举报
回复
你这样写的意义在哪里?

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧