有关main函数的前因后果

时间:2022-06-20 00:06:25

 

以下内容摘自原文:

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.
下载:Download article