2022
我们一起努力

FUNCDN(funcdn客服)

本文目录:

  • 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】的内容来源于互联网,如引用不当,请联系我们修改。

赞(0)
文章名称:《FUNCDN(funcdn客服)》
文章链接:https://www.fzvps.com/27245.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

评论 抢沙发

评论前必须登录!