Command-Line Arguments

All the executable programs above have a main(void) program

  • more generally, executables take arguments on the command line
  • these enter the program via parameters

    切换行号显示

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

For example:

prompt$ ./a.out -pre 3

means that:

  • argc refers to the number of arguments, including the program name

    • argc = 3

  • argv[] allows access to arguments represented as strings

    • argv[0] is a pointer to the string "./a.out"

    • argv[1] is a pointer to the string "-pre"

    • argv[2] is a pointer to the string "3"

Command-line argument processing example 1

Write a program that:

  • takes an optional numerical command-line argument 1, 2, 3 or 4
  • if the argument is not one of these characters (!), a usage message is printed

    切换行号显示

     // commarg.c
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[]) {
    if (argc == 1 ||
    (argc == 2 && atoi(argv[1]) >= 1 && atoi(argv[1]) <= 4)) {
    // we can do something here
    } else {
    printf("Usage: %s [1|2|3|4]\n", argv[0]);
    }
    return EXIT_SUCCESS;
    }
  • notice that atoi() had to be called to convert the character to a number

  • compiling and executing the program:
     prompt$ dcc commarg.c
    prompt$ ./a.out
    prompt$ ./a.out 1
    prompt$ ./a.out 2
    prompt$ ./a.out 3
    prompt$ ./a.out 4
    prompt$ ./a.out 5
    Usage: ./a.out [1|2|3|4]

Command-line argument processing example 2

Write a program that:

  • takes an optional command-line switch -reverse

  • if the switch is not correct, a usage message is printed

    切换行号显示

     // commargrev.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(int argc, char *argv[]) {
    if (argc == 1 ||
    (argc == 2 && !strcmp(argv[1], "-reverse"))) {
    // NOTE: strcmp returns 0 if matches.
    // we could do something here
    } else {
    printf("Usage: %s [-reverse]\n", argv[0]);
    }
    return EXIT_SUCCESS;
    }
     prompt$ ./a.out
    prompt$ ./a.out -reverse
    prompt$ ./a.out rubbish
    Usage: ./a.out [-reverse]

Makefile

【413】C 语言 Command line的更多相关文章

  1. Visual Studio C++ Command Line

    最近在Visual Studio 2012进行vp8的开发,之前一直都是在ubuntu上进行开发,关于编译链接的一些选项可直接在makefile中定义,比如vp8的头文件都放在include文件下面, ...

  2. 如何在Windows下开发Python:在cmd下运行Python脚本+如何使用Python Shell(command line模式和GUI模式)+如何使用Python IDE

    http://www.crifan.com/how_to_do_python_development_under_windows_environment/ 本文目的 希望对于,如何在Windows下, ...

  3. ERROR: Unrecognized command line argument: 'use'

    Unrecognized command line argument: 'use' gvm--GoLang语言多版本管理工具 基础环境 centos6.5 报错内容 gvm在命令行以外的任何地方调用 ...

  4. How to build .apk file from command line(转)

    How to build .apk file from command line Created on Wednesday, 29 June 2011 14:32 If you don’t want ...

  5. Can't use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it.

    1.最近使用SVN工具时,Checkout出项目到本地后后,然后将其导入到Intellij idea中开发,在提交svn代码的时候,出现这样的错误:Can't use Subversion comma ...

  6. How to Use Android ADB Command Line Tool

    Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Andro ...

  7. Chrome-Console( Command Line API Reference)

    来源于:https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference The Comma ...

  8. logoff remote desktop sessions via command line tools

    This trick I learned from my one of ex-college.  In Windows servers, only two remote desktop session ...

  9. 使用intellij的svn时提示出错: Can't use Subversion command line client: svn.Errors found while svn working copies detection.

    使用Intellij的svn时提示出错:Can't use Subversion command line client: svn. Errors found while svn working co ...

随机推荐

  1. bzoj 2326 矩阵乘法

    [HNOI2011]数学作业 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2415  Solved: 1413[Submit][Status][Di ...

  2. 应对ADT(Eclipse)的No more handles解决办法

    应对ADT(Eclipse)的No more handles解决方法 ADT(Eclipse)最近几天经常出现如下错误对话框:org.eclipse.swt.SWTError: No more han ...

  3. 收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。

    AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...

  4. 洛谷P3406 海底高铁

    题目背景 大东亚海底隧道连接着厦门.新北.博艾.那霸.鹿儿岛等城市,横穿东海,耗资1000亿博艾元,历时15年,于公元2058年建成.凭借该隧道,从厦门可以乘坐火车直达台湾.博艾和日本,全程只需要4个 ...

  5. CritterAI与Recast Navigation寻路

    版权声明:本文为博主吴欣伟原创文章,未经博主允许不得转载. 前言 这篇文章写于去年,由于工作需要,故写出这个研究文档,发现网上有关此寻路库的中文资源十分稀少,故发布出来与诸位共享交流,如文中有不对之处 ...

  6. 为什么utf8占用3个字节

    UNICODE是万能编码,包含了所有符号的编码,它规定了所有符号在计算机底层的二进制的表示顺序.有关Unicode为什么会出现就不叙述了,Unicode是针对所有计算机的使用者定义一套统一的编码规范, ...

  7. Access to Image at 'file:///Users canvas本地图片跨域报错解决方案

    1.设置跨域 添加跨域条件   crossorigin="anonymous" 前提是后端支持这个图片跨域 2.上面加了之后还是报错 如标题所示 你需要把你的项目放到服务器上面跑 ...

  8. windows安装ZIP压缩版的Weblogic Server

    以前要装Weblogic Server的时候都是装的安装版,最近发现ZIP版本的Weblogic Server是一个只包含Weblogic Server的版本,于是就想着弄一下它. 这里用到的Webl ...

  9. Oracle 数据库管理员的任务

    设计.实施和维护 Oracle 数据库时,按优先次序排列的任务包括:   1. 确定数据库服务器硬件   2. 安装 Oracle 软件   3. 为数据库和安全策略制定计划   4. 创建.移植和打 ...

  10. java-组合优于继承

    组合和继承.都能实现对类的扩展. 差别例如以下表所看到的 组合 继承 has-a关系 is-a关系 执行期决定 编译期决定 不破坏封装,总体和局部松耦合 破坏封装,子类依赖父类 支持扩展,任意添加组合 ...