c language command-line arguments argc,argv

Start

int main(int argc, const char * argv[])

In the C language learning time,We are writing a program,Then view the results in a terminal run,Enter a number, and so on ...... do not know if you have not tried it allows you to write programs that can accept parameters like system commands it? For example,:ls -al like this。

If you wish to do so,Like above, as to declare it!

among them:

argv: Pointer Pointer

argc: Integer

For chestnuts

The name of the program to test assumptions,When only input test,It came by the operating system parameters:

argc = 1, represents only a program name;

argc only one element,argv[0]Point to Programs Enter the path and name:./ test

When the input test para_1,There is a parameter,It came by the operating system parameters:

argc = 2,In addition to showing the name of the program but also have a parameter;

argv[0]Point to Programs Enter the path and name;

argv[1]Pointing to a string parameter para_1

When the input test para_1 para_2 there are two parameters,It came by the operating system parameters:

argc = 3,In addition to showing the program name outside there are two parameters;

argv[0]Point to Programs Enter the path and name;

argv[1]Pointing to a string parameter para_1;

argv[2]Pointing to a string parameter para_2;

so,So ~

The actual terms of chestnuts:

char *argv[]: argv is an array of pointers,Argc is the number of elements,Stored is a pointer to each parameter,The first element of argv[0]The full path name for the program to run,From the two elements(argv[1])Start,Is the name of each parameter,The final element is NULL。In general,which is:

* argv: Array of strings

argv[0] The full path name for the program to run

argv[1] The first string is executed after the program name;

argv[2] For the second string after the execution of the program name;

argv[argc]Is NULL。

int argc: It indicates the size of the argv,Is the actual number of arguments +1,Where +1 is because argv[0]Is the name of the compiled executable file

In this article from referenceHere

Original article written by LogStudio:R0uter's Blog » c language command-line arguments argc,argv

Reproduced Please keep the source and description link:https://www.logcg.com/archives/494.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

Your email address will not be published. Required fields are marked *