您的位置:首页 > 大数据 > 人工智能

有关main函数的前因后果

2007-07-14 10:48 176 查看
以下内容摘自原文:

You would have been taught how main() is main in C? But, believe me, main() is nothing more than a word. i.e. It can be anything like Start(), Begin(), EntryPoint() etc. Before we directly go for main's hack, we must learn its basics. Here we go -

What main() is generally known for?
Ø It's entry point of a C program.
Ø Program without main() isn't possible.
Ø main() executes first.
Ø It has two parameters. i.e. argc, argv

Are these points right? See these possibilities...
Ø A C program which doesn't have main().
Ø A program which contains main() but is never called.
Ø main() has three parameters i.e. argc, argv and environ or envp.

Every program contains an entry point which is the place from where the program starts its execution. Whenever we execute a program, it gets loaded into the memory (RAM). But, instead of starting the execution from main() OS passes the control to startup() function located in crt0.c OR in wincmdln.c (if console application). This function initializes the global and environment variables (OR Environment table) for the program. i.e. argc, argv, _osver, _winmajor, _winminor, _winver, environ. Startup routine passes the values of argc, argv and environ to main() and finally calls main().

/*
Author: Bindesh Kumar Singh
Date: March, 2007
*/
#include <windows.h>
#pragma comment(linker,"/ENTRY:EntryPoint") /* Entry point set to EntryPoint() */

void main() /* Never executes */
{
MessageBox(0,"inside main()","information",0);
}

void EntryPoint() /* Entry point */
{
MessageBox(0,"inside EntryPoint()","information",0);
}

main can be bypassed by using #pragma comments. Here EntryPoint() is set to be the entry point of program.

详细内容见:http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=11485&lngWId=3

下载:Download article
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: