[c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释
getopt被用来解析命令行选项参数。
#include <unistd.h>
extern char *optarg; //选项的参数指针
extern int optind, //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。
extern int opterr, //当opterr=0时,getopt不向stderr输出错误信息。
extern int optopt; //当命令行选项字符不包括在optstring中或者选项缺少必要的参数时,该选项存储在optopt 中,getopt返回'?’
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> #ifdef RDP2
#define VNCOPT "V:Q"
#else
#define VNCOPT "V:"
#endif int main(int argc, char **argv)
{
int ch, i;
opterr = ; while ((ch = getopt(argc,argv, VNCOPT ":a:bcde::f"))!=-)
{
printf("optind:%d,opterr:%d\n", optind, opterr);
switch(ch)
{
case 'V':
printf("option V:'%s'\n",optarg);
break;
case 'a':
i = strtol(optarg, NULL, );
printf("i = %x\n", i);
printf("option a:'%s'\n",optarg);
break;
case 'b':
printf("option b :b\n");
break;
case 'e':
printf("option e:'%s'\n",optarg);
break;
default:
printf("other option :%c\n",ch);
}
}
for(int i = ; i < argc; i++)
printf("argv[%d] = [%s]\n", i, argv[i]);
printf("argc = [%d]\n", argc);
printf("optopt +%c\n",optopt); return ;
}
执行结果:
$ ./getopt -a -bcd -e
i =
option a:''
optind:,opterr:
option b :b
optind:,opterr:
other option :c
optind:,opterr:
other option :d
optind:,opterr:
option e:''
optind:,opterr:
argv[] = [./getopt]
argv[] = [-a]
argv[] = []
argv[] = [-bcd]
argv[] = [-e]
argv[] = []
argc = []
optopt +e
optind =
argv 数组存储命令行字符串(包含./getopt执行程序)
argc 命令行字符串个数(./getopt -a 123 -bcd -e 456) 6个
optind 下一次调用getopt的时,从optind存储的位置处重新开始检查选项。从0开始计算
(对应下面位置)
./getopt -a 123 -bcd -e 456
0 1 2 3 4 5 6
[c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释的更多相关文章
- main(int argc, char **argv)参数解读
main(int argc, char **argv)参数解读 编译生成了test.exe ,然后在控制台下相应的目录下输入:test 1 2 3 4 argc就是一个输入了多少个参数,包括te ...
- int main(int argc,char* argv[])参数详解
argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数 命令行后面跟的用户输入的参数,比如: int main(int argc, char* argv[] ...
- int main(int argc,char *argv[])参数的应用
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/sta ...
- main()函数的输入参数 main(int argc, char** argv)
一般简单的C++程序,main函数的写法都是 int main() {... ; return 0;},但是,如果在运行程序时需要有参数输入,可以是使用将主函数写成int main(int argv, ...
- 解决 main(int argc, char** argv)这种情况下通过命令行传参,改为main函数里面给定参数。
本文是原创文章,未经允许,请勿转载. 原来程序中是通过运行exe,然后加上参数,然程序运行起来的.也就是main(int argc, char** argv)这里是通过argv参数是从命令行 传过来的 ...
- C++中 int main(int argc, char **argv) 命令行传递参数
C++中,比较常见的是不带参数的主函数int main(),如果使用命令行执行程序,主函数也可以接收预先输入的参数,形式如下. int main(int argc,char **argv) argc: ...
- [笔记] 命令行参数 int main(int argc,char *argv[])
int main(int argc,char *argv[]) // argument count 变量个数 argument values 变量值 C程序的main函数有两个形参* argc:整数, ...
- 关于int main( int argc, char* argv[] ) 中arg和argv参数的解析及调试
https://blog.csdn.net/LYJ_viviani/article/details/51873961 https://stackoverflow.com/questions/30241 ...
- int main(int argc,char* argv[]) 的含义和用法
1.基本概念 argc,argv 用命令行编译程序时有用. 主函数main中变量(int argc,char *argv[ ])的含义,有些编译器允许将main()的返回类型声明为void,这已不再是 ...
随机推荐
- POJ1015 动态规划
POJ1015 问题重述: 在n个候选者中选取m个人进入陪审团.每个候选者获得两项评分:D[j],P[j].求解最佳评审团,使得在每个成员的两项评分和之差 最小的情况下,使得两项评分和之和 最大. 分 ...
- 畅通工程续--hdu1874
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- python小程序之并发连接
import threading import socket import time def conn(): cli = socket.socket() cli.connect(("58.6 ...
- [LA] 3644 - X-Plosives [并查集]
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- 《Programming WPF》翻译 第8章 5.创建动画过程
原文:<Programming WPF>翻译 第8章 5.创建动画过程 所有在这章使用xaml举例说明的技术,都可以在代码中使用,正如你希望的.可是,代码可以使用动画在某种程度上不可能在x ...
- Android实现Live Photos 加源代码
在Android手机上实现类似于iphone中的LivePhoto的功能 源代码分享 github:https://github.com/amazingyyc/DeepRed 代码说明: 1.改变视频 ...
- Android 开发笔记-Eclipse中文乱码
使用eclipse时经常中文乱码网上搜罗了下解决办法: 使用Eclipse编辑文件经常出现中文乱码或者文件中有中文不能保存的问题,Eclipse提供了灵活的设置文件编码格式的选项,我们可以通过设置 ...
- java与.net比较学习系列(7) 属性
文章摘自:http://www.cnblogs.com/mcgrady/p/3411405.html 说起属性,实际上java中没有属性这个概念,只有字段和方法,但是可以通过私有字段和声明get,se ...
- C++: int和string相互转换
假设在一个C++的程序中常常会用到int和string之间的互换.个人建议能够写成一个函数,下次用的时候直接调用就可以. #include <iostream> #include < ...
- [RxJS] Reactive Programming - What is RxJS?
First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...