Detailed description of the main function with command line parameters [reprint]

Source: Internet
Author: User
Document directory
  • 1. Form of main () function
  • Ii. return values of the main () function
  • 3. parameters of the main () function

C's design principle is to use functions as the component module of the program. The main () function is called the main function, and a C program is always executed from the main () function.

1. Form of main () function

In the latest c99 standard, only the following two definitions are correct:

(See ISO/IEC 9899: 1999 (E) 5.1.2.2.1 program startup)

Int main (void) -- no parameter form
{
...
Return 0;
}
Int main (INT argc, char * argv []) -- with parameter form
{
...
Return 0;
}

Int indicates the return type of the main () function. The parentheses after the function name generally contain information passed to the function. Void indicates that no parameters are passed to the function. We will discuss the form with parameters later.
Browsing the C code of the old version will find that the program often uses
Main ()

This form begins. This form is allowed by the C90 standard, but not by the c99 standard. Therefore, do not write this statement even if your current compiler permits it.
You may have seen another form:

Void main ()
Some compilers allow this form, but there is no standard to accept it. Bjarne stroustrup, the father of C ++, clearly stated in the FAQ on his homepage that the definition of void main () never exists in C ++ or C. Therefore, the compiler does not have to accept this form, and many compilers do not allow this writing.
Sticking to the standard means that the program can still run normally when you move the program from one compiler to another.

Ii. return values of the main () function

From the above, we know that the return value type of the main () function is int type, while the return 0 at the end of the program is echo with it. 0 is the return value of the main () function. So where will this 0 be returned? Return to the operating system, indicating that the program Exits normally. Because the return statement is usually written at the end of the program, no matter what value is returned, as long as this step is reached, it indicates that the program has been completed. Return not only returns a value, but also ends a function.
Now let's do a small experiment (Note: My system is Windows XP and the compiling environment is Tc) to observe the return value of the main () function. Compile and run the following code:

// A. C
# Include "stdio. H"
Int main (void)
{
Printf ("I love you .");
Return 0;
}

Save this file as A.C. After compilation and operation, an a.exe file will be generated. Now open the command prompt, run the compiled executable file in the command line, input echo % errorlevel %, and press Enter. Then, the program returns 0. If you change return 0; to return 99;, you can see that the program returns 99 after performing the preceding steps again. If you write return 99.99 in this way, 99 is returned because the forced type is converted to the integer type before 99.99 is passed to the operating system.
Now, we change A.C back to the original code, and then write another program B .C:

// B .C
# Include "stdio. H"
Int main (void)
{
Printf ("/Ni'm too .");
Return 0;
}

 

After compiling and running, open the command prompt and enter a & B in the command line, so that you can see the classic love dialogue in "The ghost is gone:

I love you.
I'm too.

& Meaning: IF & the previous program Exits normally, continue to execute & the subsequent program; otherwise, it will not be executed. Therefore, if you delete the return 0; in A. C or change it to return 99;, you can only see I love you .. That is to say, program B. C will not be executed. Now, you should understand the role of return 0.

3. parameters of the main () function

The C compiler allows the main () function to have no parameters or two parameters (some implementations allow more parameters, but this is only an extension of the standard ). The two parameters are int type and string type. The first parameter is the number of strings in the command line. According to the Convention (but not mandatory), this int parameter is called argc (argument count ). You may only understand why this parameter has such a strange name! Look up the dictionary by yourself. The second parameter is a pointer array pointing to a string. Each string in the command line is stored in the memory and a pointer is assigned to it. By convention, this pointer array is called argv (argument value ). The system uses spaces to separate strings. In general, assign the program name to argv [0], and then assign the first string to argv [1.
Let's take an example:

// C. C
# Include "stdio. H"
Int main (INT argc, char * argv [])
{
Int count;
Printf ("the command line has % d arguments:/N", argc-1 );
For (COUNT = 1; count <argc; count ++)
Printf ("% d: % s/n", Count, argv [count]);
Return 0;
}

Compile and run the program. Enter c I love you in the command line and press Enter. The following is the result of running the program from the command line:

The command line has 3 arguments:
1: I
2: Love
3: You

In this example, the program receives four strings (including program names) from the command line and stores them in the string array. The correspondence relationship is as follows:
Argv [0] ------> C (program name)
Argv [1] ------> I
Argv [2] ------> love
Argv [3] ------> you
As for the value of argc, that is, the number of parameters, the program will automatically count during runtime, so we don't have to worry about it.
In this example, every string is a word (letter). What should I do if I want to assign a sentence to the program as a parameter? You can enter C "I love you." "I'm too." In the command line .". Program running result:

The command line has 2 arguments:
1: I love you.
2: I'm too.

Corresponding Relationship:
Argv [0] ------> C (program name)
Argv [1] ------> I love you.
Argv [2] ------> I'm too.

Note that all your input in the command line will be stored in memory as strings. That is to say, if you enter a number, you should output this number in % s format instead of % d or others.
Let's look at an example:

// D. c
# Include "stdio. H"
Int main (INT argc, char * argv [])
{
File * FP;
Fp = fopen (argv [1], "W ");
Fputs ("I love you.", FP );
Fclose (FP );
Return 0;
}

Compile and run. Open the command line and enter D love.txt and press Enter. In this way, open the directory where the d. c file is located, and you will find that there is an additional file named love.txt. After opening it, the content inside is exactly the most widely spoken sentence in the world.
Of course, you may say that you can do this without using command line parameters. Yes, of course you can. The reason for using command line parameters may be to practice command line usage, So that you need to write a command line-based program in the future. Another advantage is that you can run compiled programs without the need for the c environment. For example, if you send the d.exe generated after the compilation of the program to your girlfriend and then tell her how to run it, then your girlfriend can use another way to appreciate your affection for her.

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.