在C语言中,为什么多次声明做工精细的全局变量而不是局部变量? [英] In C,why is multiple declarations working fine for a global variable but not for a local variable?

查看:325
本文介绍了在C语言中,为什么多次声明做工精细的全局变量而不是局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下code,为什么多次声明(和一个定义)一个全局变量X工作正常,但同样不为本地变量Y,这是主要的内工作()功能它显示了以下2个错误:

1)'Y'的,没有重复声明联动

2)previous宣言Y在这里

为什么示值误差为局部变量而不是全局变量?不只是我的书,但是从这个论坛下面的2个链接明确指出,我们可以在一个变量多次声明(虽然只定义一次)。

链接1
<一href=\"http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration\">link2

和好心照顾来解释什么是第一个错误'Y',没有联动的重声明中的无关联的部分是什么意思?有什么联系和给谁呢?会在哪里的局部变量链接?

 #包括LT&;&stdio.h中GT;    INT X;
    INT X;
    INT X = 303;    INT主要(无效)
    {      诠释Ÿ;
      诠释Ÿ;
      INT Y = 776; //如果上述两个声明被删除做工精细!      输出(x的值为%d,和y为%d,X,Y);    }


解决方案

在C和C ++, INTŸ; 在函数中既是声明和定义。

在C, INT X; 文件范围(任何函数之外)是一个声明和一个初步确定指标。多个暂定定义被允许;只有一个定义是允许的。

In the following code,why does multiple declarations(and one definition) for a global variable "x" works fine but the same doesn't work for a local variable "y" which is inside the main() function?It shows the following 2 errors :

1)redeclaration of 'y' with no linkage

2)previous declaration of 'y' was here

Why is it showing error for the local variable but not global variable?Not only my book,but the following 2 links from this forum clearly state that we can declare a variable multiple times (though define only once).

link1 link2

And kindly take care to explain what does the "with no linkage" part of the first error "redeclaration of 'y' with no linkage" mean?What linkage and to whom?Where would a local variable be linked?

    #include<stdio.h>

    int x;
    int x;
    int x=303;

    int main(void)
    {

      int y;
      int y;
      int y=776;   //Works fine if above 2 declarations are removed!!

      printf("The value of x is %d,and of y is %d",x,y);

    }

解决方案

In C and C++, int y; within a function is both a declaration and a definition.

In C, int x; in the file scope (outside any function) is a declaration and a tentative defintion. Multiple tentative definitions are allowed; only one definition is allowed.

这篇关于在C语言中,为什么多次声明做工精细的全局变量而不是局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆