C language main function parameter and its return value detailed parsing _c language

Source: Internet
Author: User
Tags function prototype

function of the 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 that you just compiled on the command line, and enter "echo%errorlevel%", and return to see that the program returns a value of 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.

----------------------Split Line--------------------

The return value is a common way to view the operating system
But it can be shown by the program itself.
The program code is as follows (only the contents of standard C are used)
#include
#include
int code;
void My_exit (void)
{
printf ("Retrun value is%d\n", code);
}
int main (void)
{
Atexit (My_exit);
return code = 0; or other return value because the result of the code = 0 expression is the value of the code and then the value is passed back, so it's the same effect as returning 0. It's just a little more than a function of assigning code to a good output.
}
Don't think there's any difference between this and direct print 0.
Look at the atexit function. It's also helpful for you to learn about main.

----------------------Split Line--------------------

Just for example, recursion calls the example of main, without practical meaning.
int num=3;//global variable
int main ()
{
num--;
int i=10;
if (num>=0)
int I=main ();//Recursive call Main ()


return 0;
}
When you set breakpoints, you can see changes in I values

In a program that does not recursively invoke main (), exit (0) and return 0 are the same, but in a recursive call to main program, exit (0) can end the program, and return 0; Is the end of the current main function

----------------------Split Line--------------------
Part Two: Analysis of the return value of main function in C language

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.

"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 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.

--------------------------------------------------
In Summary:
void main main function has no return value
main defaults to int, that is, int main (), which returns an integer.
Note that the new standard does not allow the default return value, that is, the int cannot be saved, and the corresponding main function no longer supports the void return value, so for the program to be well ported, it is strongly recommended that:
int main ()
{
return 0;
}
--------------------------------------------------------------------------

Part III: About main (int argc, char *argv[])

The following extracts a short paragraph:

ARGC is the total number of parameters on the command line  
   argv[] is a argc parameter, where the No. 0 parameter is the full name of the program, and subsequent parameters  
   command line followed by the user input parameters, For example: 
   int   Main (int   argc,   char*   argv[])  
   { 
   int   i; 
   for   (i   =& nbsp;  0;   i<argc;   i++)  
   cout<<argv[i]<<endl;  
   cin>>i; 
   return   0; 
  } 
   typing  
   f:\mydocu~1\tempcode\d1\debug\d1 when executing. exe   aaaa   bbb   ccc   ddd 
   output is as follows: 
    F:\MYDOCU~1\TEMPCODE\D1\DEBUG\D1. exe 
   aaaa 

   ccc 
   ddd 

Related Article

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.