The biggest feature of the C program is that all programs are assembled using functions. Main () is called the Master function, which is all the threads
Preface to run the entrance. The remaining functions are called either with or without parameters, both by the main () function or by other general functions, if you call
is a parameter function, the argument is passed at the time of the call.
Main ()
{
...
Y1=F1 (X1,X2);
...
}
F1 (int a,int b)
{
....
Y2=F2 (X3,X4);
....
}
F2 (int m,int N)
{
....
.....
}
In the study of the previous course, the main () function is always treated as a calling function, that is, allow main () to call
Other functions and pass parameters. In fact, the main () function can be either a parameterless function or a function with a parameter. For a
argument, you need to pass parameters to it. But no other function can call the main () function. And of course, the same
cannot be passed to the main () function and can only be passed outside of the program. How can this specific problem be solved?
Let's take a look at the form of a parameter with the main () function:
Main (ARGC,ARGV)
int Argc,char *argv[];
{
.....
}
From the form of function arguments, contains an integer and an array of pointers. When a C source program is compiled and linked, the extension is generated. EXE executable file, which can be run directly under the operating system files, in other words, is the system to start running. Since the main () function cannot be invoked and passed by other functions, it can only be done by the
The system passes parameters when the runtime is started.
In the operating system environment, a complete run command should include two parts: command and corresponding parameters. The format is:
Command parameter 1 parameter 2 .... Parameter n¿
This format is also known as the command line. The command line is the file name of the executable file, followed by a space separating the parameters, and further supplementing the command, which is the argument passed to the main () function.
The command line has the following relationship with the parameter of the main () function:
SET command behavior: Program str1 str2 STR3 STR4 STR5
Program is the file name, which is compiled by PROGRAM.C, linked to the resulting executable file Program.exe, followed by 5 parameters. For the main () function, its parameter ARGC records the number of commands and arguments in the command line, a total of 6, the size of the pointer array is determined by the value of the parameter argc, that is char*argv[6], and the pointer array is shown in Figure 6-15.
The pointers to the array point to a string, respectively. It should be noted that the pointers to the array of pointers received are received from the start of the command line, first received by the command, and then
is the argument.
The following example illustrates the correct use of the main () function with parameters.
[Example 6-29] draws a changing loop using a graphics library function. It's dividing a circle with a radius of R 1 into n parts,
At the center of each dividing point, the N circle is drawn with R S as the radius (detailed theory of the Drawing) the 9th Chapter 1th section of the textbook is a special
Door Introduction, here only for simple analysis). With the parameter form of the M a I n () function, we can from the keyboard to the command line side
Type R 1 and R S and the background color of the screen.
#include <graphics.h>/* header files containing graphics library functions */
#include <math.h>
#define PI 4.1415926
M a i n (a r g C, a R G v)
int Argc;char *argv[]; /* Define M a i n () with parameters */
{
int x,y,r1,rs,color;
Double A;
int Gdriver=detect,gmode;
Initgraph (&gdriver,&gmode, ". \\bgi ") Kai;/moving * Graphics working mode * *
R1=atoi (argv[1])//* Calculate Base Circle radius * *
Rs=atoi (argv[2])/* Calculate Concentric circle radius * *
Color=atoi (argv[3])//* Background color * *
Cleardevice ()/* Clear the graphics screen */
SetBkColor (color);/* Set Background color * *
SetColor (4);/* Set the graphics display color * *
for (A=0;A<=2*PI;A+=PI/18)/* Draw Concentric circle * *
{
X=r1*cos (a) +320;
Y=r1*sin (a) +240;
Circle (X,Y,RS)/* To center coordinates x, y, radius of Rs Draw Circle * * *
}
Getch ()/* Wait for key to continue * *
Close graph ();/* Turn off graphics work/
}
If the program name is L6-29.C, compile, link to generate executable file L6-29.exe. Running in the operating system environment
program, the command-line method is:
L6-29 40 20 3
The arguments to the command line and main () function are as close as Figure 6-16 shows
System.