int main () or void main ()

Source: Internet
Author: User

According to the new C99 standard, even if the function itself does not have a return value defined, the compiler adds it to return to the firing program, running state. Many people even have some books on the market that use void main (), which is actually wrong. void Main () has never been defined in C + +. C + + 's father, Bjarne Stroustrup, explicitly wrote in the FAQ on his homepage that the definition void main () {/* ... */} is not and never have been C + +, nor Has it even been c. (void Main () is never present in C + + or C). Let me say separately the definition of the main function in the C and C + + standards.
"The C Programming Language (" C programming Language ") uses 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 you can not write, later modified version in order to be compatible with the previous code so that the default value of the return value is not explicitly marked as an int, that is, main () is equal to the int main (), instead of the equivalent of void main (). In C99, the standard requires the compiler to give at least a warning to the use of main ().
(1) in C language
In C89, main () is acceptable. The classic masterpiece of Brian W. Kernighan and Dennis M. Ritchie, the C programming Language 2e (second edition of the C programming language), is the 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 conventions of the variables.
If you do not need to get arguments from the command line, use int main (void), or 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's activator (such as the operating system).
If the return statement is not written at the end of the main function, C99 specifies that the compiler automatically adds return 0 to the generated target file (e.g. EXE file); , indicating that the program exited 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 include return 0 in the target file; , presumably because VC6 is a 98 product, so this feature is not supported. Now understand why I suggest you better add a return statement! However, gcc3.2 (C compiler under Linux) adds return 0 to the generated target file.
(2) in the C + + language
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 the use of 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 specifies that the compiler automatically adds return 0 to the generated target file. Similarly, VC6 does not support this feature, but g++3.2 (c + + compiler under Linux) is supported.
(3) about void main
In C and C + +, function prototypes that do not receive any parameters and return no information are "void foo (void);". Probably because of this, many people mistakenly think that if you do not need the program return value, you can define the main function as void main (void). However, this is wrong! The return value of the main function should be defined as type int, 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, since 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 will not compile at all. The gcc3.2 will issue a warning. So, if you want your program to have good portability, be sure to use int main.
Word:
void main main function has no return value, main defaults to int, which is int main (), which returns an integer. Note that the new standard does not allow the default return value, that is, int cannot be saved, and the corresponding main function no longer supports void return values, so in order for the program to be well-ported, it is highly recommended to use:
int main ()
{
return 0; /* Return value of the new standard main function This statement can be omitted */
}
The function of the return value:
The return value of the main function is used to describe the program's exit status. 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 delegate program exits unexpectedly. Below we do a small experiment under the WinXP environment. First compile the following program:
int main (void)
{
return 0;
}
Then open the attachment "command Prompt" in the command line to run the just compiled executable file, and then enter "echo%errorlevel%", return, you can see the return value of the program is 0. Assuming that the file you just compiled is a.exe, if you enter "a && dir", the folders and files in the current directory are listed. However, if you change to "return-1", or another value other than 0, the Dir will not execute if you recompile and enter "a && dir". Because && means: If the previous program in && exits normally, it will continue to execute && the subsequent program, otherwise it will not execute. That is, by using the return value of the program, we can control the execution of the next program. This is the benefit of int main. If you are 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", and see what happens, and think about why that happened. By the way, if you enter a | | Dir, then the dir is executed if a exits unexpectedly.

int main () or void 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.