全局变量与局部变量的重新声明

时间:2022-11-10 16:44:49

When I compile the code below

当我编译下面的代码时

#include<stdio.h>

int main()
{
  int a;
  int a = 10;
  printf("a is %d \n",a);
  return 0;
}

I get an error:

我收到一个错误:

test3.c: In function ‘main’:
test3.c:6:5: error: redeclaration of ‘a’ with no linkage
test3.c:5:5: note: previous declaration of ‘a’ was here

But if I make the variable global then it works fine.

但是,如果我将变量设为全局,那么它可以正常工作。

#include<stdio.h>

int a;
int a = 10;
int main()
{
  printf("a is %d \n",a);
  return 0;
}

Why is declaring the same global variable twice not an error, but doing that for a local variable is an error?

为什么两次声明相同的全局变量而不是错误,但对局部变量执行此操作是错误的?

3 个解决方案

#1


16  

In C, the statement int a; when made at file scope, is a declaration and a tentative definition. You can have as many tentative definitions as you want, as long as they all match each other.

在C中,语句int a;在文件范围内制作时,是一个声明和一个暂定的定义。您可以拥有任意数量的暂定定义,只要它们彼此匹配即可。

If a definition (with an initializer) appears before the end of the translation unit, the variable will be initialized to that value. Having multiple initialization values is a compiler error.

如果定义(使用初始化程序)出现在翻译单元结尾之前,则该变量将初始化为该值。具有多个初始化值是编译器错误。

If the end of the translation unit is reached, and no non-tentative definition was found, the variable will be zero initialized.

如果到达翻译单元的末尾,并且未找到非暂定定义,则该变量将初始化为零。

The above does not apply for local variables. Here a declaration also serves as a definition, and having more than one leads to an error.

以上不适用于局部变量。这里的声明也可以作为定义,并且有多个声明会导致错误。

#2


1  

The other reason I could think of is that un-initialized global variables are stored in the BSS (Block Structured Segment) where are the global variables that are initialized are stored in Data Segment.

我能想到的另一个原因是未初始化的全局变量存储在BSS(块结构化段)中,其中初始化的全局变量存储在数据段中。

I am guessing that there is some kind of a name space resolution and when there is a conflict the variable in the Data segment overrides the one in the Block Structured Segment.

我猜测存在某种名称空间分辨率,当存在冲突时,数据段中的变量将覆盖块结构化段中的变量。

if you were to declare

如果你要宣布

int a =5 int a = 10

int a = 5 int a = 10

in the global scope (both in the data segment) there would be conflict as expected.

在全球范围内(均在数据部分中)会出现预期的冲突。

#3


1  

You can't have two global variables with the same name in C program. C might allow multiple definitions in the same file scope through the tentative definition rule, but in any case all definitions will refer to the same variable.

在C程序中,不能有两个具有相同名称的全局变量。 C可能允许通过暂定定义规则在同一文件范围中进行多个定义,但无论如何所有定义都将引用相同的变量。

Local Variable

In C, multiple local variables are not "merged" into one.

在C中,多个局部变量不会“合并”为一个。

All the local variables with the same name will be referring to the different int-sized piece of memory.

具有相同名称的所有局部变量将引用不同的int大小的内存块。

 #include<stdio.h>

 int main()
 {
  int a;
  int a = 10;
  printf("a is %d \n",a);  
  return 0;
 }

So when assigning the memory to the redeclaration of the same variable it gives an Error.

因此,当将内存分配给同一变量的重新声明时,它会给出错误。

Global Variable

In C, multiple global variables are "merged" into one. 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中,多个全局变量被“合并”为一个。所以你确实只有一个全局变量,多次声明。这可以追溯到C.不需要extern(或者可能不存在 - 不太确定)的时候。

In other words, all global variables with the same name will be converted to be one variable - so your

换句话说,所有具有相同名称的全局变量将被转换为一个变量 - 所以你的

#include<stdio.h>

int a;
int a = 10;
int main()
{
printf("a is %d \n",a);
return 0;
}

will be referring to the same int-sized piece of memory.

将引用相同的int大小的内存。

#1


16  

In C, the statement int a; when made at file scope, is a declaration and a tentative definition. You can have as many tentative definitions as you want, as long as they all match each other.

在C中,语句int a;在文件范围内制作时,是一个声明和一个暂定的定义。您可以拥有任意数量的暂定定义,只要它们彼此匹配即可。

If a definition (with an initializer) appears before the end of the translation unit, the variable will be initialized to that value. Having multiple initialization values is a compiler error.

如果定义(使用初始化程序)出现在翻译单元结尾之前,则该变量将初始化为该值。具有多个初始化值是编译器错误。

If the end of the translation unit is reached, and no non-tentative definition was found, the variable will be zero initialized.

如果到达翻译单元的末尾,并且未找到非暂定定义,则该变量将初始化为零。

The above does not apply for local variables. Here a declaration also serves as a definition, and having more than one leads to an error.

以上不适用于局部变量。这里的声明也可以作为定义,并且有多个声明会导致错误。

#2


1  

The other reason I could think of is that un-initialized global variables are stored in the BSS (Block Structured Segment) where are the global variables that are initialized are stored in Data Segment.

我能想到的另一个原因是未初始化的全局变量存储在BSS(块结构化段)中,其中初始化的全局变量存储在数据段中。

I am guessing that there is some kind of a name space resolution and when there is a conflict the variable in the Data segment overrides the one in the Block Structured Segment.

我猜测存在某种名称空间分辨率,当存在冲突时,数据段中的变量将覆盖块结构化段中的变量。

if you were to declare

如果你要宣布

int a =5 int a = 10

int a = 5 int a = 10

in the global scope (both in the data segment) there would be conflict as expected.

在全球范围内(均在数据部分中)会出现预期的冲突。

#3


1  

You can't have two global variables with the same name in C program. C might allow multiple definitions in the same file scope through the tentative definition rule, but in any case all definitions will refer to the same variable.

在C程序中,不能有两个具有相同名称的全局变量。 C可能允许通过暂定定义规则在同一文件范围中进行多个定义,但无论如何所有定义都将引用相同的变量。

Local Variable

In C, multiple local variables are not "merged" into one.

在C中,多个局部变量不会“合并”为一个。

All the local variables with the same name will be referring to the different int-sized piece of memory.

具有相同名称的所有局部变量将引用不同的int大小的内存块。

 #include<stdio.h>

 int main()
 {
  int a;
  int a = 10;
  printf("a is %d \n",a);  
  return 0;
 }

So when assigning the memory to the redeclaration of the same variable it gives an Error.

因此,当将内存分配给同一变量的重新声明时,它会给出错误。

Global Variable

In C, multiple global variables are "merged" into one. 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中,多个全局变量被“合并”为一个。所以你确实只有一个全局变量,多次声明。这可以追溯到C.不需要extern(或者可能不存在 - 不太确定)的时候。

In other words, all global variables with the same name will be converted to be one variable - so your

换句话说,所有具有相同名称的全局变量将被转换为一个变量 - 所以你的

#include<stdio.h>

int a;
int a = 10;
int main()
{
printf("a is %d \n",a);
return 0;
}

will be referring to the same int-sized piece of memory.

将引用相同的int大小的内存。