免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 4088 | 回复: 22
打印 上一主题 下一主题

全局变量问题 又搞不清了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-04-01 16:22 |只看该作者 |倒序浏览
本帖最后由 tianxiaogang12 于 2010-04-01 16:25 编辑

文件f1.h

#ifndef _F1_H
#define _F1_H

#include <stdio.h>
void setone();

#endif //_F1_H

文件f1.c
#include "f1.h"

int i;
int i;  //////// 为什么这不报重复定一个的错误呢?
void setone()
{
        printf("f1 i = %的\n", i);
}

文件mymain.c
#include "f1.h"
int i = 100;

int main()
{
        printf("mymain i = %d\n", i);
        setone();
       
}

该程序可以编译过运行正常(gcc 4.1.2),但是我在文件f1.c明明重复顶一个了一个int i 啊
文件mymain.c也定义了一个int i
按理这三个i都要分配内存,然后报错
有人说这两个int i 是声明,但这明明是定义 不知道我学的课本是不是没有教对

论坛徽章:
0
2 [报告]
发表于 2010-04-01 16:46 |只看该作者
你这两个定义,没有连接在一起吧?并不是你包含了f1.h,就会自动给你连接f1.c的。

论坛徽章:
0
3 [报告]
发表于 2010-04-01 16:47 |只看该作者
你gcc f1.c mymain.c -o out 试试看

论坛徽章:
0
4 [报告]
发表于 2010-04-01 17:46 |只看该作者
是不是当声明来看了,而不是定义

论坛徽章:
0
5 [报告]
发表于 2010-04-01 18:32 |只看该作者
你这两个定义,没有连接在一起吧?并不是你包含了f1.h,就会自动给你连接f1.c的。
naihe2010 发表于 2010-04-01 16:46



    编译器在编译f1.c时变量i会不会分配空间?

论坛徽章:
0
6 [报告]
发表于 2010-04-01 20:20 |只看该作者
很奇怪会有人这样写header file
更奇怪有人会把 int i;写两次
我知道问题的答案
但我真的不想说

论坛徽章:
5
狮子座
日期:2013-08-20 10:12:24午马
日期:2013-11-23 18:04:102015年辞旧岁徽章
日期:2015-03-03 16:54:152015亚冠之德黑兰石油
日期:2015-06-29 18:11:1115-16赛季CBA联赛之新疆
日期:2024-02-21 10:00:53
7 [报告]
发表于 2010-04-01 20:47 |只看该作者
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.


这样?

论坛徽章:
0
8 [报告]
发表于 2010-04-01 21:38 |只看该作者
很奇怪会有人这样写header file
更奇怪有人会把 int i;写两次
我知道问题的答案
但我真的不想说
KBTiller 发表于 2010-04-01 20:20



    头文件这么写有什么不对? 把 int i;写两次是为了测试一下全局变量,更好的理解现在就是不理解
    麻烦楼上的讲解一下 因为哥很菜

论坛徽章:
0
9 [报告]
发表于 2010-04-01 22:19 |只看该作者
本帖最后由 KBTiller 于 2010-04-01 22:33 编辑
头文件这么写有什么不对? 把 int i;写两次是为了测试一下全局变量,更好的理解现在就是不理解
...
tianxiaogang12 发表于 2010-04-01 21:38



  
  1. #inc\
  2. lude <\
  3. std\
  4. io.h>
  5. in\
  6. t ma\
  7. in( v\
  8. oid ){p\
  9. ri\
  10. ntf          ("He"
  11.     "llo"
  12. " Wo"
  13. "rld\n"
  14. ); re\
  15. turn
  16. 0;}
复制代码


Hello world 这样写有何不可?

如果有人问我为什么可以这样写HelloWorld
我也不会回答的
代码缺少精神与灵魂
即使语法上没有错误
也不过是行尸走肉而已

论坛徽章:
0
10 [报告]
发表于 2010-04-01 22:31 |只看该作者
我愿意误入歧途, 我问为什么不报错? 知道就说,不知道就去打酱油
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP