The standard form of the main function

Source: Internet
Author: User
Tags first string

This article is reproduced from http://blog.chinaunix.net/uid-26495963-id-3054731.html

The design principle of C is to use the function as the constituent module of the program. The main () function is called the main function, and a C program always
is executed starting from the main () function.
The form of the main () function
In the latestC99 Standard, only the following two ways of defining them 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, and the parentheses following the function name typically contain the function passed to the
Information. void means that no arguments are passed to the function. With regard to the form of parameters, we will discuss them.

Browsing the old version of C code, you will find that the program often

Main ()
This form begins. The C90 standard allows this form, but the C99 standard is not allowed. So even if your current
The compiler allows and does not write this.

You may also have seen another form:
void Main ()

Some compilers allow this form, but there is no standard to consider accepting it. C + + 's parent Bjarne
Stroustrup clearly stated in the FAQ on his homepage: void main () definition never exists
is C + + or C. Therefore, the compiler does not have to accept this form, and many compilers do not allow such
Write.

The point of sticking with the standard is that when you move a program from one compiler to another, you still
can operate normally.
Second, the return value of the main () function
From the front we know that the return value type of the main () function is int type, and the last return of the program is 0;
Is echoing, 0 is the return value of the main () function. So where does this 0 return to? Return to the operator
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 soon as this step is reached, the program is complete. And return is not just about returning a value,
It is also the End function.
Now let's do a little experiment (note: My system is Windows XP, the compilation environment is TC) to
Observe the return value of the main () function. Write the following code and compile the run:
A.c
#include "stdio.h"
int main (void)
{
printf ("I love You.");

return 0;
}
Save this file as A.C, and a a.exe file will be generated when the compilation runs. Now open the command prompt
, run the executable you just compiled in the command line, and then enter Echo%errorlevel%, back
Car, you can see that the program returns a 0. If you put return 0; Change to return 99; , then it is very obvious
Then, after performing the above steps again, you can see that the program returns 99. If you write this return 99.99;
That still returns 99, because the coercion type is converted to an integer type before the 99.99 is passed to the operating system.
Now, let's change the 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 the run, open a command prompt and enter a&&b in the command line so you can see the
The classic love Dialogue in the Ghost of Man:

I Love you.
I ' m too.
&& means: If the previous program in && exits normally, proceed to && the following program, no
is not executed. So, if you put the return of A.C inside 0; Delete or change to return 99; , then you
can only see I love you. 。 In other words, the program B.C is not executed. Now, everyone should know that return
0; The role of the bar.

Parameters of the main () function
The C compiler allows the main () function to have no arguments, or two parameters (some implementations allow more arguments
, but this is only an extension of the standard). These two parameters, one is the int type and one is the string type. The
One parameter is the number of strings in the command line. By convention (but not required), this int parameter is called
ARGC (argument count). You may now understand why this parameter takes such a strange name.
Word, hehe! As for the meaning of English, look it up in the dictionary. The second argument is a pointer to a string
Array. Each string in the command line is stored in memory, and a pointer is assigned to it. By convention
, this pointer array is called ARGV (argument value). The system uses spaces to open each string.
In general, assign the name of the program itself to Argv[0], and then assign the last first string to the
Argv[1], and so on.
Now let's look at 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
printf ("%d:%s\n", Count,argv[count]);

return 0;
}
Compile run, enter C I love on the command line, and the following is the node that runs the program from the command line
Fruit:
The command line has 3 arguments:
1:i
2:love
3:you
As you can see from this example, the program accepts 4 strings (including the program name) from the command line and
stored in a string array, its corresponding relationship:
ARGV[0]------> C (program name)
ARGV[1]------> I
ARGV[2]------> Love
ARGV[3]------> You
As for the value of ARGC, which is the number of parameters, the program will be automatically counted at runtime, do not need us to worry about.
In this example, each string is a word (the letter), and since it is a string, a sentence
What should I do if I assign to a program as a parameter?  You can type C "I love You" at the command line. "I ' m
too. ". Program Run Result:
The command line has 2 arguments:
1:i Love you.
2:i ' m too.
The corresponding relationship:
ARGV[0]------> C (program name)
ARGV[1]------> I Love you.
ARGV[2]------> I ' m too.
Note that your input to the command line will be stored as a string in memory. Other words
If you enter a number, you should use the%s format instead of%d or the other to output the number.
Let's look at one more 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 run, open the command line and enter D love.txt carriage return. This opens the directory where the D.C file is located
, you will find a file called Love.txt, open after the content is the world's most said
That sentence.
Of course, you might say that I don't need to use command-line arguments to do this. Yes, of course you can.
。 The reason for using command-line arguments might be to practice command-line usage in case you need to write command-line-based
Program. Another benefit is that you can run a compiled program without the need for a C environment. For example, you put on
Face that program compiled after the generated d.exe sent to your girlfriend, and then tell her how to run, so that your female
A friend can feel your love for her in another way.

The standard form of the main function

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.