Sample code for passing command line arguments (C and Python)

Source: Internet
Author: User

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)

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.