【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 ...
随机推荐
- 【BZOJ1225】求正整数(数论)
题意:对于任意输入的正整数n,请编程求出具有n个不同因子的最小正整数m. n<=50000 思路:记得以前好像看的是maigo的题解 n即为将m分解为质数幂次的乘积后的次数+1之积 经检验只需要 ...
- Android资源目录结构
资源目录结构 res为资源目录,主要以xml语法编写静态的资源. 资源的命名标准:小写字母和数字,且以小写字母开头. 资源的生成,为了和java语法沟通,资源文件会自动的生成在[gen]目录的R.ja ...
- eslint (js代码检查)
eslint 是一个应用广泛的javascript代码检查工具. 能检测变量名重复等等... 1.安装 npm install -g eslint 2.初始化 会在当前目录下生成一个.eslintrc ...
- 洛谷——P1347 排序
洛谷—— P1347 排序 题目描述 一个不同的值的升序排序数列指的是一个从左到右元素依次增大的序列,例如,一个有序的数列A,B,C,D 表示A<B,B<C,C<D.在这道题中,我们 ...
- java的异常与记录日志
今天在<java编程思想>一书中看到了异常与记录日志,发现学会将异常记录进日志中还是很有必要的,以下是书中的例子: import java.io.PrintWriter; import j ...
- CORS:Source.priciple implimentation in Spring
Cors(Cross-origin Resource Sharing)一种跨域访问技术,基本思想是使用自定义的HTTP头部允许浏览器和服务器相互了解对方,从而决定响应成功与否. CORS与JSONP对 ...
- EXTJS中整合tinymce的富文本编辑器,添加上传图片功能
提供部分代码.Ext.create('Ext.window.Window', { id: 'wind', title: 'CRUD窗口', modal: true, height: 800, widt ...
- Entity framework自定义字段实现思路
ublic class MyModel { public int MyModelID { get; set; } public string FixedProperty1 { get; set; } ...
- Linux OpenSSH后门的加入与防范
引言:相对于Windows,Linux操作系统的password较难获取.只是非常多Linuxserver配置了OpenSSH服务.在获取root权限的情况下,能够通过改动或者更新OpenSSH代码等 ...
- 【C/C++学院】0901-设计模式的汇总演练
备忘录模式 数据库的备份,文档编辑中的撤销等功能 #include <iostream> #include <string> #include <vector> u ...