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. centos 中找不到 php-config

    两种解决方式: 1.CentOS6 PHP extension install: Cannot find php-config. Please use --with-php-config=PATH 2 ...

  2. iOS url带中文下载时 报错解决方法

    问题描述:下载文件时, 请求带中文的URL的资源时,比如:http://s237.sznews.com/pic/2010/11/23/e4fa5794926548ac953a8a525a23b6f2/ ...

  3. Codeforces 938G(cdq分治+可撤销并查集+线性基)

    题意: 有一个无向连通图,支持三个操作: 1 x y d : 新建一条x和y的无向边,长度为d 2 x y    :删除x和y之间的无向边 3 x y    :询问x到y的所有路径中(可以绕环)最短的 ...

  4. loj515 贪心只能过样例(bitset)

    题目: https://loj.ac/problem/515 分析: 所有可能和的最大值是1e6 如果dp的话,dp[i][j]表示前i个数能否凑出和为j的数 这样是O(n^5)的 考虑到[j]可以用 ...

  5. 学习MarkDown--初体验

    学习MarkDownPad 突然兴趣来了,在博客园里面也有Markdown的格式,昨天晚上安装了MarkDownPad pro这个是免费的,但是有些功能不支持.本来想破解的,百度了很多方法感觉不靠谱, ...

  6. UILable怎样加入单击事件

    //初始化UILable UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 320, 40)]; //设置其能够接收用 ...

  7. Jquery改动页面标题title其他JS失效

    Jquery代码   $("title").html("hello"); 后来仅仅好用以下这段js代码来实现 Js代码   document.title=&qu ...

  8. 深入源代码解析Android中的Handler,Message,MessageQueue,Looper

    本文主要是对Handler和消息循环的实现原理进行源代码分析.假设不熟悉Handler能够參见博文< Android中Handler的使用>,里面对Android为何以引入Handler机 ...

  9. web爬虫之登录google paly 商店

    我们先打开Google play 首页 ,点击右上角"登陆"button,即跳到登陆页面 每次我要用爬虫的方式来登陆某个站点的时候,我都会先随便输入一个账号password点击登陆 ...

  10. Windows server 2003 + IIS6 搭建Asp.net MVC执行环境

    安装.Net Framework4.0. 下载地址: http://www.microsoft.com/zh-cn/download/details.aspx?id=17718  安装WindowsS ...