C language main function of the parameters and their return value detailed analysis

  • 2020-04-02 01:56:16
  • OfStack

The function of the return value

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. The meaning of returning other Numbers is determined by the system. Typically, returning a non-zero represents the exit of a program exception. Let's do a little experiment in winxp. First compile the following program:
Int main (void)
{
      Return 0;
}
Then open the "command prompt" in the attachment, run the compiled executable file in the command line, then type "echo%ERRORLEVEL%", press enter, you will see that the return value of the program is 0. Assuming that the file you just compiled is a.exe, if you type "a && dir," it lists the folders and files in the current directory. But if you change it to "return -1", or some other non-zero value, and recompile "a && dir", dir will not be executed. Because am&& means: if the preceding program exits normally, continue with the following program, or not. That is, with the return value of the program, we can control whether or not to execute the next program. That's the nice thing about int main. If you're interested, you can also change the return value type of the main function to a non-int (such as float), recompile and do "a && dir", and see what happens and why. By the way, if you type a || dir, that means dir is executed if a exits.

-- the dividing line ----

Return values from the operating system are common
However, it can also be displayed by the program itself
The code is as follows (using only standard C)
# include
# include
Int code;
Void my_exit (void)
{
    Printf ("retrun value is %d\n", code);
}
Int main (void)
{
      Atexit (my_exit);
      The return code = 0; // or some other return value because the result of the expression code equals 0 is the value of code and then you pass that value to it       The return   So return 0 is the same thing   There's just a side effect of giving code a good output
}
Don't think it's any different than just printing a 0 here
What does the atexit function do   It also helps you to learn main

-- the dividing line ----

Just as an example, the example of calling main recursively, doesn't really make sense
Int num = 3; // global variable
Int main ()
{
      Num -;
      Int I = 10;
      If (num > = 0)
      Int I = the main (); // recursively call main()


      Return 0;
}
After setting the breakpoint, debugging can see the change of I value

In non-recursive programs that call main(),exit(0) and return 0; But in a program that calls main recursively,exit(0) ends the program, and return 0; Is the end of the current main function

-- the dividing line ----
The second part: C language Main function return value problem analysis

A lot of people, even some books on the market, use void main(), which is wrong. Void main() has never been defined in C/C++. Bjarne Stroustrup, The father of C++, clearly states in his 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 talk about the definition of the main function in the C and C++ standards, respectively.

"The C programming Language USES main()." This is because the first version of the C language only one type, that is, int, no char, no long, no float...... Since there was only one type, it could be left unwritten, and later versions, in order to be compatible with the previous code, provided that the default return value was int if the return value was not explicitly specified, which meant that main() was the same as int main() instead of void main(). In C99, the standard requires the compiler to give at least a warning about the use of main().  

1. C
In C89, main() is acceptable. Classic masterpiece The C by Brian w. Kernighan and Dennis m. Ritchie
Programming Language 2e (C programming Language 2nd edition) 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 make some small changes. 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 only if the variable's naming rules are followed.

If you do not need to get arguments 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's 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 object file (such as exe). , means the program exits normally. Still, I recommend that you add a return at the end of the main function, which is not necessary, but is a good habit. Note that vc6 does not include return 0 in the target file; , presumably because vc6 was a '98 product, so this feature is not supported. Now you see why I recommend that you include a return statement. However, gcc3.2 (the C compiler under Linux) adds 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(1998-9-01)Programming languages - C++ 3.6 Start and termination)

Int main() is equal to int main(void) in C99; Int main(int argc, char *argv[]) is also used as defined in C99. Also, 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. . Again, vc6 does not support this feature, but g++3.2 (the C++ compiler under Linux) does.

3. About void main
In C and C++, a function that takes no arguments and returns no information is prototyped as "void foo(void);" . Probably because of this, many people mistake the main function for void main(void) if you don't want the program to return a value. But this is wrong! The return value of the main function should be defined as an int, as specified in the C and C++ standards. While void main can be compiled in some compilers (such as vc6), not all compilers support void main because it is never defined in the standard. If the return value of the main function in g++3.2 is not of type int, it will not pass compilation at all. Gcc3.2 issues a warning. So, if you want your program to be portable, be sure to use int main.

--------------------------------------------------
In short:
The main function void main returns no value
Main defaults to int, that is, int main(), and returns an integer.
Note that the new standard does not allow default return value, that is, int cannot be saved, and the corresponding main function no longer supports void return value, so in order to make the program has a good portability, it is strongly recommended to use:
Int main ()
{
Return 0;  
}
--------------------------------------------------------------------------

Part 3: about main(int argc, char *argv[])

Here is a short excerpt:

Argc is the total number of arguments on the command line  
    Argv [] is the argc parameter, the 0th parameter is the full name of the program, and the subsequent parameter  
    Command line followed by user input parameters, such as:  
    int     The main (int     Arg c,     Char *     Argv [])  
    {  
    int     I;  
    The for     (I     =     0;     i. < Arg c;     I++)  
    cout < < Argv [I] < < Endl;  
    cin > > I;  
    The return     0;  
    }  
    Type in during execution  
    F: \ MYDOCU ~ 1 \ TEMPCODE \ \ DEBUG \ D1 D1. EXE     aaaa     BBB     CCC     DDD  
    The output is as follows:  
    F: \ MYDOCU ~ 1 \ TEMPCODE \ \ DEBUG \ D1 D1. EXE  
    aaaa  

    CCC  
    DDD  


Related articles: