为什么在main()之后有函数定义? [英] Why there are function definitions after the main()?

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

问题描述

我认为由最权威的程序员之一(Richard Stallman)编写的最著名的操作系统(linux)之一,其中最常用的系统功能(ls)可能是编写得很好的代码的示例.

I assumed that one of the most used system functions (ls) of one of the most famous OSs (linux) written by one of the most authoritative programmers (Richard Stallman) could be an example of really well written code.

因此,由于它是开源的,所以我决定看一下代码(请参见例如

So, being it open source, I decided to have a look at the code (see e.g. here). There I found several functions defined after the main(), hence after their call, which I expected to be quite uncommon.

任何有经验的C程序员都会对此发表评论吗?

Will any experienced C programmer comment on this?

推荐答案

Stallman在这里所做的工作绝对没有错.

There's absolutely nothing wrong with what Stallman did here.

C语言允许转发声明 之后将定义.

这具有许多优点,不应被视为不良行为,而应被视为非常良好的行为.

This has many advantages, and should not be considered as bad behavior, but rather very good behavior.

优势(并非详尽无遗):
-让程序员快速了解C代码公开的API,而不必查看所有代码.
-允许使用头文件,您可以在其中声明一个在编译过程中稍后会定义的函数.

Advantages (not exhaustive):
- give the programmer a vision of the API exposed by the C code in a quick look, without having to look at all the code
- permits the use of header files, where you declare a function that will be defined later on in the compilation process. So that you don't have to define your function every time you use it..

对于这种ls实现,他只是预先声明了将在main()中使用的功能,但是如果仔细看,主要功能是第一个出现的功能. 这很可能是出于可读性的考虑,因此您不必一直向下滚动即可到达程序的入口点.

In the case of this ls implementation, he simply pre-declared the functions that he'll use in the main(), but if you look carefully, the main function is the first one to appear. This is most probably for the sake of readability, so that you don't have to scroll all the way down to reach the entry point of the program.

请注意,此处的词汇很重要:
-函数声明的意思是:告诉编译器在代码中的某个地方将定义一个具有相同名称的函数.
-功能定义:实际的功能实现

Note that the vocabulary is important here:
- function declaration means: just tells the compiler that, somewhere in your code, a function with the same name will be defined.
- function definition : the actual function implementation

int my_function( char *text); // function declaration, no implementation
int main( int argc, char **argv)
{
   return my_function(argv[0]); // use of the declared function
}

// actual function definition / implementation
int my_function( char *text )
{
   printf("%s\n", text);
}

编辑:仔细查看代码后,您可以看到Stallman并未向前声明其所有功能.他还有一种很奇怪的定义功能的方式.我将其归因于代码的过时,该代码可追溯到1985年,当时C编译器的定义不如今天. 在声明或定义之前,必须允许 使用这种功能.

Edit: after looking more carefully to the code, you can see that Stallman didn't forward-declare all his functions. He has also a rather strange manner of defining functions. I attribute this to the oldness of the code, which is dated of 1985, when the C compiler was not as well defined as today. It must have permitted this kind of function usage before being declared or defined.

最近但并非最不重要的是,>源代码的最新版本可在以下位置找到:

Last but not least, the recent version of ls source code can be found here: http://coreutils.sourcearchive.com/documentation/7.4/ls_8c-source.html ,
with much more C99-compliant coding than the '85 (Back-to-the-Future) version.

这篇关于为什么在main()之后有函数定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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