The C ++ program must have the main () function. The main () function is the entry point of the program. Each sample program described above has the main () function. However, not all C ++ programs have traditional main () functions. Windows program entry point functions written in C or C ++ are called WinMain () instead of traditional main () functions. It indicates that the C ++ Builder GUI application has WinMain (), but is hidden.
C ++ Builder allows you to focus on the creation of user interfaces and other parts without considering the low-level details of Windows programs. The main () function is similar to other functions and has the same components. In a 32-bit console application, C ++ Builder generates the default main () function with the following prototype: int main (int argc, char ** argv); this main () take two parameters in the function form and return an integer value.
- Waste collection defects in c ++
- Learn and guide Visual C ++ development tools
- Introduction to function overloading in C ++
- Reference functions and variables in C ++ in C
- Advanced technical scholars compile C ++ code
As mentioned above, numeric values are passed to the function when a function is called. However, the main () function is not directly called, but is automatically executed when the program is running. So how does the main () function obtain parameters? The method is obtained from the command line. It is described as follows: Assume that a Win32 console application needs to be executed with the following command line at the DOS prompt.
Here we need to use the command line variable WM_KILLFOCUS, d, and I to start the grep program. We want to demonstrate how to change it into argc and argv in the main () function. first, the argc variable contains at least 1 Number of parameters passed in the command line, because the program name is also counted as a parameter. The variable argv is an array containing the pointer of a string. This array contains each string passed in the command line. In this example:
- 1: #include <iostream.h>
- 2: #include <conio.h>
- 3: #pragma hdrstop
- 4:
- 5: int main(int argc,char **argv)
- 6: {
- 7:cout << "argv = "argc << end1;
- 8.for (int i=0;i<argc;i++)
- 9. cout << "Parameter " << i << ": " << argv<< end1;
- 10. cout << end1 << "Press any key to continue...";
- 11: getch();
- 12: return 0;
- 13: }
Save the Project as arstest. Instead of clicking the Run button, select Project | Build All in the main menu. In this way, only the Project is created and no program is executed. After the project is completed, select Run | Parameters from the main menu and enter the following content in the RunParameters field of the RunParameters dialog box: one two three "four five" six and then click the Run button, the program runs with the specified command line parameters. Another method is to run the program at the DOS prompt using the following command line: When arstest one two three "four five" six is running, it displays the number of incoming changes, then list each variable.
Run the command several times and provide variable elements for different command lines each time. Pay attention to the result. In most programs, the return value of the main () function is not important because the return value is usually not used. In fact, the main () function is not required to return a value. The main () function has multiple forms. The following statements are valid:
- main();int main();// same as above
- int main(void); // same as above
- int main(int argc,char** argv);
- void main();
- void main(int argc, char** argv);
There are more forms. If you do not want to use the command line variable, C ++ Builder can use the first form of main () function, which is not null in the parameter brackets) returns an int (if not specified, the default return value is returned ). In other words, the most basic form of the main () function does not take the parameter and returns an int