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:
1 |
int main(int argc, const char * argv[]) |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/*program name EXAMPLE.EXE*/ #i nclude <stdio.h> #i nclude <stdlib.h> main(int argc, char *argv[], char *env[]) { int i; printf( "These are the %d command- line arguments passed to main:\n\n", argc); for( i = 0; i <= argc; i++) printf( "argv[%d]:%s\n", i, argv[i] ); printf( "\nThe environment string(s)on this system are:\n\n"); for( i = 0; env[i] != NULL; i++ ) printf( " env[%d]:%s\n", i, env[i] ); } ./EXAMPLE first "I’m a good boy" c "last " stop //注意: 可以用双引号括起内含空格的参数, 如本例中的: " argument with blanks"和"Last but one")。 //结果是这样的: These are the 6 command-linearguments passed to main: argv[0]:./ EXAMPLE argv[1]:first argv[2]: I’m a good boy argv[3]:c argv[4]: last argv[5]:stop argv[6]:(NULL) |
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