zoukankan      html  css  js  c++  java
  • C基础知识(14):命令行参数

    命令行参数是使用main()函数参数来处理的,其中,argc是指传入参数的个数,argv[]是一个指针数组,指向传递给程序的每个参数。

    应当指出的是,argv[0]存储程序的名称,argv[1]是一个指向第一个命令行参数的指针,*argv[n]是最后一个参数。如果没有提供任何参数,argc 将为1,否则,如果传递了一个参数,argc将被设置为2。

    多个命令行参数之间用空格分隔,但是如果参数本身带有空格,那么传递参数的时候应把参数放置在双引号或单引号内部

     1 #include <stdio.h>
     2 
     3 int main(int argc, char *argv[]) {
     4     if (argc == 2) {
     5         printf("The argument supplied is %s
    ", argv[1]);
     6     } else if (argc > 2) {
     7         printf("Too many arguments supplied.
    ");
     8     } else {
     9         printf("One argument expected.
    ");
    10     }
    11 }

    Result

    [xxxxx]$ ./Test.o 
    One argument expected.
    [xxxxx]$ ./Test.o a
    The argument supplied is a
    [xxxxx]$ ./Test.o a b
    Too many arguments supplied.
    [xxxxx]$ ./Test.o "a b"
    The argument supplied is a b
  • 相关阅读:
    win7下命令行添加系统服务
    java执行cmd命令
    grails-BuildConfig.groovy中grails.war.resources
    密码学
    Grails框架优劣势
    groovy+idea+Maven项目加载自身jar包
    cmd查看我的电脑盘符和文件
    MySQL insert插入
    MySQL截取字符串函数方法
    mysql 替换语句
  • 原文地址:https://www.cnblogs.com/storml/p/7826732.html
Copyright © 2011-2022 走看看