extern、static关键字问题,网上搜到的,试了试,结果是错的!

hqsh9 2009-05-30 12:49:46
google里搜了几篇相关文章,文章里都提到:

static全局变量——作用域仅该文件;static函数——仅限于该文件使用

但我的试验程序却不是这么回事:

/*test.h*/
static int i = 1;
int j = 2;
static int fun()
{
return 123;
}
extern int fun2() /* extern 可以省略 */
{
return 456;
}

/*test.c*/
#include "test.h"
int main()
{

extern int i,j; /* 这一行可以省略 */
extern int fun(), fun2(); /* 这一行可以省略 */
printf("%d\t%d\t%d\t%d\n",i,j,fun(),fun2());
}

结果输出:1 2 123 456

另外,凡是用extern关键字的地方,都可以被省略?
...全文
118 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingyin55 2009-05-30
  • 打赏
  • 举报
回复
在源文件中使用#include "test.h"
编译时期就会把"test.h" 中的内容在c文件中展开,
和你写在同一个文件没有什么差别。
光宇广贞 2009-05-30
  • 打赏
  • 举报
回复
同学……编译的时候,同名的H文件里的内容会被包进同名的C文件中,实际上是一个文件……你一个文件里面extern个什么。
风老二 2009-05-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 hqsh9 的回复:]
8楼的gcc命令好像不对,我用gcc -o test test.h test.c,报错:gcc: compilation of header file requested

/*test.h*/
static int i = 1;
int j = 2;
static int fun()
{
return 123;
}
extern int fun2()
{
return 456;
}

/*test.c*/
int main()
{
extern int i,j;
extern int fun(), fun2();
printf("%d\t%d\t%d\t%d\n",i,j,fun(),fun2());
}
[/Quote]

汗,我是举个例子,我也不熟,实际编译是要先单个编,然后再合成,具体命令你得查一下
ljl2200062 2009-05-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lylm 的回复:]
举个例子说明一下,更明白些:

/*a.cpp*/
int a=10;
int b=20;

/*b.cpp*/
#include <stdio.h>
extern a,b;
void main()
{
printf("a=%d,b=%d\n",a,b);
}
VC6运行输出:a=10,b=20

int a=10;改为static后
报链接错误,变量a不能解析
[/Quote]
受用 学习
hqsh9 2009-05-30
  • 打赏
  • 举报
回复
原来不能用.h,改成.c就行了。
hqsh9 2009-05-30
  • 打赏
  • 举报
回复
8楼的gcc命令好像不对,我用gcc -o test test.h test.c,报错:gcc: compilation of header file requested

/*test.h*/
static int i = 1;
int j = 2;
static int fun()
{
return 123;
}
extern int fun2()
{
return 456;
}

/*test.c*/
int main()
{
extern int i,j;
extern int fun(), fun2();
printf("%d\t%d\t%d\t%d\n",i,j,fun(),fun2());
}
hqsh9 2009-05-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 everest79 的回复:]
然后连编
gcc /o test.c test2.c test.exe
这时extern才有作用或存在的价值
[/Quote]

哟,学习了!

看来问题出在这儿。

连编,我一直以为include才能连编,以前一直用TC和VC的,TC、VC里也有这种连编,怎么弄?

lylm 2009-05-30
  • 打赏
  • 举报
回复
举个例子说明一下,更明白些:

/*a.cpp*/
int a=10;
int b=20;

/*b.cpp*/
#include <stdio.h>
extern a,b;
void main()
{
printf("a=%d,b=%d\n",a,b);
}
VC6运行输出:a=10,b=20

int a=10;改为static后
报链接错误,变量a不能解析
风老二 2009-05-30
  • 打赏
  • 举报
回复
你加了#include "test2.h"之后,编译时会先把test2.h的内容合并到test.c中

例如说test2.h中有三行

1.void fun(){
2.cout<<"abcdefg"<<endl;
3.}


test.c中也有三行

1.//...
2.#include "test2.h"
3.func();

当你编译test.c时,经过处理,test.c的内容就成了这样

1.//...
2.void fun(){
3.cout<<"abcdefg"<<endl;
4.}
5.fun();

include就是包含的意思,你按头文件的方式加入,编译器就是把他跟test.c看成是一个文件,所以不需要额外的文件声明,但你不添加,当然会少语句

再看个例子
test2.c

1.void fun(){
2.cout<<"abcdefg"<<endl;
3.}


test.c

1.//...
2.extern void func();
3.func();

然后连编
gcc /o test.c test2.c test.exe
这时extern才有作用或存在的价值
光宇广贞 2009-05-30
  • 打赏
  • 举报
回复
你只要include 了,不管是什么名的文件,该头文件中定义的都将视为与c文件为同一文件中的内容。
hqsh9 2009-05-30
  • 打赏
  • 举报
回复
我再把test.h文件名改为test2.h
然后在test.c里面#include "test2.h",其他不变,结果还是一样。
老师,不信你们试试看。
hqsh9 2009-05-30
  • 打赏
  • 举报
回复
同名的.h算同一个文件,怎么都这么说?
我把 #include "test.h" 给去掉的话,就报i,j,fun,fun2未定义。
tianya0609 2009-05-30
  • 打赏
  • 举报
回复
mark
风老二 2009-05-30
  • 打赏
  • 举报
回复
文件作用域是指多文件编译时各个文件限定的作用域,例如a.cpp,b.cpp,这时两个文件的变量与过程需要extern
*.h是头文件,编译时是附加到cpp文件中的,其实应看作一个文件,所你试不出来

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧