Reprinted-functions and standard formats of the Main Function

Source: Internet
Author: User
Tags first string
[Go] about the main function

10:39:33 | category: Learning Space | font size subscription

From: http://blog.csdn.net/jylnn/archive/2008/03/08/2158429.aspx

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:

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.

Command line parameters in C language: argc and argv
Key words: argc argv
Main (INT argc, char ** argv)
Argv is a pointer.
Argc is an integer
Char ** argv or: char * argv [] or: Char argv [] []
The main () brackets are fixed.

The following is an example to understand the usage of these two parameters:

Assume that the program name is prog,

If you only enter prog, the parameters sent from the operating system are:

Argc = 1, indicating that there is only one program name.
Argc has only one element. argv [0] points to the input program path and name:./prog

When you enter prog para_1, there is a parameter, the parameter sent from the operating system is:

Argc = 2, indicating that there is a parameter besides the program name.
Argv [0] points to the input program path and name.
Argv [1] points to the para_1 string.

When the input prog para_1 para_2 has two parameters, the parameters sent from the operating system are:

Argc = 3, indicating that there are two parameters besides the program name.
Argv [0] points to the input program path and name.
Argv [1] points to the para_1 string.
Argv [2] points to the para_2 string.

Void main (INT argc, char * argv [])
Char * argv []: argv is a pointer array. Its number of elements is argc, which stores pointers to each parameter, the first element of argv [0] is the executable file name generated by compilation (including path eg: "F: \ Vc \ ex1 \ debug \ ex1.exe "), each parameter starts with two elements (argv [1 ]).
Int argc indicates the size of argv, which is the actual number of parameters + 1, and + 1 is because argv [0] is the executable file name after compilation.

 

 

Main () Main Function

Every C program must have a main () function, which can be placed in
. Some programmers put it at the beginning, while others put it at the end, regardless
Which of the following statements is applicable.
1. Main () parameter
During Turbo c2.0 startup, three parameters of the main () function are passed: argc, argv, and Env.
* Argc: an integer that represents the number of command line parameters passed to main.
* Argv: String Array.
In dos 3.x, argv [0] is the full path name of the program running. For DoS 3.0
In the following versions, argv [0] is an empty string ("").
Argv [1] is the first string after the program name is executed in the doscommand line;
Argv [2] is the second string after the execution program name;
...
Argv [argc] is null.
* Env: an array of security strings. Each element of env [] contains characters in the form of envvar = value.
String. Envvar is an environment variable, such as path or 87. The value is the corresponding value of envvar, such as c: \ dos, C:
\ Turboc (for Path) or yes (for 87 ).
When Turbo c2.0 is started, these three parameters are always passed to the main () function.
Description (or not description). If some (or all) parameters are specified, they become main () subprograms.
.
Note: Once you want to describe these parameters, they must be in the order of argc, argv, and env, as shown below
Example:
Main ()
Main (INT argc)
Main (INT argc, char * argv [])
Main (INT argc, char * argv [], char * env [])
The second case is legal, but not common, because argc is rarely used in programs
Where argv [] is used.
The following provides the example program example. EXE to demonstrate how to use three parameters in the main () function:
/* Program name example. EXE */
# I nclude <stdio. h>
# I nclude <stdlib. h>
Main (INT argc, char * argv [], char * env [])
{
Int I;
Printf ("these are the % d command-line arguments passed
Main: \ n ", argc );
For (I = 0; I <= argc; I ++)
Printf ("argv [% d]: % s \ n", I, argv [I]);
Printf ("\ nthe environment string (s) on this system are: \ n ");
For (I = 0; ENV [I]! = NULL; I ++)
Printf ("env [% d]: % s \ n", I, ENV [I]);
}
Run example. EXE as follows at the DOS prompt:
C: \ example first_argument "argument with blanks" 3 4 "Last
One "Stop!
Note: Double quotation marks can be used to enclose parameters with spaces. In this example, "argument
With blanks "and" last but one ").
The result is as follows:
The value of argc is 7
These are the 7 command-linearguments passed to main:
Argv [0]: C: \ Turbo \ example. exe
Argv [1]: first_argument
Argv [2]: argument with blanks
Argv [3]: 3
Argv [4]: 4
Argv [5]: last but one
Argv [6]: Stop!
Argv [7]: (null)
The environment string (s) on this system are:
Env [0]: comspec = c: \ command. com
Env [1]: prompt = $ p $ g/* depends on the specific settings */
Env [2]: Path = c: \ DOS; C: \ TC/* depends on the specific settings */

It should be noted that the maximum length of the command line parameter for transmitting the main () function is 128 characters (Package
Including spaces between parameters), which is restricted by DOS.

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.