C language
In the C language, the input parameters of the main function, argc and argv, are passed into the command-line arguments.
- The ARGC is of type int, indicating the number of arguments passed in the command line (argument count);
- ARGV is the char** type, which represents the command-line argument vector (argument vector), and each command-line argument is a string type, so the parameter argv can also be seen as a string array.
The sample code is as follows:
/** filename:pass_args.c* This code was modified a little according to the Arun ' s answer inHTTPS://STACKOVERFLOW.COM/QUESTIONS/3898021/REGARDING-MAININT-ARGC-CHAR-ARGV*/#include<stdio.h>intMainintargcChar*argv[]) { inti =0; printf ("argc =%d\n", ARGC); for(; i < argc; + +)i) {printf ("argv[%d] =%s\n", I, argv[i]); } return 0;}
The Compile command is
$ gcc pass_args.c-o Pass_args
Run the command and the results are as follows:
34 0] =./1] = 2] = 3 3
Python Language
The Python language can use the argv variable in the SYS module to achieve similar functionality. If you need more advanced command-line parameter passing, you can use the Argparse module. The sample code for using SYS.ARGV is as follows:
#!/usr/bin/env python#-*-Coding:utf8-*- from __future__ Importprint_functionImportSYSif __name__=='__main__': Print('The argument count is%d.'%Len (sys.argv))Print('The argument vectors:') forI, ARGinchEnumerate (SYS.ARGV):Print('argv[%d] =%s'% (i, ARG))
Run the command and the results are as follows:
34. The argument vectors: 0 ] = pass_args.py 1 ] = arg1 2 ] = arg2 33
References
1. Regarding ' main (int argc, char *argv[]) ' [duplicate]. https://stackoverflow.com/questions/3898021/regarding-mainint-argc-char-argv
2. Entry Point-wikipedia. Https://en.wikipedia.org/wiki/Entry_point
3. Python-command line Arguments. Https://www.tutorialspoint.com/python/python_command_line_arguments.htm
Sample code for passing command line arguments (C and Python)