云服务器免费试用

FUNCDN(funcdn客服)

服务器知识 0 1524

本文目录:

  • 1、什么时候说明函数?

什么时候说明函数?

只在当前源文件中使用的函数应该说明为内部函数(static),内部函数应该在当前源文件中说明和定义。对于可在当前源文件以外使用的函数,应该在一个头文件中说明,要使用这些函数的源文件要包含这个头文件。例如,如果函数stat_func()只在源文件stat.c中使用,应该这样说明:

/* stat.c */

# include atdio.h

atatic int atat_func(int,int); /* atatic declaration of atat-funcO */

void main (void);

viod main (void)

{

......

rc=stat_func(1,2);

......

}

/* definition (body) of stat-funcO */

static int stat-funcdnt argl,int arg2)

{

return rc;

}

在上例中,函数stat_func()只在源文件stat.c中使用,因此它的原型(或说明)在源文件stat.c以外是不可见的,为了避免与其它源文件中可能出现的同名函数发生冲突,应该将其说明为内部函数。

在下例中,函数glob_func()在源文件global.c中定义和使用,并且还要在源文件extern,c中使用,因此应该在一个头文件(本例中为proto.h)中说明,而源文件global.c和extern.c

中都应包含这个头文件。

File: proto.h

/* proto.h */

int glob_func(int,int); /* declaration of the glob-funcO function * /

File: global. c

/* global. c */

# include stdio.h

# include "proto. h" /*include this file for the declaration of

glob_func() */

viod main(void);

viod main (void)

{

rc_glob_func(l,2);

}

/* deHnition (body) of the glob-funcO function */

int glob_func(int argl,int arg2)

{

return rc;

}

File extern. c

/* extin.c */

# include atdio.h

# include "proto. h" /*include thia file for the declaration of

glob_func() */

void ext_func(void);

void ext_func(void)

{

/* call glob_func(), which ia deHncd in the global, c source file * /

rc=glob_func(10,20);

}

在上例中,在头文件proto.h中说明了函数glob_func(),因此,只要任意一个源文件包含了该头文件,该源文件就包含了对函数glob_func()的说明,这样编译程序就能检查在该源文件中glob_func()函数的参数和返回值是否符合要求。请注意,包含头文件的语句总是出现在源文件中第一条说明函数的语句之前。

【FUNCDN】的内容来源于互联网,如引用不当,请联系我们修改。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942@qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: FUNCDN(funcdn客服)
本文地址: https://solustack.com/25949.html

相关推荐:

网友留言:

我要评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。