CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

为什么函数可以重复声明,而变量不能?

楼主zhngxVPP(萧何)2006-03-20 11:38:06 在 C/C++ / C语言 提问

void   fun1();  
  void   fun1();  
   
  void   main()  
  {  
          fun1();  
  }  
   
  void   fun1()  
  {  
      printf("hello!");  
  }  
   
  为什么两次声明了函数不会有错误,而两次声明变量就不行呢   ?如int   a;   int   a; 问题点数:10、回复次数:34Top

1 楼steedhorse(晨星)回复于 2006-03-20 11:44:17 得分 0

变量也可能重复声明的,但不可以重复定义。  
  同样,函数也不可以重复定义的。Top

2 楼zez(思恩 闭关练功ing...)回复于 2006-03-20 11:44:55 得分 0

声明变量是   让   系统   创建   一个变量  
   
  声明函数知识   告诉   系统   这里   有一个这样的函数  
   
  Top

3 楼steedhorse(晨星)回复于 2006-03-20 11:45:20 得分 0

比如假如这样肯定不行的:  
  void   fun1();  
   
  void   main()  
  {  
          fun1();  
  }  
   
  void   fun1()  
  {  
      printf("hello!");  
  }  
  void   fun1()  
  {  
      printf("hello!");  
  }Top

4 楼steedhorse(晨星)回复于 2006-03-20 11:49:35 得分 0

变量重复定义的例子如下,没问题的。  
   
  #include   <stdio.h>  
   
  int   i;  
  int   i;  
  int   i;  
  int   i;  
  int   i;  
  int   i   =   6;  
  extern   int   i;  
  extern   int   i;  
  extern   int   i;  
  extern   int   i;  
  extern   int   i;  
   
  int   main()   {  
  printf("%d\n",   i);  
  }Top

5 楼steedhorse(晨星)回复于 2006-03-20 11:50:48 得分 0

Sorry,打错了,“变量重复声明的例子如下”,汗一吧。。。。-_-Top

6 楼cunsh(村少)回复于 2006-03-20 11:57:42 得分 0

ooooooooooooooTop

7 楼ydfivy(我就是一送外卖的)回复于 2006-03-20 17:10:06 得分 0

变量的声明应该是加上extern才是声明.  
  不然就是定义了.Top

8 楼xiaobubu()回复于 2006-03-20 17:50:10 得分 0

你完全把声明和定义混在了一起,Study!Top

9 楼lyskyly(浮生三笑)回复于 2006-03-20 18:30:31 得分 0

全局变量被视为一个临时性的定义,可声明多次,最后被链接器折叠起来,只有一个实体Top

10 楼cuibo1123(月满C楼)回复于 2006-03-20 18:43:43 得分 0

extern   int   i;  
    是声明变量~  
  int   i;   是定义变量~  
   
   
  不一样的~  
   
  呵呵Top

11 楼hustacsky(见好不收)回复于 2006-03-20 18:59:09 得分 0

声明是在编译时,定义赋值是在运行时。所以可能重复声明的,但不可以重复定义。Top

12 楼adintr(www.adintr.com)(风流总被雨打风吹去)回复于 2006-03-20 19:05:12 得分 0

关于声明和定义:  
  http://www.adintr.com/program/article/04.cppcpt.htmlTop

13 楼cg5353(努力学习中)回复于 2006-03-20 22:13:08 得分 0

本来不论什么都可以声明多次的,但只能定义一次  
   
  不过,变量的int   i声明同时也是定义,所以这样的声明只能有一次  
  比如下面的代码,系统会为i分配一个空间,赋予随机值  
  #include   <stdio.h>  
  int   main()  
  {  
  int   i;  
                    //多一个   int   i;   即会编译错误  
  printf("%d\n",   i);  
  system("pause");  
  }  
   
  而extern   int   i就仅仅是声明而已,因此这样的声明可以有多次  
  这样的话系统不会为i分配空间,因此编译到printf时出错  
  #include   <stdio.h>  
  int   main()  
  {  
  extern   int   i;  
  extern   int   i;       //多一个extern   int   i;无影响,错误是在下面的printf  
  printf("%d\n",   i);  
  system("pause");  
  }  
  Top

14 楼ytfrdfiw()回复于 2006-03-20 22:25:59 得分 0

来晚了。上面说得很好。  
  int   i;           //既是声明,也是定义  
  extern   int   j;     //纯粹是一个声明Top

15 楼iamcaicainiao(老菜,长征)回复于 2006-03-21 08:43:31 得分 0

顶打斑竹晨星。Top

16 楼ToHardToGetAnValidId()回复于 2006-03-21 08:56:49 得分 0

变量重复定义的例子如下,没问题的。  
   
  #include   <stdio.h>  
   
  int   i;  
  int   i;  
  int   i;  
  int   i;  
  int   i;  
  int   i   =   6;  
  extern   int   i;  
  extern   int   i;  
  extern   int   i;  
  extern   int   i;  
  extern   int   i;  
   
  int   main()   {  
  printf("%d\n",   i);  
  }  
   
  **********************************************************  
  上面的代码明显是重定义,编译器会报重定义错误。  
  steedhorse(晨星),希望出来给小弟们解释解释。  
   
  为什么重定义报错而重复声明却没问题,我认为主要是语言标准制定过程中作出的决定。编译器既然能够成功的识别出重复定义   ,那么,把重复定义合并为一个并不难。也许制定标准的人有更充分的理由不允许语言这样做。  
   
  Top

17 楼nnlucky()回复于 2006-03-21 09:24:38 得分 0

void   fun1();//函数声明   不分配内存  
  int   x;//变量定义   分配内存  
  Top

18 楼steedhorse(晨星)回复于 2006-03-21 09:25:18 得分 0

To:ToHardToGetAnValidId()    
  你试过了么?哪个编译器说他是重复定义了?  
  注意这里是C版不是C++版。Top

19 楼bughole(虫洞)回复于 2006-03-21 09:39:11 得分 0

函数的声明是   void   fun1();     函数的定义是   void   fun1()   {}  
  变量的声明是   extern   int   i;     变量的定义是   int   i;  
   
  do   you   understand?Top

20 楼yzx1983(捕风捉影)回复于 2006-03-21 09:50:52 得分 0

lyskyly(浮生三笑)   (   )   信誉:100     2006-3-20   18:30:32     得分:   0      
     
     
         
  全局变量被视为一个临时性的定义,可声明多次,最后被链接器折叠起来,只有一个实体  
   
  -----------------  
  这个解释是比较好的  
   
  全局变量的处理和局部变量有所不同。全局中未赋初值的变量都被视作声明而非定义,例如  
  int   i;/*对于全局变量而言这些都只是声明一个符号*/  
  int   i;  
  int   i;  
  int   i;  
  int   i;  
  int   i   =   6;/*这里分配了空间*/  
  以上没有任何问题,但是如果有两个地方都赋了初始值就不行了,例如  
  int   i   =   6;  
  int   i   =   6;  
  这就会有重复定义的error。  
   
  对于函数中的变量,任何不带extern的声明都是定义,即局部变量立刻分配空间,所以  
  int   i;  
  int   i;  
  也会报错。Top

21 楼steedhorse(晨星)回复于 2006-03-21 10:27:14 得分 0

顶一下yzx1983(捕风捉影)    
  关于C语言(不是C++语言)中的“external   definition”,以下是C99标准的原文摘录:  
  6.9.2   External   object   definitions  
  Semantics  
  1   If   the   declaration   of   an   identifier   for   an   object   has   file   scope   and   an   initializer,   the   declaration   is   an   external   definition   for   the   identifier.  
  2   A   declaration   of   an   identifier   for   an   object   that   has   file   scope   without   an   initializer,   and   without   a   storage-class   specifier   or   with   the   storage-class   specifier   static,   constitutes   a   tentative   definition.   If   a   translation   unit   contains   one   or   more   tentative   definitions   for   an   identifier,   and   the   translation   unit   contains   no   external   definition   for   that   identifier,   then   the   behavior   is   exactly   as   if   the   translation   unit   contains   a   file   scope   declaration   of   that   identifier,   with   the   composite   type   as   of   the   end   of   the   translation   unit,   with   an   initializer   equal   to   0.Top

22 楼yleiou(单刀匹马)回复于 2006-03-21 10:43:22 得分 0

学习Top

23 楼ToHardToGetAnValidId()回复于 2006-03-21 10:50:26 得分 0

哦,抱歉,我用的是c++编译器,改用c编译器确实能通过。  
  谢谢喽Top

24 楼ToHardToGetAnValidId()回复于 2006-03-21 10:52:33 得分 0

标准一出,谁与争峰,呵呵Top

25 楼fierygnu(va_list)回复于 2006-03-21 11:07:20 得分 0

to   ToHardToGetAnValidId()  
  如果你用的是gcc,加上-fno-common选项试试:)Top

26 楼ToHardToGetAnValidId()回复于 2006-03-21 11:09:46 得分 0

我用的是vc6,把测试文件改成.c来做的。Top

27 楼gkexy(孤客行云)回复于 2006-03-21 13:19:14 得分 0

int   i=8;  
  main()  
  {  
              int   i=5;  
              printf("%d",i);                   /*访问局部变量i*/  
              printf("%d",填什么);         /*访问同名全局变量i*/  
  }  
  要怎么访问全局变量?注意不是C++       用::引用不行Top

28 楼linli2006(考研TOBEORNOTTOBE)回复于 2006-03-21 13:26:04 得分 0

函数是重载Top

29 楼linli2006(考研TOBEORNOTTOBE)回复于 2006-03-21 13:27:04 得分 0

搞错了  
  继续潜水。。。Top

30 楼winyin(无名飞非)回复于 2006-03-21 14:34:07 得分 0

时间都浪费在这种问题上...Top

31 楼kendejihxx(冰赐&龙龙)回复于 2006-03-21 21:13:58 得分 0

我晕。我都被你们说得头都是大的了。  
  谁出来给个标准的说法。也好让我学习学习。Top

32 楼nongdi(nongdi)回复于 2006-03-21 21:44:18 得分 0

灌下水:  
  为什么要把void   fun1();   void   fun1();放在主函数前或者有的放在.h头文件中?是因为c中函数要求返回值,默认为int型,而void   是无值函数,所以要先声明下,不然会出错的,好像还有指针函数也要声明。但其他函数就不用这样做了。Top

33 楼LoveCreatesBeauty(lovecreatesbeauty.googlepages.com)回复于 2006-03-21 23:45:25 得分 0

steedhorse(晨星)   (   )   信誉:145     2006-3-20   11:49:36     得分:   0      
   
  变量重复定义的例子如下,没问题的。  
   
  #include   <stdio.h>  
   
  int   i;  
  int   i;  
  int   i;  
  int   i;  
  int   i;  
  int   i   =   6;  
  extern   int   i;  
  extern   int   i;  
  extern   int   i;  
  extern   int   i;  
  extern   int   i;  
   
  int   main()   {  
  printf("%d\n",   i);  
  }  
   
       
     
    steedhorse(晨星)   (   )   信誉:145     2006-3-20   11:50:49     得分:   0      
         
  Sorry,打错了,“变量重复声明的例子如下”,汗一吧。。。。-_-  
   
       
     
  ---   ---   ---  
   
   
   
  Read   following   the   articles   for   the   reference.   And   try   to   make   yourself   clear   before   you   want   to   say   something   on   the   internet,   or   just   hide   your   opinion   to   avoid   confusing   yourself.  
   
  Keep   reading   comp.lang.c   frequently,   reread   the   C   textbook   periodically.   You   should   know   other   people   answered   the   same   question   years   ago,   though   it   is   not   just   for   you,   though   it   is   some   complex.  
   
   
   
  http://tinyurl.com/eqba7  
   
  From:     Jack   Klein   -   view   profile    
  Date:     Sat,   Sep   8   2001   1:14   pm      
  Groups:       comp.lang.c    
   
  <snip>  
   
  C   allows   you   to   have   as   many   tentative   definitions   of   external   objects    
  as   you   want.     If   at   the   end   of   the   file   there   have   been   no   complete    
  definitions   (with   an   initializer),   the   compiler   will   generate   its   own    
  complete   definition   with   the   default   initializer   of   0.     So   you   could    
  have:    
   
  int   i;    
  int   i;    
  int   i;    
  int   i;    
  int   i;    
  int   i;    
  int   i;    
  int   i;    
  int   i;    
  int   i   =   42;    
   
   
   
  How   does   ANSI   C   allow   redefinition   of   global   variants  
   
  http://spaces.msn.com/lovecreatesbeauty/blog/cns!F8F640E9473C03F4!138.entryTop

34 楼cyblueboy83(爱情白痴—电脑迷)回复于 2006-03-22 00:18:31 得分 0

变量也可能重复声明的,但不可以重复定义。  
  同样,函数也不可以重复定义的。Top

相关问题

  • 再问弱智问题:怎样声明全局变量、函数
  • 函数声明
  • 函数声明?
  • vc的变量声明是不是不一定非得在函数得开头?
  • [提问C++ Primer] 有关头文件中变量和函数的声明
  • C++ 多文件编译时,全局变量,函数如何声明
  • 我把一个函数里的变量改成声明为类里的public变量,为什么编译不过!
  • 问个问题?为什么声明类的话他会锁掉函数和变量,但是声明函数的时候却不锁啊?
  • API函数声明
  • 为什么在函数中声明大型变量会产生stack overflow(堆栈溢出)?

关键词

  • c++
  • 函数
  • 编译器
  • translation
  • 信誉
  • 语言
  • 变量
  • 声明
  • 定义
  • 重复定义

得分解答快速导航

  • 帖主:zhngxVPP

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo