Can the main function be recursively called __ function

Source: Internet
Author: User
Tags function prototype
In C and C + + is different, we know that C language is more relaxed, flexible, and C + + goal is to change the C language more relaxed characteristics, the implementation of a more rigorous approach, including strict type checking and so on. In fact, in the C language, the main function can be called recursively, while in the C + + standard, the main function cannot be called recursively.
recursive invocation in C: (Reference: Http://blog.csdn.net/songkexin/article/details/1842654#reply)
#include <stdio.h>
int main ()
{
int C;
if ((C=getchar ())!= EOF)//EOF with Ctrl+z (win)
Ctrl+d (Linux)
{
Main ();
}
printf ("%c", c);
return 0;
}

C + + standard does not allow, does not mean that the following main function recursive call C + + program, as long as your compiler support on the line.

I experimented on the Linux g++ compiler, and I could make recursive calls. (reference: http://blog.csdn.net/jingdoit/article/details/6788058) int main ()
{
static int a = 5;
cout<<a<<endl;
if (--a> 0)
Main ();
return 0;
}

Then the experiment was done under Windows, and it was also possible.

In addition, the VC default installation success, does not have the ability to compile from the command line to create a VC program this feature. We have to do the following 3-step work:
1 Open a DOS command Prompt window.
2 in the C-Packing directory input:
CD Program Files\Microsoft Visual Studio\vc98\bin
Enter into the following directory:
C:\Program Files\Microsoft Visual Studio\vc98\bin>
3 then enter VCVARS32 and return, and you can see the following information:
C:\Program Files\Microsoft Visual Studio\vc98\bin>vcvars32
Setting environment for using Microsoft Visual C + + tools.
As shown in Figure 1: (Reference: http://szy-891023.blog.163.com/blog/static/37074859201131102322967/)

This allows you to use the VC compiler cl.exe at the command line.

Here is an introduction to the main function of the article, very good, turn around and share with you (reference: http://www.byvoid.com/blog/void-main/)

"void Main ()"?

NOIP2007 is "void Main ()" On the quiz paper?

If I remember correctly, the C + + group should be on the test paper "void Main ()." However, I have read the article of the father of C + + Bjarne Stroustrup, I seriously doubt it.

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 has 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 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.6Start 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 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) 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.

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 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, and 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, 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. Then int main (int argc, char *argv[], char *envp[]).

This, of course, is not something defined in standard C/A. Char *envp[] is an extension provided by some compilers to obtain the environment variables of the system. Because it is not a standard, so not all compilers are supported, and therefore poor portability, not recommended.

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.