Explores the "standard" Writing Method of the main function, obtains the parameters and return values of the main function, and explores the main function

Source: Internet
Author: User

Explores the "standard" Writing Method of the main function, obtains the parameters and return values of the main function, and explores the main function
Main function notation

When I was a beginner in C or C ++, many people have seen a variety of main function Notation:

  • Main (){/*...*/}
  • Void main (){/*...*/}
  • Int main (){/*...*/}
  • Int main (void ){/*...*/}
  • Int main (int argc, char * argv []) {/*... */}
  • Int main (int argc, char ** argv ){/*...*/}
  • ......

After reading so many writing methods, I can't help wondering which one is the standard writing method of the main function?

 

Standard Writing of main Function

First, let's look at the description of the main function in the C language standard documentation:

Let's look at the description of the main function in the C ++ standard document:

Appendix: link for downloading and redirecting from standard documents-C Language Standard

-- C ++ Standard

Now, we can see that in C, the standard syntax isInt main (void) andInt main (int argc, char * argv []). In C ++, the standard syntax isInt main () andInt main (int argc, char * argv []).

Can it be written as main () or void main? The answer is not certain. In the old compiler, this write can also be passed, but it is mainly to be compatible with the historical code before the C/C ++ standard is released. In the newer compiler, a warning message will be generated for the write, or the error message is simply an error message and cannot be compiled.Therefore, for the sake of code standardization and versatility, please stick to the recommended Writing Method in the standard document. Especially for beginners, writing by standard is a good habit, and good habits in programming are even more important than programming itself!

Some people wonder whether the second parameter of main function is char * argv [] or char ** argv? The answer is yes. argv is an array first. Its Array Element type is character pointer. Each character Pointer Points to a specific string, for example:

In addition, some people may have doubts about the parameter name of the main function. Since the main function is also a function, can its parameter names argc and argv be replaced with other valid identifiers? This is also a positive answer. If you are excited, writing it into int main (int a, char * B []) is also allowed, but it is still recommended to write it according to argc and argv, avoid unnecessary questions.

 

Why does the main function have return values and parameter values?

Simply put, in order to communicate with the system environment, the system is not necessarily the operating system, but also the upper-layer environment of other called systems.

The main function is the entry to the system execution program and can be regarded as the main function for the system to call. Since the system can call the main function, the system requires that the main function transmit commands (parameters). At the same time, the main function also has the obligation to return signals to the system environment. In this way, the parameter acts as the entry and return acts as the exit, making the main function more complete.

 

Obtains the parameter value of the main function.

Since the main function can have parameters, you can get the parameter value of it. As long as you pass in the specified real parameter for it, you can print it out in the main function.

Write a piece of code that prints the parameter, name it text. c, compile, the default result shows argv [0] storage function name and its path, and argv [argc] is a null pointer:

Open the cmd command window, switch to the program directory, and input parameters for this program:

Obviously, the printed parameter content is consistent with our expectation.

 

Returns the value of the main function.

Similarly, since the main function returns a signal for the system, the system can certainly obtain it. Rewrite the source file. This time, we skipped all irrelevant parts and only returned values are left.

Compile the program first, then open the window and switch to the program directory. Input text.exe, press enter, and then input echo % errorlevel %. Then you can see the printed return value:

In addition to this method, you can use the main function to call the main function itself to print its return value, but this is equivalent to letting the main function enter the recursive call, and you must set an appropriate termination condition, otherwise, the program will fall into infinite recursion and crash.

 

Summary

From this we can see that the main function can communicate with the system, and it is very consistent with the common function. After all, is it just a function.

In programming, some syntaxes are mandatory by the standard, while others are not mandatory, but are recommended or have no suggestions, but are determined by the implementation of the compiler. At this time, we should try to write the program according to the standard or recommended syntax, which can greatly ensure the robustness of the program.

 

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.