Main()函数纠错

来源:互联网 发布:软件开发项目保密协议 编辑:程序博客网 时间:2024/05/22 00:24

Main()函数纠错

----------------你使用对了吗?

在C/C++程序中,都必须有一个主函数(入口函数),这个函数就是main()函数,先看看他们的标准定义吧:

 (1) C语言中

C89:2种,当时还没有void类型

int main();//不带参数

main( );//可选,因为默认的返回类型就是int,所以可以省略。

int main( int argc, char *argv[]) ;//带参数

C99:只有以下两种定义方式是正确的:

int main( void ) 
int main( int argc, char *argv[] ) 

(参考资料:ISO/IEC9899:1999 (E) Programming languages C 5.1.2.2.1 Program startup
   
当然,我们也可以做一点小小的改动。例如:char *argv[]可以写成 char **argvargv argc 可以改成别的变量名(如 intval charval),不过一定要符合变量的命名规则。
   
如果不需要从命令行中获取参数,请用int main(void);否则请用int main( int argc, char *argv[] ) 
    main
函数的返回值类型必须是 int,这样返回值才能传递给程序的激活者(如操作系统)。 
   
如果 main函数的最后没有写 return语句的话,C99规定编译器要自动在生成的目标文件中(如 exe文件)加入return 0;,表示程序正常退出。不过,还是建议你最好在main函数的最后加上return语句,虽然没有这个必要,但这是一个好的习惯。注意,vc6不会在目标文件中加入return 0;,大概是因为 vc6 98年的产品,所以才不支持这个特性。现在明白我为什么建议你最好加上 return语句了吧

 (2) C++语言中

 C++98 中定义了如下两种main 函数的定义方式:
    int main( )
    int main( int argc, char *argv[] ) 
   
(参考资料:ISO/IEC 14882(1998-9-01)Programming languages C++ 3.6 Start and termination 
    int main( )
等同于 C99中的 int main( void )int main( int argc, char *argv[] )的用法也和 C99中定义的一样。同样,main函数的返回值类型也必须是int。如果main函数的末尾没写return语句,C++98规定编译器要自动在生成的目标文件中加入 return 0;。同样,vc6也不支持这个特性。 

(3) 关于 void main 
   
C C++中,不接收任何参数也不返回任何信息的函数原型为“void foo(void);”。可能正是因为这个,所以很多人都误认为如果不需要程序返回值时可以把 main函数定义成void main(void)。然而这是错误的!main函数的返回值应该定义为 int类型,C C++ 标准中都是这样规定的。虽然在一些编译器中,void main可以通过编译(如 vc6),但并非所有编译器都支持 void main,因为标准中从来没有定义过 void main

很多人甚至市面上的一些书籍,都使用了void main( ) ,其实这是错误的。C/C++中从来没有定义过void main( ) C++ 之父Bjarne Stroustrup 在他的主页上的FAQ 中明确地写着The definition void main( ) { /* ...*/ } is not and never has been C++, nor has it even been C.void main( )从来就不存在于C++或者C


   
总而言之:
    void main
主函数没有返回值,main默认为int型,即 int main()返回整数。注意,新标准不允许使用默认返回值,即int不能省,而且对应main函数不再支持void型返回值,因此为了使程序有很好的移植性,强烈建议使用:
    int main()
    { 
        return 0;  
    } 
   
返回值的作用:
    main
函数的返回值用于说明程序的退出状态。如果返回0,则代表程序正常退出;返回其它数字的含义则由系统决定。通常,返回非零代表程序异常退出。下面我们在winxp环境下做一个小实验。首先编译下面的程序:
    int main( void )
    {
        return 0;
    }

(4)关于int main(int argc,char *argv[],char *envp[])

这当然也不是标准 C/C++ 里面定义的东西!char *envp[]是某些编译器提供的扩展功能,用于获取系统的环境变量因为不是标准,所以并非所有编译器都支持,故而移植性差,不推荐使用。

 

C/C++程序的入口函数与Windows程序的入口函数是两个不同的概念。只不过有时他们长的很像。因为Windows程序并不是都是C/C++编写的,也没有要求必须是有C/C++编写的。

 

(5) Windows应用程序的入口函数 

Windows支持的两种应用程序:
  (1)  基于图形用户界面的应用程序,即GUI;
  (2)  基于控制台用户界面的应用程序,即CUI.
  Windows应用程序也必须有一个在应用程序启动运行的调用进入点函数(即入口函数),以供操作系统调用。该入口函数的格式和语法也是由操作系统预先定义好了的,Windows下可以使用的入口函数有如下四个:它们都是Windows提供的API。

 

int WINAPI WinMain(

HINSTANCEhinstance,    //程序本身的实例句柄

                 HINSTANCE hPrevInstance, //历史遗留,hPrevInstance=NULL; 

                LPSTR lpCmdLine,        //命令行字符串
                                  int nCmdShow);         //窗口显示模式

 

int WINAPIwWinMain(

                  HINSTANCE hinstance,

                 HINSTANCE hPrevInstance,

                 PWSTR pszCmdLine,

                 int nCmdShow);

 

int __cdecl main(

                int argc,

               char *argv[],

               char *envp[]);

 

int __cdecl wmain(

                 int argc,

                wchar *argv[],

                 wchar *envp[]);

 

先看看MSDN上的解释:

=====================================================================

main: Program Startup

Updated: November 2007

A special function called main is the starting point of executionfor all C and C++ programs. If you are writing code that adheres to the Unicodeprogramming model, you can use the wide-character version ofmain,wmain.

The main function is not predefined by the compiler; rather, itmust be supplied in the program text.

The declaration syntax for main is:

Intmain();

or, optionally:

Int main(int argc,char *argv[],char *envp[]);

Microsoft Specific

The declaration syntax for wmain is as follows:

Int wmain();

or, optionally:

Int wmain(int argc,wchar_t*argv[],wchar_t*envp[]);

You can also use _tmain, which is defined in TCHAR.h. _tmainwill resolve to main unless _UNICODE is defined, in which case_tmainwill resolve towmain.

END Microsoft Specific

The types for argc and argv are defined by the language. The names argc, argv, and envp are traditional, but are not required by thecompiler. See Argument Definitions for more information and for an example.

Alternately,the main and wmain functions can be declared as returning void (no return value). If you declaremain orwmainas returning void, you cannot return an exit code tothe parent process or operating system using a return statement; to return anexit code whenmain orwmain is declared as void,you must use the exit function.

WinMain Function


The WinMainfunction is the conventional name for the user-provided entry point for aMicrosoft Windows-based application.

The name WinMain is used by convention by manyprogramming frameworks. Depending on the programming framework, the call to theWinMain function can be preceded and followed by additional activitiesspecific to that framework.

Your WinMain should initialize the application,display its main window, and enter a message retrieval-and-dispatch loop thatis the top-level control structure for the remainder of the application'sexecution. Terminate the message loop when it receives a WM_QUITmessage. At that point, your WinMain should exit the application,returning the value passed in the WM_QUIT message's wParamparameter. If WM_QUIT was received as a result of callingPostQuitMessage, the value ofwParam is the value of thePostQuitMessagefunction'snExitCode parameter. For more information, see Creating aMessage Loop.

ANSI applications can use the lpCmdLineparameter of the WinMain function to access the command-line string,excluding the program name. Note thatlpCmdLine uses theLPSTRdata type instead of theLPTSTR data type. This means thatWinMaincannot be used by Unicode programs. TheGetCommandLineW function can beused to obtain the command line as a Unicode string. Some programmingframeworks might provide an alternative entry point that provides a Unicodecommand line. For example, the Microsoft Visual Studio C++ complier uses thenamewWinMain for the Unicode entry point.

疑问:

C++程序必须有main入口函数,而window程序也必须有入口函数(如WinMain),如果C++程序在window平台上运行,入口函数是怎么解决(转换)的呢?


转载请说明出处,谢谢。http://blog.csdn.net/cbnotes/article/details/8221578

原创粉丝点击