【413】C 语言 Command line
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
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的更多相关文章
- Visual Studio C++ Command Line
最近在Visual Studio 2012进行vp8的开发,之前一直都是在ubuntu上进行开发,关于编译链接的一些选项可直接在makefile中定义,比如vp8的头文件都放在include文件下面, ...
- 如何在Windows下开发Python:在cmd下运行Python脚本+如何使用Python Shell(command line模式和GUI模式)+如何使用Python IDE
http://www.crifan.com/how_to_do_python_development_under_windows_environment/ 本文目的 希望对于,如何在Windows下, ...
- ERROR: Unrecognized command line argument: 'use'
Unrecognized command line argument: 'use' gvm--GoLang语言多版本管理工具 基础环境 centos6.5 报错内容 gvm在命令行以外的任何地方调用 ...
- 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 ...
- 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 ...
- 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 ...
- Chrome-Console( Command Line API Reference)
来源于:https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference The Comma ...
- 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 ...
- 使用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 ...
随机推荐
- 《effective C++》:条款07——为多态基类声明virtual析构函数
在继承中,基类的析构函数需要定义为虚析构函数数否则: (1)当派生类对象经由一个base类指针删除时,而这个base类的析构函数不是虚函数时,其结果是未定义的. (2)这样做会导致derived类部分 ...
- msp430项目编程32
msp430中项目---电阻测量系统32 Ad 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- HDU——2444 The Accomodation of Students
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- poj——2239 Selecting Courses
poj——2239 Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10656 A ...
- BZOJ4555求和(cdq分治+NTT)
题意: 输出f(n)对998244353(7 × 17 × 223 + 1)取模的结果.1 ≤ n ≤ 100000 其中S(i,j)是第二类Stirling数,即有i个球,丢到j个盒子中,要求盒子不 ...
- send-mail: fatal: parameter inet_interfaces: no local interface found for ::1
转载:http://blog.csdn.net/csdnones/article/details/50717934 发送邮件: [root@iZ23whn33jnZ log]# echo '这是邮件标 ...
- Meteor事件
使用事件是非常简单的.我们将学习如何使用tag,class 和id作为事件选择器. 让我们创建HTML模板三大要素.第一个是 p 标签,第二个是 myClass 类,最后一个是myId. meteor ...
- [正在学习开发板]分享--- iTOP-4412移植CAN
首先拷贝迅为提供的 libcanjni.tar.gz 压缩包到 android 源代码的"iTop4412_ICS/device/samsung/common"文件夹以下,然后使用 ...
- CentOS 6.x ELK(Elasticsearch+Logstash+Kibana)
CentOS 6.x ELK(Elasticsearch+Logstash+Kibana) 前言 Elasticsearch + Logstash + Kibana(ELK)是一套开源的日志管理方案, ...
- Cocos2d-x 3.1.1 学习日志12--一Cocos2dx3.1.1移植到Android平台的方法(最实用最有效的!!)
须要用到工具(依照顺序): 1.JDK 2.NDK 3.ANT 4.Adt-bundle-windows 将JDK文件夹下的bin文件夹路径加入到系统环境变量中. 解压NDK 解压Adt-bundle ...