extern用法
先定义subf1.h和subf1.c文件,如下,
subf1.h
#ifndef subf1_h #define subf1_h #include <stdio.h> #endif /* subf1_h */ void singTheSong(int numberOfBottles);
subf1.c
#include "subf1.h" // 函数的递归调用 void singTheSong(int numberOfBottles){ if (numberOfBottles == 0) { printf("There are simple no more bottles of beer on the wall.\n"); }else { printf("%d bottles of beer on the wall. %d of bottles of beer.\n",numberOfBottles,numberOfBottles); int oneFewer = numberOfBottles - 1; printf("Take one down,pass it around,%d bottles of beer on the wall.\n",oneFewer); singTheSong(oneFewer); // 函数的递归调用 printf("put a bottle in the recycling,%d empty bottles in the bin.\n",numberOfBottles); } }
在main.c文件中,
#include <stdio.h> #include <stdlib.h> extern void singTheSong(int numberOfBottles); void showCookTimeForTurkey(int pounds){ int factor = 15; printf("the local static factor is %d\n",factor); int necessaryTime = factor + factor * pounds; printf("cook for %d min\n",necessaryTime); } int main(int argc,const char * argv[]){ printf("Hello, World!\n"); showCookTimeForTurkey(12); singTheSong(2); return 0; }
虽然在main.c文件只是用
extern void singTheSong(int numberOfBottles);
声明了函数,标志着这个函数是一个外部函数,是在其他文件中声明和定义的。在main.c文件中照样可以调用。
而一般的做法就是用include引入声明singTheSong函数的头文件,
#include "subf1.h"
比如像下面这样,
#include <stdio.h> #include <stdlib.h> #include "subf1.h" //extern void singTheSong(int numberOfBottles); void showCookTimeForTurkey(int pounds){ int factor = 15; printf("the local static factor is %d\n",factor); int necessaryTime = factor + factor * pounds; printf("cook for %d min\n",necessaryTime); } int main(int argc,const char * argv[]){ printf("Hello, World!\n"); showCookTimeForTurkey(12); singTheSong(2); return 0; }
使用extern和包含头文件来引用函数有什么区别呢?
extern的引用方式比包含头文件要简洁得多!extern的使用方法是直接了当的,想引用哪个函数就用extern声明哪个函数。
在上面说到的subf1.h和subf1.c文件修改如下,
subf1.h
#ifndef subf1_h //if not define #define subf1_h #include <stdio.h> int weight = 14; //在头文件中定义全局变量,一般不要这么做,虽然放在了if not define中。在多个源文件中引用该头文件会有问题,导致全局变量重复定义。 #endif /* subf1_h */ void singTheSong(int numberOfBottles); void printHeight();
subf1.c
#include "subf1.h" int height = 13; // 函数的递归调用 void singTheSong(int numberOfBottles){ if (numberOfBottles == 0) { printf("There are simple no more bottles of beer on the wall.\n"); }else { printf("%d bottles of beer on the wall. %d of bottles of beer.\n",numberOfBottles,numberOfBottles); int oneFewer = numberOfBottles - 1; printf("Take one down,pass it around,%d bottles of beer on the wall.\n",oneFewer); singTheSong(oneFewer); // 函数的递归调用 printf("put a bottle in the recycling,%d empty bottles in the bin.\n",numberOfBottles); } } void printHeight(){ printf("the height is %d\n",height); }
在main.c中,
#include <stdio.h> #include <stdlib.h> extern int height; extern int weight; extern void singTheSong(int numberOfBottles); extern void printHeight(); void showCookTimeForTurkey(int pounds){ int factor = 15; printf("the local static factor is %d\n",factor); int necessaryTime = factor + factor * pounds; printf("cook for %d min\n",necessaryTime); } int main(int argc,const char * argv[]){ printf("Hello, World!\n"); showCookTimeForTurkey(12); singTheSong(2); printHeight(); printf("the extern height is %d\n",height); printf("the extern weight is %d\n",weight); return 0; }
使用extern只是声明了这些函数和变量,并没有定义,也就是说没有分配内存空间,
extern int height; extern int weight; extern void singTheSong(int numberOfBottles); extern void printHeight();
输出打印结果,
Hello, World!
the local static factor is 15
cook for 195 min
2 bottles of beer on the wall. 2 of bottles of beer.
Take one down,pass it around,1 bottles of beer on the wall.
1 bottles of beer on the wall. 1 of bottles of beer.
Take one down,pass it around,0 bottles of beer on the wall.
There are simple no more bottles of beer on the wall.
put a bottle in the recycling,1 empty bottles in the bin.
put a bottle in the recycling,2 empty bottles in the bin.
the height is 13
the extern height is 13
the extern weight is 14
Program ended with exit code: 0
比如下面这样也是可以的,extern声明放在了main函数中,
#include <stdio.h> #include <stdlib.h> void showCookTimeForTurkey(int pounds){ int factor = 15; printf("the local static factor is %d\n",factor); int necessaryTime = factor + factor * pounds; printf("cook for %d min\n",necessaryTime); } int main(int argc,const char * argv[]){ extern int height; extern int weight; extern void singTheSong(int numberOfBottles); extern void printHeight(); printf("Hello, World!\n"); showCookTimeForTurkey(12); singTheSong(2); printHeight(); printf("the extern height is %d\n",height); printf("the extern weight is %d\n",weight); return 0; }
这样做就会导致在该文件作用域内的其他函数就不会访问到extern声明的变量和函数。
如下实例代码,
#include <stdio.h> #include <stdlib.h> extern int saleValue=1;//在xcode中显示warn int main(int argc,const char * argv[]){ printf("Hello, World!\n"); // extern int a = 1;//在xcode中不允许给extern声明的变量初始化 printf("the saleValue is %d\n",saleValue); return 0; }
当一个源程序由多个源文件组成时,C语言根据函数能否被其它源文件中的函数调用,将函数分为内部函数和外部函数。
如果在一个源文件中定义的函数,只能被本文件中的函数调用,而不能被同一程序其它文件中的函数调用,这种函数称为内部函数。
定义一个内部函数,只需在函数类型前再加一个“static”关键字即可,如下所示:
static 函数类型 函数名(函数参数表) {……}
关键字“static”,译成中文就是“静态的”,所以内部函数又称静态函数。但此处“static”的含义不是指存储方式,而是指对函数的作用域仅局限于本文件。
使用内部函数的好处是:不同的人编写不同的函数时,不用担心自己定义的函数,是否会与其它文件中的函数同名,因为同名也没有关系。
外部函数的定义:在定义函数时,如果没有加关键字“static”,或冠以关键字“extern”,表示此函数是外部函数:
[extern] 函数类型 函数名(函数参数表) {……}
调用外部函数时,需要对其进行说明:
[extern] 函数类型 函数名(参数类型表)[,函数名2(参数类型表2)……];
参考:http://blog.csdn.net/xinyuwuxian/article/details/8996014
http://www.cnblogs.com/sideandside/archive/2007/03/29/692559.html
http://lpy999.blog.163.com/blog/static/117372061201182051413310/
========END========
原文:http://my.oschina.net/xinxingegeya/blog/513233