[转载] c语言中检查命令行参数_C中的命令行参数

参考链接: Java中的命令行参数

c语言中检查命令行参数

  Command line argument is a parameter supplied to the program when it is invoked. Command line argument is an important concept in C programming. It is mostly used when you need to control your program from outside. Command line arguments are passed to the main() method. 

  命令行参数是调用程序时提供给程序的参数。 命令行参数是C编程中的重要概念。 它主要用于需要从外部控制程序的情况。 命令行参数将传递给main()方法。  

 Syntax: 

  句法:  

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

 Here argc counts the number of arguments on the command line and argv[ ] is a pointer array which holds pointers of type char which points to the arguments passed to the program. 

  这里argc计算命令行上的参数数量,而argv[ ]是一个指针数组,其中保存着char类型的指针,该指针指向传递给程序的参数。  

  命令行参数示例 (Example for Command Line Argument) 

 #include <stdio.h>

#include <conio.h>

 

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

{

    int i;

    if( argc >= 2 )

    {

        printf("The arguments supplied are:\n");

        for(i = 1; i < argc; i++)

        {

            printf("%s\t", argv[i]);

        }

    }

    else

    {

        printf("argument list is empty.\n");

    }

    return 0;

  Remember that argv[0] holds the name of the program and argv[1] points to the first command line argument and argv[n] gives the last argument. If no argument is supplied, argc will be 1.  

  请记住, argv[0]保存程序的名称, argv[1]指向第一个命令行参数,而argv[n]给出最后一个参数。 如果未提供任何参数,则argc将为1。  

 

  翻译自: https://www.studytonight.com/c/command-line-argument.php

 

 c语言中检查命令行参数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值