The main function is the entry point of C program, which is indispensable and cannot be duplicated.
The main function should return an integer value to the operating system, and a return of 0 indicates a normal end.
The main function used earlier is all without arguments: int main (void);
Main can have two parameters: int main (int argc,char *argv[]);
The first is the number of parameters that are automatically fetched (+1), and the second is the argument list (a list of strings);
This is generally used for console or DOS,
When we double-click the filename to open the file under Windows, the file name (including the path) is passed through these parameters.
1. Gets the default value for the first parameter of the main function:
It would be 1; We didn't give it a parameter? It turns out to be the first parameter of the file name.
#include <stdio.h>
int main(int argc,char* argv[])
{
printf("%d\n",argc); /* 1 */
getchar();
return 0;
}
2. Get the first parameter (that is, the filename):
#include <stdio.h>
int main(int argc,char* argv[])
{
printf("%s\n",argv[0]);
getchar();
return 0;
}
3. Create and get a list of parameters for the main function:
If it is in the console or DOS, after the program name input is, now?
C++builder menu-> Run-> Parameters ...-> enter test parameters (for example: 11 22 33) in Parameters, separated by spaces.
This dialog box can also be entered from Project-> project Options-> Debuger.
#include <stdio.h>
int main(int argc,char* argv[])
{
int i;
for (i = 1; i < argc; i++) {
printf("%s\n",argv[i]);
}
printf("---\n");
while (--argc) {
printf("%s\n",argv[argc]);
}
getchar();
return 0;
}