Void main (), one of the misunderstandings of C/C ++ ()

Source: Internet
Author: User

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

Exist 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

The C programming language 2e (second edition of C programming language) uses main (). However, in the latest c99

In the 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 another variable name (such as intval and charval), but it 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 caller (such as the operating system ).

 

If the return statement is not written at the end of the main function, c99 requires that the compiler automatically be In the generated target file.

(Such as EXE files) add return 0;, indicating that the program Exits normally. However, I suggest you add

Return Statement, although not necessary, but this is a good habit. Note that vc6 will not add return 0 to the target file;

This feature is not supported because vc6 is a 98-year product. Now I understand why I suggest you add

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; int main (INT argc, char * argv [])

The usage is also the same as that defined in c99. Similarly, the return value type of the main function must be Int. If the end of the main function is not written

Return Statement. 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) supports.

 

 

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 );". Possible

It is precisely because of this that many people mistakenly think that if the program does not need to return values, the main function can be defined as void main (void)

. 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. Although

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 G ++ 3.2, if the return value of the main function is not of the int type

Translation. 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. Other values are returned.

The meaning of a word is determined by the system. Generally, if the return value is non-zero, the program exits unexpectedly. 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, 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 & dir ", the folders and files under the current directory are listed. But if it is changed to "Return-1" or another non-0 value,

Enter "A & dir" after re-compilation, then dir will not be executed. Because the meaning of & is: if the program in front of & Exits normally,

Continue to execute the & subsequent programs. Otherwise, the programs will not be executed. That is to say, using the return value of the program, we can control whether or not to execute

A 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 class.

Type (such as float), re-compile and execute "A & dir" to see what will happen and think about why it will happen.

. By the way, if a | DIR is input, it indicates that if a exits abnormally, DIR is executed.

 

 

5. What about int 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 for obtaining

Take the environment variable of the system. Because it is not a standard, not all compilers support it. Therefore, it has poor portability and is not recommended.

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.