C/C ++ misunderstanding 1: void main ()

Source: Internet
Author: User

C/C ++ misunderstanding 1: void main ()

Source: ant C/C ++ standard programming
Author: antigloss
Level: strongly recommended
Released on
Read 4796 Times [Font: Large

Medium
Small]

 

Many people even use some books on the market.Void main ()In fact, this is wrong. C/C ++
Has never been definedVoid main ().C ++Father
Bjarne stroustrupOn his homepage
FAQThe definition void main () {/*... */} is not and never has been c ++, nor has it even been c .(
Void main () never exists in C ++
Or C ). Let me talk about C separately.
In the C ++ standard
Function Definition.

 

1. c

 

In
C89Medium,Main ()Is acceptable.Brian W. kernighanAnd
Dennis M. RitchieThe C programming language 2e (C
The second edition of the programming language) uses main (). However, in the latest
C99In the standard, only the following two definitions are correct:

 

Int main (void)

Int main (INT argc, char * argv [])

 

(References: ISO/IEC 9899: 1999 (E) programming ages
-C 5.1.2.2.1 program startup)

 

Of course, we can also make a small change. Example: char * argv []
It can be written as char ** argv; argv
And argc can be changed to another variable name (such as intval
And charval), but 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.
In this way, the return value can be passed to the activator (such as the operating system) of the program ).

 

If the main
No return is written at the end of the function.
Statement, c99
Requires the compiler to automatically be In the generated target file (such as exe
File) add return 0;
, Indicating that the program Exits normally. However, I suggest you add return at the end of the main function.
Statement, although not necessary, but this is a good habit. Note,Vc6Return 0 will not be added to the target file;
Probably because vc6
It is a 98-year product, so this feature is not supported. Now I understand why I suggest you add return
Statement! However,Gcc3.2(Linux
The C compiler under) will add return 0 to the generated target file;
.

 

 

2. c ++

 

C ++ 98
The following two main functions are defined:

 

Int main ()

Int main (INT argc, char * argv [])

 

(References: ISO/IEC 14882 () programming ages
-C ++ 3.6 start and termination)

 

Int main ()
Equivalent to int main (void) in c99)
; Int main (INT argc, char * argv [])
And c99
. Similarly, main
The Return Value Type of the 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
This feature is not supported, but G ++ 3.2 (Linux
C ++ compiler.

 

 

3. About void main

 

In C
In 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 if the return value of the program is not required.
Main (void ). However, this is wrong! Main
The Return Value of the function should be defined as Int.
Type, C and C ++
This is the standard. Although in some compilers, void main
It can be compiled (such as vc6), but not all compilers support void main
BecauseThe standard has never defined void main..G ++ 3.2
If the return value of the main function is not int
Type.
. Gcc3.2
A warning is issued. So, if you want your program to have a goodPortabilityPlease 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. Below we
WINXPPerform a small experiment in the 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.
If you enter"A & dirTo list the folders and files in the current directory. But if it is changed to "Return-1", or other non-
0 value. After re-compilation, enter "A & dir". Then, the Dir
Not executed. Because &&
The meaning is: If & the previous program Exits normally, continue to execute &&
Otherwise, the program is not executed. 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
The Return Value Type of the function is changed to non-int.
Type (such as float), re-compile and execute "A & dir" to see what happens and think about why that happens. By the way, if you enter
A | dirIf
Run the Dir command to exit unexpectedly.
.

 

 

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

 

This is certainly not a standard C/C ++
Something defined in it! 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.

========================================================== ======================================

If you think that I am not authoritative enough, let C ++
FatherBjarne stroustrupLet me convince you!

FromC ++ style and technique FAQs
(Chinese Version)
Bjarne stroustrupTranslated by Ziyun

Http://www.research.att.com /~ BS/bs_faq2.html # void-mainq: Can I write "Void main?

A:Such definition

            void main() { /* ... */ }

 

It is neither C ++ nor C. (See iso c ++
Standard 3.6.1 [2]
Or Iso c Standard 5.1.2.2.1) A standard-compliant compiler implementation should accept

            int main() { /* ... */ }

And

            int main(int argc, char* argv[]) { /* ... */ }

 

The compiler can also provide more overloaded versions of main (), but all of them must return an int. This int is returned to the caller of your program. This is a "responsible" approach, "Nothing returns" is not great. If the caller of your program does not support "Return Value" for communication, this value will be automatically ignored -- but this does not allow void
Main () becomes a legal C ++ or C code. Even if your compiler supports this definition, it is best not to develop this habit-otherwise, you may be considered a little ignorant by other C/C ++.

 

In C ++, if you are too troublesome, you do not need to explicitly write a return statement. The compiler automatically returns 0. For example:

            #include<iostream>
 
            int main()
            {
                        std::cout << "This program returns the integer value 0\n";
            }

 

Trouble? No problem. Int main () is one letter less than void main (): O). In addition, please note that no matter whether it is ISO
Both C ++ and c99 do not allow you to omit the return type definition. That is to say, with c89 and arm C ++
The C ++] described in the annotated C ++ reference manual, which was co-authored by Ellis and Bjarne stroustrup in 1990, is different. Int Is not the default return value. So,

            #include<iostream>
 
            main() { /* ... */ }

An error occurs because the main () function lacks the return type.

 

========================================================== ======================================

 

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.