C command-line arguments (main function parameter) and calls in the console window

Source: Internet
Author: User

The main function can take parameters, which can be thought of as the formal parameters of the main function. The C language stipulates that the main function can only have two parameters, which are used to write the two parameters argc and argv. Therefore, the function header of the main function can be written as: Main (ARGC,ARGV) C language also specifies that ARGC (the first parameter) must be an integer variable, argv (the second parameter) must be an array of pointers to the string. With the formal parameter description, the function head of the main function should be written as: Main (argc,argv) int argv; Char *argv[]; or written as: main (int argc,char *argv[])

Because the main function cannot be called by other functions, it is not possible to get the actual value inside the program. So where does the argument value be given to the formal parameter of the main function? In fact, the parameter value of the main function is obtained from the operating system command line.   When we want to run an executable file, type the file name at the DOS prompt, and then enter the actual parameters to transfer these arguments to the main parameter. The general form of the command line at the DOS prompt is:

c:/> executable file name parameter parameters ...;

However, it should be noted that the two parameters of main and the arguments in the command line are not one by one corresponding to the position. Because main has only two formal parameters, the number of arguments in the command line is not limited in principle. The ARGC parameter represents the number of arguments in the command line (note: The file name itself is also a parameter), and the value of ARGC is automatically assigned by the system by the number of actual parameters when the command lines are entered. For example, there are command behaviors:

C:/>e6 BASIC dbase FORTRAN, because the file name E6 24 itself is a parameter, so there are 4 parameters, so argc obtained a value of 4. The argv parameter is an array of string pointers whose element values are the first address of each string in the command line (arguments are processed by string). The length of the pointer array is the number of arguments. The initial value of an array element is automatically assigned by the system.

The following procedures are examples

#include <stdio.h>
int main (int argc,char *argv[]) {
for (int i=0;i<argc;i++)
printf ("%s\n", Argv[i]);
return 0;
}

Compile the above program to generate EXE file 1.18.exe

Put it on the C drive

Window key plus r to open run Windows

Enter CMD to open the console window

Enter cd\ to transfer the directory to the C packing directory (1.18.exe file is placed here)

Input 1.18 paral1 Paral2 s.txt 5 4

C command-line arguments (main function parameter) and calls in the console window

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.