Dialysis C ++ main () function form

Source: Internet
Author: User

No matter what program is developed or the code is compiled, it must be determined that the C ++ program must have the C ++ main () function, C ++ main () function is the entry point of the C ++ program. However, not all C ++ programs have traditional C ++ main () functions, because they usually do not use return values.

The C ++ main () function is similar to other functions and has the same components. In a 32-bit console application, C ++ Builder generates the default C ++ main () function with the following prototype: int main (int argc, char ** argv ); the main () function takes two parameters and returns an integer value.

As mentioned above, numeric values are passed to the function when a function is called. However, the C ++ main () function is not directly called, but is automatically executed when the program is running. So how does the C ++ 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 run the following command line at the DOS prompt: grep WM_KILLFOCUS handle-I

Here we need to use the command line variable WM_KILLFOCUS, d, and I to start the grep program. We need to demonstrate how to change it to argc and argv in the C ++ 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. 1: #include <iostream.h>   
  2.  
  3. 2: #include <conio.h>   
  4.  
  5. 3: #pragma hdrstop   
  6.  
  7. 4:   
  8.  
  9. 5: int main(int argc,char **argv)   
  10.  
  11. 6: {   
  12.  
  13. 7:cout << "argv = "argc << end1;   
  14.  
  15. 8.for (int i=0;i<argc;i++)   
  16.  
  17. 9. cout << "Parameter " << i << ": " << argv[i]<< end1;   
  18.  
  19. 10. cout << end1 << "Press any key to continue...";   
  20.  
  21. 11: getch();   
  22.  
  23. 12: return 0;   
  24.  
  25. 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 C ++ main () function is not important because the return value is usually not used. In fact, the C ++ main () function is not required to return a value. The C ++ main () function has multiple forms. The following statements are valid:

 
 
  1. int main(void); // same as above   
  2.  
  3. int main(int argc,char** argv);   
  4.  
  5. void main();   
  6.  
  7. void main(int argc, char** argv);  

There are more forms. If you do not want to use the command line variable, you can use the first C ++ 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 basic form of the C ++ main () function does not take the parameter and returns an int.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.