什么是“原型"?在 C 程序中? [英] What are "prototypes" in a C program?

查看:50
本文介绍了什么是“原型"?在 C 程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来学习 C 的书解释了我无法正确理解的称为原型"的东西.在书中,以下示例代码解释了这些原型".这是什么意思?什么是原型"?

The book's I'm using to learn C explains something called "prototypes" which I couldn't understand properly. In the book, the following sample code explains these "prototypes". What does this mean here? What are the "prototypes"?

//* two_func.c -- a program using two functions in one file */
#include <stdio.h>
void butler(void);
int main(void)
{
    printf("I will summon the butler function.\n");
    butler();
    printf("Yes! bring me some tea and writable DVD's.\n");
    getchar();
    return 0;
}

void butler(void)   /*start of function definition*/

{
    printf("You rang,sir.\n");

}

请用简单的语言解释.

推荐答案

函数原型(也称为前向声明")声明函数而不立即提供主体".除了函数本身之外,您还要编写原型,以便告诉编译器您在别处定义的函数:

Function prototypes (also called "forward declarations") declare functions without providing the "body" right away. You write prototypes in addition to the functions themselves in order to tell the compiler about the function that you define elsewhere:

你的原型 void butler(void); 做了以下所有事情:

Your prototype void butler(void); does all of the following things:

  • 它告诉编译器 butler 函数存在,
  • 它告诉 butler 没有参数,并且
  • 它告诉 butler 没有返回任何东西.
  • It tells the compiler that function butler exists,
  • It tells that butler takes no parameters, and
  • It tells that butler does not return anything.

原型很有用,因为它们可以帮助您隐藏函数的实现细节.您将原型放在库的头文件中,并将实现放在 C 文件中.这让依赖于您的库的代码与您的代码分开编译 - 这是一件非常重要的事情.

Prototypes are useful because they help you hide implementation details of the function. You put your prototypes in a header file of your library, and place the implementation in a C file. This lets the code that depends on your library to be compiled separately from your code - a very important thing.

这篇关于什么是“原型"?在 C 程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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