如果变量重复定义了,会出现什么情况?

screen12 2011-06-30 09:32:04
我把两个程序合并在一起,当然所有全局变量也合在一起了。本来以为,会出现很多错误,因为很多变量重名,但是编译却成功了,仔细一看,原来变量重复并不报错。比如
int a;
。。。
int a;

编译能通过。

可是:我却希望编译器能当作错误处理,因为我这些同名的变量在两个程序中是用于不同用途的。现在不报错,怎么办?

如果是警告,那么警告号是多少?就是那个warning c....什么的。
...全文
477 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
就想叫yoko 2011-06-30
  • 打赏
  • 举报
回复
你用的是gcc吧
screen12 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 babilife 的回复:]
引用 5 楼 screen12 的回复:
都是全局变量。

全局不报错?啥编译器?
[/Quote]
我又试了一下,这回报错了。大概是这样的:如果定义两个变量而不初始化,不会报错。如果两个变量均初始化,会报错。
ff007bit 2011-06-30
  • 打赏
  • 举报
回复
全局变量应该会报错的,重复定义啊.你是咋合并的?
另外,变量名要取得有意义一些,可以和功能相关,总之不要a,b,c之类的,会带来很多问题.
flyxkh 2011-06-30
  • 打赏
  • 举报
回复
局部变量先生效 局部失效后就是全局生效
至善者善之敌 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 screen12 的回复:]
都是全局变量。
[/Quote]
全局不报错?啥编译器?
screen12 2011-06-30
  • 打赏
  • 举报
回复
都是全局变量。
就想叫yoko 2011-06-30
  • 打赏
  • 举报
回复
你咋合并的
说来听听
赵4老师 2011-06-30
  • 打赏
  • 举报
回复
比如A.c和B.c
请先
将A.c中的变量a替换为A_a
将B.c中的变量a替换为B_a
再合并。
金刚葫芦娃 2011-06-30
  • 打赏
  • 举报
回复
都是全局变量? 如果一个全局,和一个局部变量就不报错,执行的时候就近取值.
至善者善之敌 2011-06-30
  • 打赏
  • 举报
回复
如果是局部的变量,可以不用理会。。
AnYidan 2011-06-30
  • 打赏
  • 举报
回复
不是直接将两个文件 copy 到一起吧?

看看

The scope of a name is the part of the program within which the name can be used.

For an automatic variable declared at the beginning of a function, the scope is the function in which the name is declared. Local variables of the same name in different functions are unrelated. The same is true of the parameters of the function, which are in effect local variables.

The scope of an external variable or a function lasts from the point at which it is declared to the end of the file being compiled.

-- the c programming language p72

如果你没有初始化,我看不出为何要报错,

69,387

社区成员

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

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