为什么要使用函数原型? [英] Why use function prototypes?

查看:149
本文介绍了为什么要使用函数原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在C语言中使用函数原型?似乎有点多余,因为我们已经在定义中声明了函数名称,参数类型和返回类型.在定义函数或将其用于优化之前是否必须先声明原型?

Why use function prototypes in C? It seems sort of redundant because we already declare the function name, argument types, and return type in the definition. Do the prototypes have to be declared before the function is defined or used for the optimizations?

推荐答案

通常来说,您不需要显式声明函数,因为定义它们也可以声明它们.在两种情况下,您需要:

Generally speaking, you don't need to explicitly declare functions because defining them also declares them. Here are two situations where you would need to:

  1. 该功能的定义在外部模块中.

  1. The definition of the function is in an external module.

例如,如果函数是在foo.c中定义的,但是您想从bar.c调用它,则需要在bar.c或它包含的文件中声明该函数(通常为foo.h ).

For example, if the function is defined in foo.c, but you want to call it from bar.c, you will need to declare the function in bar.c or a file included by it (typically, foo.h).

该函数的定义在调用它之后出现.

The definition of the function comes after a call to it.

例如,如果您有两个相互调用的函数,则需要在定义第一个函数之前声明第二个函数.

For example, if you have two functions that call each other, you will need to declare the second one before the definition of the first one.

这篇关于为什么要使用函数原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆