Main function in C ++

Source: Internet
Author: User

Many even some books on the market use void main (), which is actually incorrect. Void main () has never been defined in C/C ++ (). The father of C ++ Bjarne stroustrup clearly states the definition void main (){/*... */} is not and never has been c ++, nor has it even been c. (void main () never exists in C ++ or C ). The following describes the definition of the main function in the C and C ++ standards respectively.
1. c 
In c89, main () is acceptable. Brian W. kernighan and Dennis M. Ritchie use main (), the classic masterpiece of the C programming language 2e (second edition of C programming language (). However, in the latest c99 standard, only the following two definitions are correct:
Int main (void)
Int main (INT argc, char * argv [])
(Reference: ISO/IEC 9899: 1999 (E) programming versions-C 5.1.2.2.1 program startup)
Of course, we can also make a small change. For example, char * argv [] can be written as char ** argv; argv and argc can be changed to other variable names (such as intval and charval), but they must comply with the variable naming rules.
If you do not need to obtain parameters from the command line, use int main (void); otherwise, use int main (INT argc, char * argv []).
The Return Value Type of the main function must be int, so that the return value can be passed to the program activator (such as the operating system ).
If the return statement is not written at the end of the main function, c99 requires the compiler to automatically add return 0 to the generated target file (such as the EXE file), indicating that the program Exits normally. However, I suggest you add the return statement at the end of the main function. Although this is not necessary, it is a good habit. Note that vc6 does not include return 0; in the target file. This feature is probably not supported because vc6 is a 98-year product. Now I understand why I suggest you add the return statement! However, gcc3.2 (C compiler in Linux) will add return 0 to the generated target file ;.

2. c ++ 
The following two main functions are defined in C ++ 98:
Int main ()
Int main (INT argc, char * argv [])
(Reference: ISO/IEC 14882 () programming ages-C ++ 3.6 start and termination)
Int main () is equivalent to int main (void) in c99. The usage of int main (INT argc, char * argv []) is the same as that defined in c99. Similarly, the return value type of the main function must be Int. If the return statement is not written at the end of the main function, C ++ 98 requires the compiler to automatically add return 0 to the generated target file ;. Similarly, vc6 does not support this feature, but G ++ 3.2 (c ++ compiler in Linux) does.

3. About void main 
In C and C ++, the function prototype that does not receive any parameters or return any information is "Void Foo (void );". This may be the reason why many people mistakenly believe that the main function can be defined as void main (void) If no program return value is required ). However, this is wrong! The Return Value of the main function should be defined as the int type, which is specified in the C and C ++ standards. In some compilers, void main can be compiled (such as vc6), but not all compilers support void main, because void main has never been defined in the standard. In G ++ 3.2, if the return value of the main function is not of the int type, it cannot be compiled. Gcc3.2 issues a warning. Therefore, if you want your program to be highly portable, use int main.

4. Functions of return values 
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. Otherwise, the program exits abnormally. Next we will do a small experiment in the WINXP 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, enter "Echo % errorlevel %", and press Enter, the Return Value of the program is 0. Assume that the compiled file is a.exe. If "A & dir" is entered, the folders and files in the current directory are listed. 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 a | DIR is input, it indicates that if a exits abnormally, DIR is executed.

5. What about intmain (intargc, char * argv [], char * envp?
This is certainly not defined in standard 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.

At this point, you should understand why the main function is defined as the int return type, and there is return 0 in the function body.
The following describes my understanding of the return application.
As long as the return value of a function is numeric, 0 (return 0) can be returned. In fact, there is no problem with how much you return. Generally, all functions made by C ++ require that a value be returned. When the function is executed normally, if 0 is returned, it indicates that the function is called correctly, and 0 is returned to the main function to notify the function that no error has occurred. If an error occurs in the function call or the function is not executed normally, 1 is returned to inform the main function to adopt a response policy. If you define a group of Status values (generally negative integers) in the header file where the class definition of a function is located ), then, the function can return different values to indicate the specific exceptions or errors of the function. This situation is generally used in scenarios where function independence is poor. Therefore, we generally do not encourage you to define the function return type as void. At least the return type should be int, and add the return 0. statement at the end of the function:
Int func (parameter list)
{
......
......
......
Return 0;
}
If a return statement is encountered in a function, the program returns the execution of the next statement that calls the function, that is, the execution of the function jumps out and returns to the original place for further execution. However, if you encounter a return statement in the main function, the entire program will stop and exit the execution of the program.
If you define a function with a return type, you can call it as follows:
Int func ()
{
Int value;
......
......
......
Return value;
}
Int main ()
{
Int intvalue;
Intvalue = func ();
......
......
Return 0;
}


The specific content after the return statement is analyzed as follows:
(1) In a function whose return type is Char, the return type should be a char value;
(2) In a function whose return type is int, if you want to stop the function call, it is best to set it to 0; otherwise, it depends on your purpose, as long as it is of the int type.
(3) In a function whose return type is a structure type, return should be an Instance Object of the structure.
In short, the return type defined by the function should be the value of the corresponding type after return in the function.

Related Article

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.