Mistake one: void Main ()

Source: Internet
Author: User
Tags definition exit command line function prototype requires linux

Many people even some books on the market use void main (), in fact, this is wrong. void Main () has never been defined in C + +. C + + Father Bjarne Stroustrup explicitly in the FAQ on his home page that the definition void main () {/* ... */} is not and never has been C + +, nor h As it even been c. (void main () never existed in C + + or C). Let me say a few words about the definition of the main function in the C and C + + standards.

1. C

In C89, main () is acceptable. Brian W. Kernighan and Dennis M. Ritchie's classic masterpiece The C programming Language 2e (the second edition of the C programming language) uses main (). 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 languages-c 5.1.2.2.1 program startup)

Of course, we can also make a little 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 be sure to conform to the naming rules for variables.

If you do not need to get the argument from the command line, use int main (void), or 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 caller of the program (such as the operating system).

If the main function does not write the return statement at the end, C99 requires the compiler to automatically add return 0 in the generated target file (such as EXE), indicating that the program exits normally. However, I suggest you better add the return statement at the end of the main function, although this is not necessary, but it is a good habit. Note that VC6 will not add return 0 to the target file, presumably because VC6 is a 98 product, so this feature is not supported. Now understand why I suggest you better add the return statement! However, gcc3.2 (the C compiler under Linux) adds return 0 to the generated target file;.

2. C + +

The following two types of main functions are defined in c++98:

int main ()

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

(reference: ISO/IEC 14882 (1998-9-01) programming languages-c++ 3.6 Start and termination)

int main () is equivalent to int main (void) in C99, and int main (int argc, char *argv []) is also defined in C99. Similarly, the return value type of the main function must also be an int. If the end of the main function does not write a returns statement, C++98 requires the compiler to automatically add the returned 0 to the generated target file. Similarly, VC6 does not support this feature, but g++3.2 (C on Linux) + + compiler) support.

3. About void Main

In C and C + +, a function prototype that does not receive any parameters and returns no information is "void foo (void);". Probably because of this, many people mistakenly assume that the main function can be defined as void main (void) if the program returns no value. But this is wrong! The return value of the main function should be defined as the int type, as specified in the C and C + + standards. Although in some compilers, void main can be compiled (such as VC6), not all compilers support void Main, because void main is never defined in the standard. g++3.2 if the return value of the main function is not of type int. Translated. And gcc3.2 will issue a warning. So if you want your program to have good portability, make sure you use int main.

4. Function of return value

The return value of the main function is used to describe the exit state of the program. If 0 is returned, the program exits normally, and the meaning of the other numbers is determined by the system. Typically, the return of a non-0 represents a program exception exit. Below we do a small experiment in the WinXP environment. First compile the following program:

int main (void)

{

return 0;

}

Then open the "command prompt" in the attachment, run the executable file that you just compiled on the command line, and enter "Echo%errorlevel%", and you can see that the return value of the program is 0. Suppose the file you just compiled is a.exe, if you enter "a && Dir, lists the folders and files in the current directory. However, if you change to "return-1", or another value other than 0, and then recompile and enter "a && dir", then Dir will not execute. Because the meaning of && is: If the previous program in && exits normally, continue to execute the program after &&, otherwise it will not execute. In other words, using the return value of the program, we can control whether or not to execute the next program. This is the benefit of int main. If you're interested, you can also change the return value type of the main function to a non-int type (such as float), recompile and execute "a && dir" to see what happens, and think about why that happened. By the way, if you enter a | | Dir, it means that dir is executed if a is exited unexpectedly.

5. What about int main (int argc, char *argv[], char *envp[])?

This is certainly not the standard C/s definition of the thing! Char *envp[] is provided by some compilers

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.