简体   繁体   English

在C中声明两个同名的全局变量

[英]Declaring two global variables of same name in C

I have declared two global variables of same name in C. It should give error as we cannot declare same name variables in same storage class.我已经在 C 中声明了两个同名的全局变量。它应该会出错,因为我们不能在同一个存储类中声明同名变量。

I have checked it in C++ — it gives a compile time error, but not in C. Why?我已经在 C++ 中检查过它——它给出了一个编译时错误,但在 C 中没有。为什么?

Following is the code:以下是代码:

int a;
int a = 25;
int main()
{

   return 0;
}

Check it out at : Code Written at Ideone查看:在 Ideone 编写的代码

I think this probably is the reason我想这可能是原因

Declaration and Definition in C C 语言中的声明和定义

But this is not the case in C++.但在 C++ 中情况并非如此。 I think in C++, whether the variable is declared at global scope or auto scope the declaration and definition is happening at the same time.我认为在 C++ 中,无论变量是在全局范围还是自动范围内声明,声明和定义都是同时发生的。

Could anyone throw some more light on it.任何人都可以对此多加了解。

Now when I define the variable two times giving it value two times it gives me error (instead of one declaration and one definition).现在,当我两次定义变量时,给它两次赋值,它会给我错误(而不是一个声明和一个定义)。

Code at : Two definitions now代码:现在有两个定义

int a;
int a;
int a;
int a = 25;

int main()
{
return 0;
}

In C, multiple global variables are "merged" into one.在 C 中,多个全局变量被“合并”为一个。 So you have indeed just one global variable, declared multiple times.所以你确实只有一个全局变量,多次声明。 This goes back to a time when extern wasn't needed (or possibly didn't exist - not quite sure) in C.这可以追溯到在 C 中不需要(或可能不存在 - 不太确定) extern的时代。

In other words, this is valid in C for historical reasons so that we can still compile code written before there was a ANSI standard for C.换句话说,由于历史原因,这在 C 中是有效的,因此我们仍然可以编译在 C 的 ANSI 标准出现之前编写的代码。

Although, to allow the code to be used in C++, I would suggest avoiding it.虽然,为了允许在 C++ 中使用代码,我建议避免它。

This is what the classic text "The C Programming Language" by Dennis Ritchie and Brain Kernighan says:这是 Dennis Ritchie 和 Brain Kernighan 的经典著作《The C Programming Language》所说的:

  1. An external declaration for an object is a definition if it has an initializer.如果对象具有初始化程序,则对象的外部声明就是定义

  2. An external object declaration that does not have an initializer, and does not contain the extern specifier, is a tentative definition .没有初始值设定项且不包含extern说明符的外部对象声明是暂定定义

  3. If a definition for an object appears in a translation unit, any tentative definitions are treated merely as redundant declarations.如果某个对象的定义出现在翻译单元中,则任何暂定定义仅被视为冗余声明。

  4. If no definition for the object appears in the translation unit, all its tentative definitions become a single definition with initializer 0 .如果在翻译单元中没有出现对象的定义,它的所有暂定定义将变成一个带有初始值设定项0定义。

So for example:例如:


extern int a =123; // this declaration is a definition as a is initialized to 123

int a; //this is a tentative definition

int a;
int a;
int a;
/*all the above the three tentative declarations are redundant*/

int a;
int a;
int a;

Futher in the code if no where do something as a=100 where we consider a corresponding to this global scope variable and not any other local variable with the same name and type, then ultimately, the global variable a shall be initialized to 0进一步在代码中如果没有 where 做某事a=100我们认为a对应于这个全局范围变量而不是任何其他具有相同名称和类型的局部变量,那么最终,全局变量a将被初始化为0


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM