Main Function Specification

Source: Internet
Author: User

I learned something about main from others, summarized my experiences, and finally posted the original content.

Experience>

In the C and C ++ specifications, the return values of main () are int, and void main () is only compiled by some compilers, in most strict compilers, compilation is not supported. Because we are used to using vc6, and vc6 is just one of the less rigorous compilers, void main () is the correct concept.

It is good to develop the correct programming habits, so it must be correct to use it :)

Int main (); // C ++

Int main (void); // C

Int main (INT argc, char * argv []); or Int main (INT argc, char ** argv );

 

The return value of a function is specific. Using the return value can effectively control whether or not subsequent programs are executed. Therefore, void, a return value without any meaning, is a great waste for main.

 

Add two additional points (original post content ):

1> aboutInt main (INT argc, char * argv [], char * envp [])

 

Of course this is not defined in the Standard C/C ++! Char * envp [] is an extension provided by some compilers to obtain system environment variables. Because it is not a standard, not all compilers support it. Therefore, it has poor portability and is not recommended.

 

2>Function of Return Value

 

The Return Value of the main function is used to indicate the exit status of the program. If 0 is returned, the program Exits normally. The meaning of other numbers returned is determined by the system. Generally, if the return value is non-zero, the program exits unexpectedly. Below weWINXPPerform a small experiment in the environment. First, compile the following program:

 

Int main (void)

{

Return 0;

}

 

Open the "command prompt" in the attachment, run the compiled executable file in the command line, and enter "Echo% Errorlevel %", Press enter to see that the return value of the program is 0. Assume that the compiled file is a.exe.A & dirTo list the folders and files in the current directory. However, if it is changed to "Return-1", or other non-0 values, enter "A & dir" after re-compilation, the Dir will not be executed. The meaning of & is: if the program before & Exits normally, continue to execute & subsequent programs; otherwise, do not execute. That is to say, using the return value of the program, we can control whether to execute the next program. This is the benefit of int main. If you are interested, you can also change the main function's return value type to a non-int type (such as float), re-compile and execute "A & dir" to see what will happen, think about why that happened. By the way, if you enterA | dirIf a exits abnormally, execute dir.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.