为什么执行从main()开始? [英] Why does execution start at main()?
问题描述
我已经用C编程了大约一年了。这听起来很傻,但是我从来没有花时间质疑为什么C(或C ++或Java)程序只需要在main()处开始执行
。这是一个约定还是有一些更深层次的原因?
I have been programming in C for about a year now. It sounds silly,
but I never took the time to question why a C(or C++ or Java) program
execution begins only at the main(). Is it a convention or is there
some deeper underlying reason?
推荐答案
是****** @ hotmail.com (Beni)写道:
be******@hotmail.com (Beni) wrote:
我一直在C编程大约一年了。这听起来很愚蠢,但是我从来没有花时间质疑为什么C(或C ++或Java)程序只在main()处开始执行。
I have been programming in C for about a year now. It sounds silly,
but I never took the time to question why a C(or C++ or Java) program
execution begins only at the main().
>
那么......你还能从哪里开始呢?在退出()?
基本上,你必须在程序启动的地方有_some_约定
执行;选择标准化的启动功能名称与任何选择一样好。
Richard
So... where else would you have it start? At exit()?
Basically, you have to have _some_ convention on where a program starts
executing; picking a standardised startup function name is as good a
choice as any.
Richard
Beni写道:
我已经用C编程了大约一年了。这听起来很愚蠢,但我从来没有花时间质疑为什么C(或C ++或Java)程序只在main()开始执行。这是一个约定还是有更深层次的根本原因?
I have been programming in C for about a year now. It sounds silly,
but I never took the time to question why a C(or C++ or Java) program
execution begins only at the main(). Is it a convention or is there
some deeper underlying reason?
执行从main()开始因为印刷
该语言早期版本的文档中出现错误。
在文档打印,绑定和发送数千份
之前,没有检测到错误对于b $ b急切的用户来说,所以更换软件要比追逐所有这些错误的副本更加容易。
在这个错误延续之前,C程序开始执行
执行mean()。
-
Er ********* @ sun.com
In< e5**************************@posting.google.com> be******@hotmail.com (Beni)写道:
In <e5**************************@posting.google.com > be******@hotmail.com (Beni) writes:
我已经在C编程了大约一年了。这听起来很愚蠢,但我从来没有花时间质疑为什么C(或C ++或Java)程序只在main()开始执行。这是一个约定还是有更深层次的根本原因?
I have been programming in C for about a year now. It sounds silly,
but I never took the time to question why a C(or C++ or Java) program
execution begins only at the main(). Is it a convention or is there
some deeper underlying reason?
这是编写C启动模块的人之间的约定
编写C程序的人。尝试省略来自
C程序的main()函数,看看链接时会发生什么:
fangorn:〜/ tmp 108> cat test.c
void foo(){}
fangorn:〜/ tmp 109> gcc test.c
/usr/lib/crt1.o(.text+0x18):在函数`_start''中:
:对'main'的未定义引用
这表明自动链接到你的程序的模块/usr/lib/crt1.o是调用main(),但链接器不能找不到
的功能。当然,你可以欺骗大多数链接器,因为它们不是特别聪明的b / b $
fangorn:〜/ tmp 111> cat test.c
int main;
fangorn:〜/ tmp 112> gcc test.c
fangorn:〜/ tmp 113>
但运行生成的可执行文件是留给傻瓜或勇敢的
(或愚蠢的勇敢;-)
请注意,此约定仅适用于托管实现。
独立实现可能使用不同的约定(那里可能是
操作系统内核中没有主要功能,即使它是用C语言编写的。
Dan
-
Dan Pop
DESY Zeuthen,RZ集团
电子邮件: Da ***** @ ifh.de
这篇关于为什么执行从main()开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!