How to use C language command line parameters? What is the function of C-language command line parameters ?, Command Line Parameters

Source: Internet
Author: User

How to use C language command line parameters? What is the function of C-language command line parameters ?, Command Line Parameters

How to use C language command line parameters? What is the function of C-language command line parameters? I have used command line parameters in C Language many times before, but I am not sure every time I use them. I need to re-query the information. Here is a summary. The command line parameters in C language are very simple. You only need a simple example to describe them:

# Include

Void main (int argc, char ** argv)

{

Printf ("% d \ n", argc );

Printf ("% s \ n", argv [0]);

Printf ("% s \ n", argv [1]);

Printf ("% s \ n", argv [2]);

}

In the above example, we pass two parameters to the main function: argc and argv. Argc is of the int type and represents the number of command line parameters. It is not allowed to be passed by the user. It is automatically determined based on the number of parameters entered by the user from the command line. Argv is of the char ** type, and its function is to store the parameters passed by the user from the command line. Its first member is the name of the program run by the user.

For the above example, we save it as test. c and use gcc to compile and generate the target file as test. The test is as follows:

(1) we run the program directly in the command line without passing any other parameters:

./Test

The running result is as follows:

The first output is argc. Because we only input./test, argc is 1, that is, there is only one command line parameter. The first command line parameter output later is./test. Then the program will go wrong, because there are no second or third parameters in the end. When writing an actual application, you should pay attention to fault tolerance for this part.

(2) run the following command:

./Test hello world

We can see that argc is 3, the first parameter is./test, the second parameter is hello, and the third parameter is world.

(3) continue to enter:

./Test hello world

As you can see, the argc is changed to 5, and the argv index has the first three parameters. Of course, you can also output the following two parameters.

If a parameter passed in the command line contains spaces, you need to use "" to expand the parameter, for example:

./Test "hello world" "hello world"

When we pass the hello world as a parameter, we need to use "hello world ".

Note that the argv type can be char ** argv, char argv [] [], char * argv [].

Related Article

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.