C ++ misunderstanding 1

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 correct. Brian W. kernighan and Dennis M. Ritchie use main (), a classic masterpiece of the C programming language (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: vc6 will not add return 0 to the generated target file, probably because vc6 is a 98-year product, so this feature is not supported. 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; [Page] int main (INT argc, char * argv []) is also used as 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 think that if the program does not need to return any information, 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. 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 int main
Benefits. 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.

This story is from the programmer (www.bczhe.com! Link: http://www.bczhe.com/biancheng/c/cyuyan_468.html

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.