Mistake one: void Main () __c++

Source: Internet
Author: User
Tags function prototype

Many people even some books on the market use void main (), in fact, this is wrong. void Main () has never been defined in C + +. The parent of C + + 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.
"The C Programming Language (" C programming Language ") uses the main (). "---this is because the first version of the C language has only one type, that is int, no char, no long, no float, ... Since there is only one type, then it can not be written, and later the improved version for the compatibility of the previous code thus stipulates: The return value is not clearly marked, the default return value is int, which means that main () is equivalent to int main (), not equivalent to void main (). In C99, the standard requires that the compiler give at least one warning to the usage of main ().

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 active person of the program, such as the operating system.

If the end of the main function does not write a return statement, C99 requires the compiler to automatically add return 0 in the generated target file (such as EXE); , which indicates 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 does not add return 0 to the target file; , presumably because VC6 is a 98 product, so it doesn't support this feature. 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 the 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 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 under Linux) supports it.

3. About void Main

        in C and C + +, a function prototype that does not receive any parameters nor returns any information is "void foo (void);". Probably because of this, many people mistakenly assume that the main function can be defined as void main (void) without the need for the program to return a value. However, 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, it is not compiled at all. And gcc3.2 will issue a warning. So if you want your program to have good portability, make sure you use int main.

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.